]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Repository.php
e82198a8a2c55ec6e0e6ea07b21fea23c278a0e8
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Repository
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.5dev - 2013-02-26
38 namespace Galette\Repository
;
42 use Galette\Core\Preferences
;
43 use Galette\Core\Login
;
48 * @category Repository
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
57 abstract class Repository
60 protected $preferences;
64 protected $defaults = [];
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)
77 public function __construct(
79 Preferences
$preferences,
86 $this->preferences
= $preferences;
87 $this->login
= $login;
88 $this->prefix
= $prefix;
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);
95 $ent = substr($repo, 0, -1);
99 throw new \
RuntimeException(
100 'Unable to find entity name from repository one. Please ' .
101 'provide entity name in repository constructor'
106 $ns = 'Galette\\Entity';
108 $entity = $ns . '\\' . $entity;
109 if (class_exists($entity)) {
110 $this->entity
= $entity;
112 throw new \
RuntimeException(
113 'Entity class ' . $entity . ' cannot be found!'
117 if (method_exists($this, 'checkUpdate')) {
118 $this->loadDefaults();
119 if (count($this->defaults
)) {
120 $this->checkUpdate();
123 'No defaults loaded!',
131 * Get entity instance
135 public function getEntity()
137 $name = $this->entity
;
150 abstract public function getList();
153 * Add default values in database
155 * @param boolean $check_first Check first if it seem initialized, defaults to true
159 abstract public function installInit($check_first = true);
166 protected function getFilters()
168 return $this->filters
;
174 * @param Object $filters Filters
178 protected function setFilters($filters)
180 $this->filters
= $filters;
184 * Load and get default values
188 protected function loadDefaults()
190 return $this->defaults
;
193 * Is field allowed to order? it shoulsd be present in
194 * provided fields list (those that are SELECT'ed).
196 * @param string $field_name Field name to order by
197 * @param array $fields SELECTE'ed fields
201 protected function canOrderBy($field_name, $fields)
203 if ($fields === null) {
205 } elseif (!is_array($fields)) {
207 } elseif (in_array($field_name, $fields)) {
211 'Trying to order by ' . $field_name . ' while it is not in ' .