]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Repository.php
e82198a8a2c55ec6e0e6ea07b21fea23c278a0e8
[galette.git] / galette / lib / Galette / Repository / Repository.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Repositories
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Repository
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.5dev - 2013-02-26
36 */
37
38 namespace Galette\Repository;
39
40 use Analog\Analog;
41 use Galette\Core\Db;
42 use Galette\Core\Preferences;
43 use Galette\Core\Login;
44
45 /**
46 * Repositories
47 *
48 * @category Repository
49 * @name Repository
50 * @package Galette
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2013-2014 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.7.5dev - 2013-02-26
56 */
57 abstract class Repository
58 {
59 protected $zdb;
60 protected $preferences;
61 protected $entity;
62 protected $login;
63 protected $filters;
64 protected $defaults = [];
65 protected $prefix;
66
67 /**
68 * Main constructor
69 *
70 * @param Db $zdb Database instance
71 * @param Preferences $preferences Galette preferences
72 * @param Login $login Logged in instance
73 * @param string $entity Related entity class name
74 * @param string $ns Related entity namespace
75 * @param string $prefix Prefix (for plugins)
76 */
77 public function __construct(
78 Db $zdb,
79 Preferences $preferences,
80 Login $login,
81 $entity = null,
82 $ns = null,
83 $prefix = ''
84 ) {
85 $this->zdb = $zdb;
86 $this->preferences = $preferences;
87 $this->login = $login;
88 $this->prefix = $prefix;
89
90 if ($entity === null) {
91 //no entity class name provided. Take Repository
92 //class name and remove trailing 's'
93 $r = array_slice(explode('\\', get_class($this)), -1);
94 $repo = $r[0];
95 $ent = substr($repo, 0, -1);
96 if ($ent != $repo) {
97 $entity = $ent;
98 } else {
99 throw new \RuntimeException(
100 'Unable to find entity name from repository one. Please ' .
101 'provide entity name in repository constructor'
102 );
103 }
104 }
105 if ($ns === null) {
106 $ns = 'Galette\\Entity';
107 }
108 $entity = $ns . '\\' . $entity;
109 if (class_exists($entity)) {
110 $this->entity = $entity;
111 } else {
112 throw new \RuntimeException(
113 'Entity class ' . $entity . ' cannot be found!'
114 );
115 }
116
117 if (method_exists($this, 'checkUpdate')) {
118 $this->loadDefaults();
119 if (count($this->defaults)) {
120 $this->checkUpdate();
121 } else {
122 Analog::log(
123 'No defaults loaded!',
124 Analog::ERROR
125 );
126 }
127 }
128 }
129
130 /**
131 * Get entity instance
132 *
133 * @return Object
134 */
135 public function getEntity()
136 {
137 $name = $this->entity;
138 return new $name(
139 $this->zdb,
140 $this->preferences,
141 $this->login
142 );
143 }
144
145 /**
146 * Get list
147 *
148 * @return Object[]
149 */
150 abstract public function getList();
151
152 /**
153 * Add default values in database
154 *
155 * @param boolean $check_first Check first if it seem initialized, defaults to true
156 *
157 * @return boolean
158 */
159 abstract public function installInit($check_first = true);
160
161 /**
162 * Get filters
163 *
164 * @return Object
165 */
166 protected function getFilters()
167 {
168 return $this->filters;
169 }
170
171 /**
172 * Set filters
173 *
174 * @param Object $filters Filters
175 *
176 * @return void
177 */
178 protected function setFilters($filters)
179 {
180 $this->filters = $filters;
181 }
182
183 /**
184 * Load and get default values
185 *
186 * @return array
187 */
188 protected function loadDefaults()
189 {
190 return $this->defaults;
191 }
192 /**
193 * Is field allowed to order? it shoulsd be present in
194 * provided fields list (those that are SELECT'ed).
195 *
196 * @param string $field_name Field name to order by
197 * @param array $fields SELECTE'ed fields
198 *
199 * @return boolean
200 */
201 protected function canOrderBy($field_name, $fields)
202 {
203 if ($fields === null) {
204 return true;
205 } elseif (!is_array($fields)) {
206 return false;
207 } elseif (in_array($field_name, $fields)) {
208 return true;
209 } else {
210 Analog::log(
211 'Trying to order by ' . $field_name . ' while it is not in ' .
212 'selected fields.',
213 Analog::WARNING
214 );
215 return false;
216 }
217 }
218 }