PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/aamenu/code/trunk/administrator/components/com_aamenu/libraries/jxtended/application/component/model.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 342 lines | 199 code | 40 blank | 103 comment | 22 complexity | 83fe0adc456357078ee084c9135d3d36 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: model.php 36 2009-05-19 01:06:55Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Application.Component
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. jximport('jxtended.database.helper');
  12. jximport('joomla.application.component.model');
  13. if (!function_exists( 'property_exists' ))
  14. {
  15. function property_exists( $class, $property )
  16. {
  17. static $class_vars;
  18. if (is_object( $class )) {
  19. $class = get_class( $class );
  20. }
  21. if (!isset( $class_vars['class'] )) {
  22. $class_vars['class'] = get_class_vars( $class );
  23. }
  24. return array_key_exists( $property, $class_vars['class'] );
  25. }
  26. }
  27. /**
  28. * Abstract Model class
  29. *
  30. * @abstract
  31. * @package JXtended.Libraries
  32. * @subpackage Application
  33. */
  34. class JXModel extends JModel
  35. {
  36. /**
  37. * @protected
  38. * @var JTable A persistant Table resource
  39. */
  40. var $_resource = null;
  41. /**
  42. * @protected
  43. * @var int The count of items returned by getList
  44. */
  45. var $_total = 0;
  46. /**
  47. * @access private
  48. * @var boolean Has the state been autoset yet
  49. */
  50. var $__state_set = false;
  51. /**
  52. * Overridden constructor
  53. *
  54. * @access protected
  55. * @param array Configuration array
  56. */
  57. function __construct($config = array())
  58. {
  59. if (!empty($config['ignore_request'])) {
  60. $this->__state_set = true;
  61. }
  62. parent::__construct($config);
  63. }
  64. /**
  65. * Get's a list of categories objects
  66. * @param array Names array of field-value filters (published|section)
  67. * @param boolean True if foreign keys are to be resolved
  68. * @return int
  69. */
  70. function getListCount( $filters, $resolveFKs=false )
  71. {
  72. $qb = $this->_getListQuery( $filters, $resolveFKs );
  73. $this->_db->setQuery( $qb->toString() );
  74. $this->_db->query();
  75. return $this->_db->getNumRows();
  76. }
  77. /**
  78. * Get's a list of categories objects
  79. * @param array Names array of field-value filters (published|section)
  80. * @param boolean True if foreign keys are to be resolved
  81. * @param string An optional key by which to index the result array
  82. * @return array
  83. */
  84. function &getList( $filters=array(), $resolveFKs=false, $key='' )
  85. {
  86. $limit = @$filters['limit'];
  87. $limitstart = @$filters['limitstart'];
  88. $qb = $this->_getListQuery( $filters, $resolveFKs );
  89. $this->_db->setQuery( $qb->toString(), $limitstart, $limit );
  90. $result = $this->_db->loadObjectList( $key );
  91. return $result;
  92. }
  93. /**
  94. * @abstract
  95. */
  96. function _getListQuery( $filters=array(), $resolveFKs=false )
  97. {
  98. return null;
  99. }
  100. /**
  101. * @return object A pagination object
  102. */
  103. function &getPagination()
  104. {
  105. static $instance;
  106. if (!$instance)
  107. {
  108. jximport('jxtended.html.pagination');
  109. $state = &$this->getState();
  110. $instance = new JXPagination( $this->_total, $state->get( 'limitstart'), $state->get( 'limit' ), $state->get( 'pagination_mode' ) );
  111. }
  112. return $instance;
  113. }
  114. /**
  115. * Gets the resource
  116. * @return JTable
  117. */
  118. function &getResource()
  119. {
  120. return $this->_resource;
  121. }
  122. /**
  123. * Get's a list of heirarchial records
  124. * @param array Names array of field-value filters (see getList)
  125. * @param boolean True if foreign keys are to be resolved
  126. * @param string The name of the text field to display, defaults to name
  127. * @param string The name of the parent id field
  128. * @param int Tree Type
  129. */
  130. function &getTree( $filters, $resolveFKs=false, $textName='name', $parentName='parent_id' )
  131. {
  132. $result = $this->getList( $filters, $resolveFKs );
  133. jximport('jxtended.tree');
  134. $tree = new JXTree();
  135. $tree->importObjectList( $result, 'id', $parentName, $textName );
  136. return $tree;
  137. }
  138. //
  139. // Common methods
  140. //
  141. function checkout()
  142. {
  143. $result = true;
  144. if ($id = (int) $this->getState( 'id' ))
  145. {
  146. $user = &JFactory::getUser();
  147. $userId = $user->get( 'id' );
  148. $table = &$this->getResource();
  149. if (property_exists( $table, 'checked_out' ))
  150. {
  151. $table->load( $id );
  152. if ($table->isCheckedOut( $userId, $table->checked_out )) {
  153. $result = &JError::raiseNotice( 500, 'errorItemCheckedOut' );
  154. } else {
  155. $table->checkout( $userId );
  156. }
  157. }
  158. }
  159. return $result;
  160. }
  161. /**
  162. * Method to checkin/unlock a row
  163. *
  164. * @access public
  165. * @return boolean True on success
  166. * @since 1.5
  167. */
  168. function checkin()
  169. {
  170. $result = true;
  171. if ($id = (int) $this->getState( 'id' ))
  172. {
  173. $table = &$this->getResource();
  174. if (!$table->checkin($id)) {
  175. $result = new JException( $table->getError() );
  176. }
  177. }
  178. return $result;
  179. }
  180. /**
  181. * Method to checkin/unlock a row
  182. *
  183. * @access public
  184. * @param array An array of id numbers
  185. * @param integer 0 if unpublishing, 1 if publishing
  186. * @return boolean True on success
  187. * @since 1.5
  188. */
  189. function publish( $cid, $value )
  190. {
  191. $result = true;
  192. $user = & JFactory::getUser();
  193. $table = &$this->getResource();
  194. if (!$table->publish( $cid, $value, $user->get( 'id' ) )) {
  195. $result = JError::raiseWarning( 500, $table->getError() );
  196. }
  197. return $result;
  198. }
  199. /**
  200. * @param array Source array
  201. * @param int User id
  202. * @return mixed A JTable, or false if unsuccessful
  203. */
  204. function save( $input = array(), $user_id=0 )
  205. {
  206. $result = true;
  207. $user = &JFactory::getUser();
  208. $table = &$this->getResource();
  209. // parse out the params form elements
  210. $params = @$input['params'];
  211. if (is_array( $params ))
  212. {
  213. $registry = new JRegistry();
  214. $registry->loadArray( $params );
  215. $input['params'] = $registry->toString();
  216. }
  217. if ($k = $table->getKeyName())
  218. {
  219. if (isset( $input[$k] ))
  220. {
  221. global $mainframe;
  222. $txOffset = $mainframe->getCfg( 'offset' );
  223. $now = date( 'Y-m-d H:i:s', time() + $txOffset * 3600 );
  224. if ($input[$k] == 0)
  225. {
  226. $input['created_date'] = $now;
  227. $input['created_user_id'] = $user->get('id');
  228. }
  229. else
  230. {
  231. $input['modified_date'] = $now;
  232. $input['modified_user_id'] = $user->get('id');
  233. }
  234. }
  235. }
  236. if (!$table->save( $input )) {
  237. $result = JError::raiseWarning( 500, $table->getError() );
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Delete an item
  243. * @param array|int A single or array of primary keys
  244. */
  245. function delete( $input )
  246. {
  247. $table = &$this->getResource();
  248. if (is_array( $input ))
  249. {
  250. foreach ($input as $id) {
  251. $table->delete( (int) $id );
  252. }
  253. return true;
  254. }
  255. else {
  256. return $table->delete( (int) $input );
  257. }
  258. }
  259. /**
  260. * @param array An array of primary keys
  261. * @param int Increment, usually +1 or -1
  262. * @return boolean
  263. */
  264. function ordering( $input, $inc=0 )
  265. {
  266. global $mainframe;
  267. $database = &$this->getDBO();
  268. $user = &JFactory::getUser();
  269. $table = $this->getResource();
  270. JArrayHelper::toInteger( $input );
  271. if (count( $input ))
  272. {
  273. $cids = 'id=' . implode( ' OR id=', $input );
  274. $hasCO = property_exists( $table, 'checked_out' );
  275. $query = 'UPDATE ' . $table->getTableName()
  276. . ' SET ordering = ordering + ' . (int) $inc
  277. . ' WHERE (' . $cids . ')'
  278. . ($hasCO ? ' AND (checked_out = 0 OR checked_out = ' . (int) $user->get( 'id' ) . ')' : '' )
  279. ;
  280. $database->setQuery( $query );
  281. if (!$database->query())
  282. {
  283. $this->setError( $database->getErrorMsg() );
  284. }
  285. else
  286. {
  287. return true;
  288. }
  289. }
  290. }
  291. /**
  292. * Set the internal resource
  293. * @param JTable The table object
  294. * @return JTable The previous value of the property
  295. */
  296. function &setResource( &$value )
  297. {
  298. $oldValue = &$this->_resource;
  299. $this->_resource = $value;
  300. return $oldValue;
  301. }
  302. }