source: OWR/Model.php @ 47c42d

Revision 47c42d, 4.5 KB checked in by pierre-alain <pierre-alain@…>, 3 years ago (diff)

adding support for plugin calls

  • Property mode set to 100644
Line 
1<?php
2/**
3 * Model Object base class
4 *
5 * PHP 5
6 *
7 * OWR - OpenWebReader
8 *
9 * Copyright (c) 2009, Pierre-Alain Mignot
10 *
11 * Home page: http://openwebreader.org
12 *
13 * E-Mail: contact@openwebreader.org
14 *
15 * All Rights Reserved
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *
31 * @author Pierre-Alain Mignot <contact@openwebreader.org>
32 * @copyright Copyright (c) 2009, Pierre-Alain Mignot
33 * @license http://www.gnu.org/copyleft/gpl.html
34 * @package OWR
35 */
36namespace OWR;
37use OWR\Interfaces\Model as iModel,
38    OWR\User;
39/**
40 * This class is used as base class for all DAO objects and defines all usefull functions
41 * Please ensure that all the non-static public functions returns $this
42 *
43 * @abstract
44 * @uses OWR\Interfaces\Model implements the Model interface
45 * @uses OWR\DB the database link
46 * @uses OWR\Request a request sent to the database
47 * @uses OWR\DB\Result a DB\Result from the database
48 * @uses OWR\Exception the exceptions handler
49 * @uses OWR\User the user
50 * @package OWR
51 */
52abstract class Model implements iModel
53{
54    /**
55    * @var string the table name
56    * @access protected
57    */
58    protected $_name = '';
59
60    /**
61    * @var string the class name
62    * @access protected
63    */
64    protected $_fullName = '';
65
66    /**
67    * @var mixed the DB instance
68    * @access protected
69    */
70    protected $_db;
71
72    /**
73    * @var mixed the DAO instance for the current class
74    * @access protected
75    */
76    protected $_dao;
77
78    /**
79    * @var array stored already processed model names
80    * @access private
81    * @static
82    */
83    static private $_models = array();
84
85    /**
86    * @var array stored already processed models objects
87    * @access private
88    * @static
89    */
90    static private $_cachedModels = array();
91
92    /**
93     * Constructor, sets the name/fullname of the instance, and set the DB obj
94     *
95     * @access public
96     * @author Pierre-Alain Mignot <contact@openwebreader.org>
97     */
98    protected function __construct()
99    {
100        $this->_db = DB::iGet(); // we assume here that the connexion has already been done, be carefull
101        $this->_fullName = get_called_class();
102        $this->_name = str_replace('\\', '_', substr($this->_fullName, strlen(__NAMESPACE__.'_Model_')));
103        $this->_dao = DAO::getDAO($this->_name);
104    }
105
106    /**
107     * Returns the specified Model object
108     *
109     * @access public
110     * @static
111     * @author Pierre-Alain Mignot <contact@openwebreader.org>
112     * @param string $model the name of the Model
113     * @return mixed the Model object
114     */
115    static public function getModel($model)
116    {
117        if(!isset(self::$_models[$model]))
118        {
119            $c = __NAMESPACE__.'\Model\\'.join('\\', array_map('ucfirst', explode('_', (string) $model)));
120            self::$_models[$model] = $c;
121        }
122        return new self::$_models[$model];
123    }
124
125    /**
126     * Returns the specified Model object from cache
127     *
128     * @access public
129     * @static
130     * @author Pierre-Alain Mignot <contact@openwebreader.org>
131     * @param string $model the name of the Model
132     * @return mixed the Model object
133     */
134    static public function getCachedModel($model)
135    {
136        $model = ucfirst((string) $model);
137        isset(self::$_cachedModels[$model]) || (self::$_cachedModels[$model] = self::getModel($model));
138
139        return self::$_cachedModels[$model];
140    }
141
142    /**
143     * Sets the user's timestamp, which corresponds to the last http request timestamp for this user
144     *
145     * @author Pierre-Alain Mignot <contact@openwebreader.org>
146     * @access public
147     * @param array $datas the datas retrieved from DB
148     */
149    protected function _setUserTimestamp(array $datas)
150    {
151        if(isset($datas['id']))
152            User::iGet()->setTimestamp($datas['id']);
153        else
154        {
155            foreach($datas as $data)
156                User::iGet()->setTimestamp($data['id']);
157        }
158    }
159}
Note: See TracBrowser for help on using the repository browser.