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

/source/administrator/components/com_paypalaccess/lib/model.php

https://github.com/yireo/pkg_paypalaccess
PHP | 1752 lines | 845 code | 208 blank | 699 comment | 283 complexity | 506a165fe23094fbaaa03d0e197a98fc MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Joomla! Yireo Library
  4. *
  5. * @author Yireo (http://www.yireo.com/)
  6. * @package YireoLib
  7. * @copyright Copyright 2014
  8. * @license GNU Public License
  9. * @link http://www.yireo.com/
  10. * @version 0.6.0
  11. */
  12. // Check to ensure this file is included in Joomla!
  13. defined('_JEXEC') or die();
  14. // Import the loader
  15. require_once dirname(__FILE__).'/loader.php';
  16. /**
  17. * Yireo Abstract Model
  18. *
  19. * @package Yireo
  20. */
  21. if(YireoHelper::isJoomla25() || YireoHelper::isJoomla15()) {
  22. jimport('joomla.application.component.model');
  23. class YireoAbstractModel extends JModel {}
  24. } else {
  25. class YireoAbstractModel extends JModelLegacy {}
  26. }
  27. /**
  28. * Yireo Model
  29. *
  30. * @package Yireo
  31. */
  32. class YireoCommonModel extends YireoAbstractModel
  33. {
  34. /*
  35. * Boolean to skip table-detection
  36. *
  37. * @protected int
  38. */
  39. protected $_skip_table = true;
  40. /*
  41. * Boolean to allow forms in the frontend
  42. *
  43. * @protected int
  44. */
  45. protected $_frontend_form = false;
  46. /**
  47. * Database table object
  48. *
  49. * @protected string
  50. */
  51. protected $_tbl = null;
  52. /**
  53. * Database table-name
  54. *
  55. * @protected string
  56. */
  57. protected $_tbl_name = null;
  58. /**
  59. * Database table-alias
  60. *
  61. * @protected string
  62. */
  63. protected $_tbl_alias = null;
  64. /**
  65. * Database primary key
  66. *
  67. * @protected string
  68. */
  69. protected $_tbl_key = null;
  70. /**
  71. * Flag to automatically set the table class prefix
  72. *
  73. * @protected boolean
  74. */
  75. protected $_tbl_prefix_auto = false;
  76. /**
  77. * Unique id
  78. *
  79. * @protected int
  80. */
  81. protected $_id = null;
  82. /**
  83. * Data array
  84. *
  85. * @protected array
  86. */
  87. protected $_data = null;
  88. /**
  89. * Override the default method to allow for skipping table creation
  90. *
  91. * @access public
  92. * @subpackage Yireo
  93. * @param string $name
  94. * @param string $prefix
  95. * @param array $options
  96. * @return mixed
  97. */
  98. public function getTable($name = '', $prefix = 'Table', $options = array())
  99. {
  100. if ($this->_skip_table == true) return null;
  101. if (empty($name)) $name = $this->_tbl_alias;
  102. if (!empty($this->_tbl_prefix)) $prefix = $this->_tbl_prefix;
  103. return parent::getTable($name, $prefix, $options);
  104. }
  105. }
  106. /**
  107. * Yireo Model
  108. *
  109. * @package Yireo
  110. */
  111. class YireoModel extends YireoCommonModel
  112. {
  113. /**
  114. * Indicator if this is a model for multiple or single entries
  115. *
  116. * @protected int
  117. */
  118. protected $_single = null;
  119. /**
  120. * Boolean to allow for caching
  121. *
  122. * @protected int
  123. */
  124. protected $_cache = false;
  125. /**
  126. * Boolean to allow for debugging
  127. *
  128. * @protected int
  129. */
  130. protected $_debug = false;
  131. /**
  132. * Boolean to allow for filtering
  133. *
  134. * @protected int
  135. */
  136. protected $_allow_filter = true;
  137. /**
  138. * Boolean to allow for checking out
  139. *
  140. * @protected int
  141. */
  142. protected $_checkout = true;
  143. /*
  144. * Boolean to skip table-detection
  145. *
  146. * @protected int
  147. */
  148. protected $_skip_table = false;
  149. /**
  150. * Category total
  151. *
  152. * @protected integer
  153. */
  154. protected $_total = null;
  155. /**
  156. * Pagination object
  157. *
  158. * @protected object
  159. */
  160. protected $_pagination = null;
  161. /**
  162. * List limit
  163. *
  164. * @protected int
  165. */
  166. protected $_limit = null;
  167. /**
  168. * Limit start
  169. *
  170. * @protected int
  171. */
  172. protected $_limitstart = null;
  173. /**
  174. * Ordering field
  175. *
  176. * @protected string
  177. */
  178. protected $_ordering = null;
  179. /**
  180. * Where segments
  181. *
  182. * @protected array
  183. */
  184. protected $_where = array();
  185. /**
  186. * Search columns
  187. *
  188. * @protected array
  189. */
  190. protected $_search = array();
  191. /**
  192. * Order-by segments
  193. *
  194. * @protected array
  195. */
  196. protected $_orderby = array();
  197. /**
  198. * Group-by segments
  199. *
  200. * @protected array
  201. */
  202. protected $_groupby = array();
  203. /**
  204. * Extra query segments
  205. *
  206. * @protected array
  207. */
  208. protected $_extra = array();
  209. /**
  210. * Order-by default-value
  211. *
  212. * @protected string
  213. */
  214. protected $_orderby_default = null;
  215. /**
  216. * Order-by default-title
  217. *
  218. * @protected string
  219. */
  220. protected $_orderby_title = null;
  221. /**
  222. * List of fields to autoconvert into column-seperated fields
  223. *
  224. * @protected array
  225. */
  226. protected $_columnFields = array();
  227. /**
  228. * Enable the limit in the query (or in the data-array)
  229. *
  230. * @protected string
  231. */
  232. protected $_limit_query = false;
  233. /**
  234. * Flag to force a value
  235. *
  236. * @constant boolean
  237. */
  238. const FORCE_NEW = true;
  239. /**
  240. * Constructor
  241. *
  242. * @access public
  243. * @subpackage Yireo
  244. * @param string $tableAlias
  245. * @return null
  246. */
  247. public function __construct($tableAlias = null)
  248. {
  249. // Import use full variables from JFactory
  250. $this->application = JFactory::getApplication();
  251. $this->user = JFactory::getUser();
  252. // Create the option-namespace
  253. $classParts = explode('Model', get_class($this));
  254. $this->_view = (!empty($classParts[1])) ? strtolower($classParts[1]) : JRequest::getCmd('view');
  255. $this->_option = $this->getOption();
  256. $this->_option_id = $this->_option.'_'.$this->_view.'_';
  257. if ($this->application->isSite()) $this->_option_id .= JRequest::getInt('Itemid').'_';
  258. $this->_component = preg_replace('/^com_/', '', $this->_option);
  259. $this->_component = preg_replace('/[^A-Z0-9_]/i', '', $this->_component);
  260. $this->_component = str_replace(' ', '', ucwords(str_replace('_', ' ', $this->_component)));
  261. // Call the parent constructor
  262. parent::__construct();
  263. // Set the database variables
  264. if($this->_tbl_prefix_auto == true) $this->_tbl_prefix = $this->_component.'Table';
  265. // @todo: Set this in a metadata array
  266. $this->_tbl_alias = $tableAlias;
  267. $this->_tbl = $this->getTable($tableAlias);
  268. $this->_tbl_name = $this->_tbl->getTableName();
  269. $this->_tbl_key = $this->_tbl->getKeyName();
  270. $this->_entity = $tableAlias;
  271. // Detect the orderby-default
  272. if(empty($this->_orderby_default)) $this->_orderby_default = $this->_tbl->getDefaultOrderBy();
  273. if(empty($this->_orderby_title)) {
  274. if ($this->_tbl->hasField('title')) $this->_orderby_title = 'title';
  275. if ($this->_tbl->hasField('label')) $this->_orderby_title = 'label';
  276. if ($this->_tbl->hasField('name')) $this->_orderby_title = 'name';
  277. }
  278. // Detect checkout
  279. if ($this->_tbl->hasField('checked_out')) {
  280. $this->_checkout = true;
  281. } else {
  282. $this->_checkout = false;
  283. }
  284. // Set the parameters for the frontend
  285. if (empty($this->params)) {
  286. if ($this->application->isSite() == false) {
  287. $this->params = JComponentHelper::getParams($this->_option);
  288. } else {
  289. $this->params = $this->application->getParams($this->_option);
  290. }
  291. }
  292. // Enable debugging
  293. if($this->params->get('debug', 0) == 1) $this->_debug = true;
  294. // Determine whether this model is single or not
  295. if ($this->_single == null) {
  296. $className = get_class($this);
  297. if (preg_match('/s$/', $className)) {
  298. $this->_single = false;
  299. } else {
  300. $this->_single = true;
  301. }
  302. }
  303. // Initialize the ID for single records
  304. if ($this->isSingular()) {
  305. $cid = JRequest::getVar( 'cid', array(0), '', 'array' );
  306. if (!empty($cid) && count($cid) > 0) {
  307. $this->setId((int)$cid[0]);
  308. }
  309. $id = JRequest::getInt( 'id', 0 );
  310. if (!empty($id) && $id > 0) {
  311. $this->setId((int)$id);
  312. }
  313. // Multiple records
  314. } else {
  315. // Initialize limiting
  316. $this->initLimit();
  317. $this->initLimitstart();
  318. // Initialize ordering
  319. $filter_order = $this->getFilter('order', '{tableAlias}.'.$this->_orderby_default, 'string');
  320. $filter_order_Dir = $this->getFilter('order_Dir');
  321. if (!empty($filter_order_Dir)) $filter_order_Dir = ' '.strtoupper($filter_order_Dir);
  322. $this->addOrderby($filter_order.$filter_order_Dir);
  323. $this->addOrderby('{tableAlias}.'.$this->_orderby_default);
  324. }
  325. }
  326. /**
  327. * Method to get the identifier
  328. *
  329. * @access public
  330. * @subpackage Yireo
  331. * @param null
  332. * @return int
  333. */
  334. public function getId()
  335. {
  336. return $this->_id;
  337. }
  338. /**
  339. * Method to set the identifier
  340. *
  341. * @access public
  342. * @subpackage Yireo
  343. * @param int $id
  344. * @param bool $reinit
  345. * @return null
  346. */
  347. public function setId($id = 0, $reinit = true)
  348. {
  349. $this->_id = $id;
  350. if ($reinit) $this->_data = null;
  351. }
  352. /**
  353. * Method to initialize the limit parameter
  354. *
  355. * @access public
  356. * @subpackage Yireo
  357. * @param null
  358. * @return null
  359. */
  360. public function initLimit($limit = null)
  361. {
  362. if (is_numeric($limit) == false) {
  363. $limit = $this->getFilter('list_limit', $this->application->getCfg('list_limit'));
  364. }
  365. $this->setState('limit', $limit);
  366. }
  367. /**
  368. * Method to initialize the limitstart parameter
  369. *
  370. * @access public
  371. * @subpackage Yireo
  372. * @param null
  373. * @return null
  374. */
  375. public function initLimitstart($limitstart = null)
  376. {
  377. if (is_numeric($limitstart) == false) {
  378. $limitstart = $this->application->getUserStateFromRequest($this->_option_id.'limitstart', 'limitstart', 0, 'int');
  379. }
  380. $this->setState('limitstart', $limitstart);
  381. }
  382. /**
  383. * Method to get a filter from the user-state
  384. *
  385. * @access public
  386. * @subpackage Yireo
  387. * @param string $filter
  388. * @param string $default
  389. * @param string $type
  390. * @param string $option
  391. * @return string
  392. */
  393. public function getFilter($filter = '', $default = '', $type = 'cmd', $option = '')
  394. {
  395. if ($this->_allow_filter == false) return null;
  396. if (empty($option)) $option = $this->_option_id;
  397. $value = $this->application->getUserStateFromRequest( $option.'filter_'.$filter, 'filter_'.$filter, $default, $type );
  398. return $value;
  399. }
  400. /**
  401. * Method to override a default user-state value
  402. *
  403. * @access public
  404. * @subpackage Yireo
  405. * @param string $key
  406. * @param mixed $value
  407. * @return bool
  408. */
  409. public function overrideUserState( $key, $value )
  410. {
  411. $this->$key = $value ;
  412. return true ;
  413. }
  414. /**
  415. * Method to get data
  416. *
  417. * @access public
  418. * @subpackage Yireo
  419. * @param null
  420. * @return array
  421. */
  422. public function getData($forceNew = false)
  423. {
  424. // Load the data if they are not just set or if the force-flag is set
  425. if ($this->_data === null || $forceNew == self::FORCE_NEW) {
  426. // Load some empty data-set
  427. $this->getEmpty();
  428. // Try to load the temporary data from this session
  429. $this->loadTmpSession();
  430. // Singular model
  431. if ($this->isSingular() && $this->getId() > 0) {
  432. $query = $this->buildQuery();
  433. $data = $this->getDbResult($query, 'object');
  434. if (!empty($data)) {
  435. // Prepare the column-fields
  436. if(!empty($this->_columnFields)) {
  437. foreach($this->_columnFields as $columnField) {
  438. if(!empty($data->$columnField) && !is_array($data->$columnField)) {
  439. $data->$columnField = explode('|', $data->$columnField);
  440. }
  441. }
  442. }
  443. // Allow to modify the data
  444. if (method_exists($this, 'onDataLoad')) {
  445. $data = $this->onDataLoad($data);
  446. }
  447. // Set the ID
  448. $key = $this->getPrimaryKey();
  449. $data->id = $data->$key;
  450. $data->metadata = $this->getMetadata();
  451. $this->_data = $data;
  452. } else {
  453. $data = (object)null;
  454. }
  455. // Check to see if the data is published
  456. $stateField = $this->_tbl->getStateField();
  457. if ($this->application->isSite() && isset($data->$stateField) && $data->$stateField == 0) {
  458. JError::raiseError(404, JText::_('LIB_YIREO_MODEL_NOT_FOUND'));
  459. return;
  460. }
  461. // Fill in non-existing fields
  462. foreach($this->getEmptyFields() as $fieldName => $fieldValue) {
  463. if(!isset($data->$fieldName)) {
  464. $data->$fieldName = $fieldValue;
  465. }
  466. }
  467. // Plural model
  468. } else if ($this->isSingular() == false) {
  469. $query = $this->buildQuery();
  470. $data = $this->getDbResult($query, 'objectList');
  471. if (!empty($data)) {
  472. // Prepare these data
  473. foreach ($data as $index => $item) {
  474. // Frontend permissions
  475. if ($this->application->isSite() && isset($item->access) && is_numeric($item->access)) {
  476. $accessLevels = $this->user->getAuthorisedViewLevels();
  477. if (YireoHelper::isJoomla25()) {
  478. if (!array_key_exists($item->access, $accessLevels) || $accessLevels[$item->access] == 0) {
  479. unset($data[$index]);
  480. continue;
  481. }
  482. } else {
  483. if ($item->access > 0 && !in_array($item->access, $accessLevels)) {
  484. unset($data[$index]);
  485. continue;
  486. }
  487. }
  488. }
  489. // Backend permissions
  490. if ($this->application->isAdmin() && (bool)$this->_tbl->hasAssetId() == true) {
  491. // Get the ID
  492. $key = $this->getPrimaryKey();
  493. $id = $item->$key;
  494. // Asset data
  495. $option = str_replace('com_', '#__', $this->_option);
  496. $view = JRequest::getCmd('view');
  497. // Construct the ACL-data
  498. $action = ($id > 0) ? 'core.manage' : 'core.create';
  499. $itemAsset = ($id > 0) ? $option.'_'.$view.'.'.$id : $option;
  500. // Check
  501. if ($this->user->authorise($action, $itemAsset) == false) {
  502. unset($data[$index]);
  503. continue;
  504. }
  505. }
  506. // Prepare the column-fields
  507. if(!empty($this->_columnFields)) {
  508. foreach($this->_columnFields as $columnField) {
  509. if(!empty($item->$columnField) && !is_array($item->$columnField)) {
  510. $item->$columnField = explode('|', $item->$columnField);
  511. }
  512. }
  513. }
  514. // Prepare the parameters
  515. if (isset($item->params)) {
  516. $item->params = YireoHelper::toParameter($item->params);
  517. } else {
  518. $item->params = YireoHelper::toParameter();
  519. }
  520. // Check for publish_up and publish_down
  521. if ($this->application->isSite()) {
  522. $publish_up = $item->params->get('publish_up');
  523. $publish_down = $item->params->get('publish_down');
  524. if (!empty($publish_up) && strtotime($publish_up) > time()) {
  525. unset($data[$index]);
  526. continue;
  527. } else if (!empty($publish_down) && strtotime($publish_down) < time()) {
  528. unset($data[$index]);
  529. continue;
  530. }
  531. }
  532. // Allow to modify the data
  533. if (method_exists($this, 'onDataLoad')) {
  534. $item = $this->onDataLoad($item);
  535. }
  536. // Add the metadata
  537. $item->metadata = $this->getMetadata();
  538. // Set the ID
  539. $key = $this->getPrimaryKey();
  540. $item->id = $item->$key;
  541. // Fill in non-existing fields
  542. foreach($this->getEmptyFields() as $fieldName => $fieldValue) {
  543. if(!isset($item->$fieldName)) {
  544. $item->$fieldName = $fieldValue;
  545. }
  546. }
  547. // Re-insert this item
  548. $data[$index] = $item;
  549. }
  550. if($this->_limit_query == false) $this->_total = count($data);
  551. $this->_data = $data;
  552. }
  553. }
  554. }
  555. if ($this->isSingular() == false && $this->_limit_query == false && $this->getState('limit') > 0) {
  556. $part = array_slice($this->_data, $this->getState('limitstart'), $this->getState('limit'));
  557. return $part;
  558. }
  559. return $this->_data;
  560. }
  561. /**
  562. * Method to get the total number of records
  563. *
  564. * @access public
  565. * @subpackage Yireo
  566. * @param null
  567. * @return int
  568. */
  569. public function getTotal()
  570. {
  571. // Lets load the content if it doesn't already exist
  572. if (empty($this->_total)) {
  573. // The original database-query did NOT include a LIMIT statement
  574. if ($this->_limit_query == false) {
  575. $this->_total = count($this->_data);
  576. // The original database-query included a LIMIT statement, so we need a second query
  577. } else {
  578. $query = $this->buildQuery();
  579. $query = preg_replace('/^(.*)FROM/sm', 'SELECT COUNT(*) FROM', $query);
  580. $query = preg_replace('/LIMIT(.*)$/', '', $query);
  581. $query = preg_replace('/ORDER\ BY(.*)$/m', '', $query);
  582. $data = $this->getDbResult($query, 'result');
  583. $this->_total = (int)$data;
  584. }
  585. }
  586. return $this->_total;
  587. }
  588. /**
  589. * Method to get a pagination object for the fetched records
  590. *
  591. * @access public
  592. * @subpackage Yireo
  593. * @param null
  594. * @return JPagination
  595. */
  596. public function getPagination()
  597. {
  598. // Lets load the pagination if it doesn't already exist
  599. if (empty($this->_pagination))
  600. {
  601. // Make sure the data is loaded
  602. $this->getData();
  603. $this->getTotal();
  604. // Reset pagination if it does not make sense
  605. if ($this->getState('limitstart') > $this->getTotal()) {
  606. $this->setState('limitstart', 0);
  607. $this->application->setUserState('limitstart', 0);
  608. $this->getData(self::FORCE_NEW);
  609. }
  610. // Build the pagination
  611. jimport('joomla.html.pagination');
  612. $this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
  613. }
  614. return $this->_pagination;
  615. }
  616. /**
  617. * Tests if an item is checked out
  618. *
  619. * @access public
  620. * @subpackage Yireo
  621. * @param int $uid
  622. * @return bool
  623. */
  624. public function isCheckedOut($uid = 0)
  625. {
  626. if ($this->_checkout == true && $this->getData()) {
  627. if ($uid) {
  628. return ($this->_data->checked_out && $this->_data->checked_out != $uid);
  629. } else {
  630. return $this->_data->checked_out;
  631. }
  632. }
  633. return false;
  634. }
  635. /**
  636. * Method to checkin/unlock the table
  637. *
  638. * @access public
  639. * @subpackage Yireo
  640. * @param null
  641. * @return bool
  642. */
  643. public function checkin()
  644. {
  645. if ($this->_checkout == false) {
  646. return true;
  647. }
  648. if ($this->_id) {
  649. if (!$this->_tbl->checkin($this->_id)) {
  650. $this->setError($this->_db->getErrorMsg());
  651. return false;
  652. }
  653. }
  654. return false;
  655. }
  656. /**
  657. * Method to checkout/lock the table
  658. *
  659. * @access public
  660. * @subpackage Yireo
  661. * @param int $uid
  662. * @return bool
  663. */
  664. public function checkout($uid = null)
  665. {
  666. if ($this->_checkout == true) {
  667. return true;
  668. }
  669. if ($this->_id) {
  670. // Make sure we have a user id to checkout the item with
  671. if (is_null($uid)) {
  672. $uid = $this->user->get('id');
  673. }
  674. // Lets get to it and checkout the thing...
  675. if (!$this->_tbl->checkout($uid, $this->_id)) {
  676. $this->setError($this->_db->getErrorMsg());
  677. return false;
  678. }
  679. return true;
  680. }
  681. return false;
  682. }
  683. /**
  684. * Method to store the model
  685. *
  686. * @access public
  687. * @subpackage Yireo
  688. * @param mixed $data
  689. * @return bool
  690. */
  691. public function store($data)
  692. {
  693. // Check the integrity of data
  694. if (empty($data) || !is_array($data)) {
  695. $this->setError('Invalid data');
  696. $this->saveTmpSession($data);
  697. return false;
  698. }
  699. // Get the user metadata
  700. jimport('joomla.utilities.date');
  701. $now = new JDate('now');
  702. $uid = $this->user->get('id');
  703. // Convert the JForm array into the default data-set
  704. $fieldgroups = array('text', 'basic', 'item');
  705. foreach($fieldgroups as $fieldgroup) {
  706. if (!empty($data[$fieldgroup]) && is_array($data[$fieldgroup])) {
  707. foreach($data[$fieldgroup] as $name => $value) {
  708. $data[$name] = $value;
  709. }
  710. unset($data[$fieldgroup]);
  711. }
  712. }
  713. // Automatically set some data
  714. $data['modified'] = (method_exists('JDate', 'toSql')) ? $now->toSql() : $now->toMySQL();
  715. $data['modified_date'] = (method_exists('JDate', 'toSql')) ? $now->toSql() : $now->toMySQL();
  716. $data['modified_by'] = $uid;
  717. // Set the creation date if the item is new
  718. if (empty($data['id']) || $data['id'] == 0) {
  719. $data['created'] = (method_exists('JDate', 'toSql')) ? $now->toSql() : $now->toMySQL();
  720. $data['created_date'] = (method_exists('JDate', 'toSql')) ? $now->toSql() : $now->toMySQL();
  721. $data['created_by'] = $uid;
  722. }
  723. // Autocorrect the publish_up and publish_down dates
  724. if (isset($data['params']['publish_up']) && isset($data['params']['publish_down'])) {
  725. $publish_up = strtotime($data['params']['publish_up']);
  726. $publish_down = strtotime($data['params']['publish_down']);
  727. if ($publish_up >= $publish_down) $data['params']['publish_down'] = null;
  728. }
  729. // All parameters to override these values
  730. if (isset($data['params']) && is_array($data['params'])) {
  731. if (!empty( $data['params']['created'])) $data['created'] = $data['params']['created'];
  732. if (!empty( $data['params']['created_date'])) $data['created'] = $data['params']['created_date'];
  733. if (!empty( $data['params']['created_by'])) $data['created_by'] = $data['params']['created_by'];
  734. if (!empty( $data['params']['modified'])) $data['modified'] = $data['params']['modified'];
  735. if (!empty( $data['params']['modified_date'])) $data['modified'] = $data['params']['modified_date'];
  736. if (!empty( $data['params']['modified_by'])) $data['modified_by'] = $data['params']['modified_by'];
  737. }
  738. // Unset these parameters
  739. if (isset($data['params']) && is_array($data['params'])) {
  740. if (isset($data['params']['created'])) unset( $data['params']['created'] );
  741. if (isset($data['params']['created_date'])) unset( $data['params']['created_date'] );
  742. if (isset($data['params']['created_by'])) unset( $data['params']['created_by'] );
  743. if (isset($data['params']['modified'])) unset( $data['params']['modified'] );
  744. if (isset($data['params']['modified_date'])) unset( $data['params']['modified_date'] );
  745. if (isset($data['params']['modified_by'])) unset( $data['params']['modified_by'] );
  746. }
  747. // Prepare the column-fields
  748. if(!empty($this->_columnFields)) {
  749. foreach($this->_columnFields as $columnField) {
  750. if(!empty($data[$columnField]) && is_array($data[$columnField])) {
  751. $data[$columnField] = implode('|', $data[$columnField]);
  752. }
  753. }
  754. }
  755. // Bind the form fields to the table
  756. if (!$this->_tbl->bind($data)) {
  757. $this->setError($this->_db->getErrorMsg());
  758. $this->saveTmpSession($data);
  759. return false;
  760. }
  761. // Make sure the table is valid
  762. if (!$this->_tbl->check()) {
  763. $this->setError($this->_tbl->getErrorMsg());
  764. $this->saveTmpSession($data);
  765. return false;
  766. }
  767. // Store the table to the database
  768. if (!$this->_tbl->store()) {
  769. $this->setError($this->_db->getErrorMsg());
  770. $this->saveTmpSession($data);
  771. return false;
  772. }
  773. // Try to fetch the last ID from the table
  774. $id = $this->_tbl->getLastInsertId();
  775. if ((!isset($this->_id) || !$this->_id > 0) && $id > 0) {
  776. $this->_id = $id;
  777. }
  778. return true;
  779. }
  780. /**
  781. * Method to remove multiple items
  782. *
  783. * @access public
  784. * @subpackage Yireo
  785. * @param array $cid
  786. * @return bool
  787. */
  788. public function delete($cid = array())
  789. {
  790. if (count($cid) && !empty($this->_tbl_name) && !empty($this->_tbl_key)) {
  791. JArrayHelper::toInteger($cid);
  792. $cids = implode( ',', $cid );
  793. $query = 'DELETE FROM '.$this->_tbl_name.' WHERE '.$this->_tbl_key.' IN ( '.$cids.' )';
  794. $this->_db->setQuery( $query );
  795. if (!$this->_db->query()) {
  796. $this->setError($this->_db->getErrorMsg());
  797. return false;
  798. }
  799. }
  800. return true;
  801. }
  802. /**
  803. * Method to (un)publish an item
  804. *
  805. * @access public
  806. * @subpackage Yireo
  807. * @param array $cid
  808. * @param int $publish
  809. * @return bool
  810. */
  811. public function publish($cid = array(), $publish = 1)
  812. {
  813. if (count($cid)) {
  814. $return = $this->_tbl->publish($cid, $publish, $this->user->get('id'));
  815. return $return;
  816. }
  817. return true;
  818. }
  819. /**
  820. * Method to move an item
  821. *
  822. * @access public
  823. * @subpackage Yireo
  824. * @param mixed $direction
  825. * @param string $field_name
  826. * @param int $field_id
  827. * @return bool
  828. */
  829. public function move($direction, $field_name = null, $field_id = null)
  830. {
  831. if (!$this->_tbl->load($this->_id)) {
  832. $this->setError($this->_db->getErrorMsg());
  833. return false;
  834. }
  835. if (!empty($field_name) && !empty($field_id)) {
  836. $rt = $this->_tbl->move($direction, ' '.$field_name.' = '.$field_id);
  837. } else {
  838. $rt = $this->_tbl->move($direction);
  839. }
  840. if ($rt == false) {
  841. $this->setError($this->_db->getErrorMsg());
  842. return false;
  843. }
  844. return true;
  845. }
  846. /**
  847. * Method to reorder items
  848. *
  849. * @access public
  850. * @subpackage Yireo
  851. * @param array $cid
  852. * @param string $order
  853. * @return bool
  854. */
  855. public function saveorder($cid = array(), $order)
  856. {
  857. $groupings = array();
  858. // update ordering values
  859. for( $i=0; $i < count($cid); $i++ ) {
  860. // Load the table
  861. $this->_tbl->load((int)$cid[$i]);
  862. // Track extra fields
  863. if ($this->_tbl->hasField('category_id')) {
  864. $groupings['category_id'] = $this->_tbl->category_id;
  865. } else if ($this->_tbl->hasField('parent_id')) {
  866. $groupings['parent_id'] = $this->_tbl->parent_id;
  867. }
  868. // Save the ordering
  869. $ordering = $this->_tbl->getDefaultOrderBy();
  870. if ($this->_tbl->$ordering != $order[$i]) {
  871. $this->_tbl->$ordering = $order[$i];
  872. if (!$this->_tbl->store()) {
  873. $this->setError($this->_db->getErrorMsg());
  874. return false;
  875. }
  876. }
  877. }
  878. // Execute updateOrder for each parent group
  879. $groupings = array_unique($groupings);
  880. foreach ($groupings as $fieldName => $group){
  881. $this->_tbl->reorder($fieldName.' = '.(int)$group);
  882. }
  883. return true;
  884. }
  885. /**
  886. * Method to increment the hit counter for the item
  887. *
  888. * @access public
  889. * @return bool
  890. */
  891. public function hit()
  892. {
  893. if ($this->_id)
  894. {
  895. $this->_tbl->hit($this->_id);
  896. return true;
  897. }
  898. return false;
  899. }
  900. /**
  901. * Method to toggle a certain field
  902. *
  903. * @access public
  904. * @return bool
  905. */
  906. public function toggle($id, $name, $value)
  907. {
  908. if (!$id > 0) return false;
  909. if (empty($name)) return false;
  910. $value = ($value == 1) ? 0 : 1;
  911. $query = 'UPDATE `'.$this->_tbl_name.'` SET `'.$name.'`='.$value.' WHERE `'.$this->_tbl_key.'`='.(int)$id;
  912. $this->_db->setQuery($query);
  913. $this->_db->query();
  914. return true;
  915. }
  916. /**
  917. * Method to build the query
  918. *
  919. * @access protected
  920. * @subpackage Yireo
  921. * @param string $query
  922. * @return string
  923. */
  924. protected function buildQuery($query = '')
  925. {
  926. // Get the WHERE clauses for the query
  927. $where = $this->buildQueryWhere();
  928. // Get the ORDER BY clauses for the query
  929. $orderby = ($this->isSingular()) ? null : $this->buildQueryOrderBy();
  930. // Get the GROUP BY clauses for the query
  931. $groupby = $this->buildQueryGroupBy();
  932. // Get the extra segments for the query
  933. $extra = $this->buildQueryExtra();
  934. // Get the LIMIT segments for the query
  935. $limit = null;
  936. if ($this->_limit_query == true) {
  937. $limit = ' LIMIT '.$this->getState('limitstart').','.($this->getState('limit'));
  938. }
  939. // Build the default query if not set
  940. if (empty($query)) {
  941. // Skip certain fields in frontend
  942. $skipFrontendFields = array(
  943. 'locked', 'published', 'published_up', 'published_down', 'checked_out', 'checked_out_time'
  944. );
  945. // Build the fields-string to avoid a *
  946. $fields = $this->_tbl->getDatabaseFields();
  947. $fieldsStrings = array();
  948. foreach($fields as $field) {
  949. if($this->application->isSite() && in_array($field, $skipFrontendFields)) continue;
  950. $fieldsStrings[] = '`{tableAlias}`.`'.$field.'`';
  951. }
  952. $fieldsString = implode(',', $fieldsStrings);
  953. // Frontend or backend query
  954. if ($this->_checkout == true && $this->application->isAdmin()) {
  955. $query = "SELECT ".$fieldsString.", `editor`.`name` AS `editor` FROM `{table}` AS `{tableAlias}`\n";
  956. $query .= " LEFT JOIN `#__users` AS `editor` ON `{tableAlias}`.`checked_out` = `editor`.`id`\n";
  957. } else {
  958. $query = "SELECT ".$fieldsString." FROM `{table}` AS `{tableAlias}`\n";
  959. }
  960. }
  961. // Add-in access-details
  962. if (strstr($query, '{access}')) {
  963. $query = str_replace('{access}', '`viewlevel`.`title` AS `accesslevel`', $query);
  964. $query .= " LEFT JOIN `#__viewlevels` AS `viewlevel` ON `viewlevel`.`id`=`".$this->_tbl_alias."`.`access`\n";
  965. }
  966. // Add-in editor-details
  967. if (strstr($query, '{editor}')) {
  968. $query = str_replace('{editor}', '`user`.`name` AS `editor`', $query);
  969. $query .= " LEFT JOIN `#__users` AS `user` ON `user`.`id`=`".$this->_tbl_alias."`.`checked_out`\n";
  970. }
  971. // Return the query including WHERE and ORDER BY and LIMIT
  972. $query = $query . $extra . $where . $groupby . $orderby . $limit;
  973. $query = str_replace( '{table}', $this->_tbl_name, $query );
  974. $query = str_replace( '{tableAlias}', $this->_tbl_alias, $query );
  975. $query = str_replace( '{primary}', $this->_tbl_key, $query );
  976. return $query ;
  977. }
  978. /**
  979. * Method to build the query ORDER BY segment
  980. *
  981. * @access protected
  982. * @subpackage Yireo
  983. * @param null
  984. * @return string
  985. */
  986. protected function buildQueryOrderBy()
  987. {
  988. $this->_orderby = array_unique($this->_orderby);
  989. if (count($this->_orderby)) {
  990. foreach ($this->_orderby as $index => $orderby) {
  991. $orderby = trim($orderby);
  992. if (empty($orderby)) unset($this->_orderby[$index]);
  993. }
  994. if (!empty($this->_orderby)) return ' ORDER BY '. implode( ', ', $this->_orderby ) ."\n" ;
  995. }
  996. return null;
  997. }
  998. /**
  999. * Method to build the query GROUP BY segment
  1000. *
  1001. * @access protected
  1002. * @subpackage Yireo
  1003. * @param null
  1004. * @return string
  1005. */
  1006. protected function buildQueryGroupBy()
  1007. {
  1008. $this->_groupby = array_unique($this->_groupby);
  1009. if (count($this->_groupby)) {
  1010. foreach ($this->_groupby as $index => $groupby) {
  1011. $groupby = trim($groupby);
  1012. if (empty($groupby)) unset($this->_groupby[$index]);
  1013. }
  1014. if (!empty($this->_groupby)) return ' GROUP BY '. implode( ', ', $this->_groupby ) ."\n" ;
  1015. }
  1016. return null;
  1017. }
  1018. /**
  1019. * Method to build the query WHERE segment
  1020. *
  1021. * @access protected
  1022. * @subpackage Yireo
  1023. * @param null
  1024. * @return string
  1025. */
  1026. protected function buildQueryWhere()
  1027. {
  1028. // Automatically add the WHERE-statement for a single ID-based query
  1029. if ($this->isSingular()) {
  1030. $this->addWhere('`{tableAlias}`.`{primary}`='.(int)$this->_id);
  1031. }
  1032. // Automatically add a WHERE-statement if the state-filter is used
  1033. $state = $this->getFilter('state');
  1034. if ($state == 'U' || $state == 'P') {
  1035. $state = ($state == 'U') ? 0 : 1;
  1036. $stateField = $this->_tbl->getStateField();
  1037. if (!empty($stateField)) $this->addWhere('`'.$this->_tbl_alias.'`.`'.$stateField.'` = '.$state);
  1038. }
  1039. // Automatically add a WHERE-statement if only published items should appear on the frontend
  1040. if ($this->application->isSite()) {
  1041. $stateField = $this->_tbl->getStateField();
  1042. if (!empty($stateField)) $this->addWhere($this->_tbl_alias.'.'.$stateField.' = 1');
  1043. }
  1044. // Automatically add a WHERE-statement if the search-filter is used
  1045. $search = $this->getFilter('search');
  1046. if (!empty($this->_search) && !empty($search)) {
  1047. $where_search = array();
  1048. foreach ($this->_search as $column) {
  1049. if(strstr($column, '.') == false && strstr($column, '`') == false) $column = "`".$column."`";
  1050. if(strstr($column, '.') == false) $column = "`".$this->_tbl_alias."`.".$column;
  1051. $where_search[] = "$column LIKE '%$search%'";
  1052. }
  1053. }
  1054. if (!empty($where_search)) {
  1055. $this->_where[] = '('.implode(' OR ', $where_search).')';
  1056. }
  1057. if ( count( $this->_where ) ) {
  1058. return ' WHERE '. implode( ' AND ', $this->_where ) ."\n" ;
  1059. } else {
  1060. return '';
  1061. }
  1062. }
  1063. /**
  1064. * Method to build an extra query segment
  1065. *
  1066. * @access protected
  1067. * @subpackage Yireo
  1068. * @param null
  1069. * @return string
  1070. */
  1071. protected function buildQueryExtra()
  1072. {
  1073. if ( count( $this->_extra ) > 0 ) {
  1074. return ' '. implode( ' ', $this->_extra ) ."\n" ;
  1075. } else {
  1076. return '';
  1077. }
  1078. }
  1079. /**
  1080. * Method to add a new ORDER BY argument
  1081. *
  1082. * @access protected
  1083. * @subpackage Yireo
  1084. * @param string $orderby
  1085. * @return null
  1086. */
  1087. public function addOrderby($orderby = null)
  1088. {
  1089. $orderby = trim($orderby);
  1090. if (empty($orderby)) return;
  1091. if ($orderby == '{tableAlias}.') return;
  1092. if (is_string($orderby) && !isset($this->_orderby[$orderby])) {
  1093. if (strstr($orderby, '.') == false && preg_match('/^RAND/', $orderby) == false) $orderby = '{tableAlias}.'.$orderby;
  1094. if (strstr($orderby, 'accesslevel')) $orderby = str_replace('{tableAlias}.', '', $orderby);
  1095. $this->_orderby[] = $orderby;
  1096. }
  1097. }
  1098. /**
  1099. * Method to add a new GROUP BY argument
  1100. *
  1101. * @access protected
  1102. * @subpackage Yireo
  1103. * @param string $groupby
  1104. * @return null
  1105. */
  1106. public function addGroupby($groupby = null)
  1107. {
  1108. $groupby = trim($groupby);
  1109. if (empty($groupby)) return;
  1110. if ($groupby == '{tableAlias}.') return;
  1111. if (is_string($groupby) && !isset($this->_groupby[$groupby])) {
  1112. if (strstr($groupby, '.') == false) $groupby = '{tableAlias}.'.$groupby;
  1113. $this->_groupby[] = $groupby;
  1114. }
  1115. }
  1116. /**
  1117. * Method to add a new WHERE argument
  1118. *
  1119. * @access protected
  1120. * @subpackage Yireo
  1121. * @param string $where
  1122. * @return null
  1123. */
  1124. public function addWhere($where = null)
  1125. {
  1126. if ($this->_allow_filter == false) return null;
  1127. if (is_string($where) && !in_array($where, $this->_where)) {
  1128. $this->_where[] = $where;
  1129. }
  1130. }
  1131. /**
  1132. * Method to add an extra query argument
  1133. *
  1134. * @access protected
  1135. * @subpackage Yireo
  1136. * @param string $extra
  1137. * @return null
  1138. */
  1139. public function addExtra($extra = null)
  1140. {
  1141. if (is_string($extra)) {
  1142. $this->_extra[] = $extra;
  1143. }
  1144. }
  1145. /**
  1146. * Method to get the current primary key
  1147. *
  1148. * @access public
  1149. * @subpackage Yireo
  1150. * @param null
  1151. * @return string
  1152. */
  1153. public function getPrimaryKey()
  1154. {
  1155. return $this->_tbl_key;
  1156. }
  1157. /**
  1158. * Method to get the ordering query
  1159. *
  1160. * @access public
  1161. * @subpackage Yireo
  1162. * @param null
  1163. * @return string
  1164. */
  1165. public function getOrderingQuery()
  1166. {
  1167. if ($this->_orderby_default == 'ordering') {
  1168. $query = 'SELECT `ordering` AS `value`, `'.$this->_orderby_title.'` AS `text`'
  1169. . ' FROM `'.$this->_tbl_name.'`'
  1170. . ' ORDER BY `ordering`';
  1171. return $query;
  1172. } else if ($this->_orderby_default == 'lft') {
  1173. $query = 'SELECT `lft` AS `value`, `'.$this->_orderby_title.'` AS `text`'
  1174. . ' FROM `'.$this->_tbl_name.'`'
  1175. . ' ORDER BY `lft`';
  1176. return $query;
  1177. }
  1178. return null;
  1179. }
  1180. /**
  1181. * Method to get empty fields
  1182. *
  1183. * @access protected
  1184. * @subpackage Yireo
  1185. * @param null
  1186. * @return array
  1187. */
  1188. protected function getEmptyFields()
  1189. {
  1190. $data = array(
  1191. 'published' => 1,
  1192. 'publish_up' => null,
  1193. 'publish_down' => null,
  1194. 'state' => 1,
  1195. 'access' => 1,
  1196. 'ordering' => 0,
  1197. 'lft' => 0,
  1198. 'rgt' => 0,
  1199. );
  1200. return $data;
  1201. }
  1202. /**
  1203. * Method to initialise the data
  1204. *
  1205. * @access protected
  1206. * @subpackage Yireo
  1207. * @param null
  1208. * @return bool
  1209. */
  1210. protected function getEmpty()
  1211. {
  1212. // Define the fields to initialize
  1213. $data = $this->getEmptyFields();
  1214. // Lets load the data if it doesn't already exist
  1215. if (empty($this->_data)) {
  1216. if ($this->isPlural()) {
  1217. $this->_data = array();
  1218. } else {
  1219. $this->_data = (object)$this->_tbl->getProperties();
  1220. foreach ($data as $name => $value) {
  1221. $this->_data->$name = $value;
  1222. }
  1223. }
  1224. return true;
  1225. }
  1226. return false;
  1227. }
  1228. /**
  1229. * Method to get a XML-based form
  1230. *
  1231. * @access public
  1232. * @subpackage Yireo
  1233. * @param array $data
  1234. * @param bool $loadData
  1235. * @return mixed
  1236. */
  1237. public function getForm($data = array(), $loadData = true)
  1238. {
  1239. // Do not continue if this is not the right backend
  1240. if ($this->application->isAdmin() == false && $this->_frontend_form == false) {
  1241. return false;
  1242. }
  1243. // Do not continue if this is not a singular view
  1244. if ($this->isSingular() == false) {
  1245. return false;
  1246. }
  1247. // Read the form from XML
  1248. $xmlFile = JPATH_ADMINISTRATOR.'/components/'.$this->_option.'/models/'.$this->_entity.'.xml';
  1249. if (!file_exists($xmlFile)) {
  1250. $xmlFile = JPATH_SITE.'/components/'.$this->_option.'/models/'.$this->_entity.'.xml';
  1251. }
  1252. if (!file_exists($xmlFile)) {
  1253. return false;
  1254. }
  1255. // Construct the form-object
  1256. jimport('joomla.form.form');
  1257. $form = JForm::getInstance('item', $xmlFile);
  1258. if (empty($form)) {
  1259. return false;
  1260. }
  1261. // Bind the data
  1262. $data = $this->getData();
  1263. $form->bind(array('item' => $data));
  1264. // Insert the params-data if set
  1265. if (!empty($data->params)) {
  1266. $params = $data->params;
  1267. if (is_string($params)) $params = YireoHelper::toRegistry($params)->toArray();
  1268. $form->bind(array('params' => $params));
  1269. }
  1270. return $form;
  1271. }
  1272. /**
  1273. * Check whether this record can be edited
  1274. *
  1275. * @access protected
  1276. * @subpackage Yireo
  1277. * @param array $data
  1278. * @return bool
  1279. */
  1280. protected function canEditState($data)
  1281. {
  1282. // Check the permissions for this edit.state action
  1283. if ($this->getId() > 0) {
  1284. return $this->user->authorise('core.edit.state', $this->_option.'.'.$this->_entity.'.'.(int)$this->getId());
  1285. } else {
  1286. return $this->user->authorise('core.edit.state', $this->_option);
  1287. }
  1288. }
  1289. /**
  1290. * Method to determine whether this model is singular or not
  1291. *
  1292. * @access public
  1293. * @subpackage Yireo
  1294. * @param null
  1295. * @return bool
  1296. */
  1297. public function isSingular()
  1298. {
  1299. if (isset($this->_single) && (bool)$this->_single == true) {
  1300. return true;
  1301. }
  1302. return false;
  1303. }
  1304. /**
  1305. * Method to determine whether this model is plural or not
  1306. *
  1307. * @access public
  1308. * @subpackage Yireo
  1309. * @param null
  1310. * @return bool
  1311. */
  1312. public function isPlural()
  1313. {
  1314. if ($this->isSingular()) {
  1315. return false;
  1316. }
  1317. return true;
  1318. }
  1319. /**
  1320. * Method to override the parameters
  1321. *
  1322. * @access public
  1323. * @subpackage Yireo
  1324. * @param mixed
  1325. * @return null
  1326. */
  1327. public function setParams($params = null)
  1328. {
  1329. if (!empty($params)) {
  1330. $this->params = $params;
  1331. }
  1332. }
  1333. /**
  1334. * Method to get the default ORDER BY value
  1335. *
  1336. * @access public
  1337. * @subpackage Yireo
  1338. * @param null
  1339. * @return string
  1340. */
  1341. public function getOrderByDefault()
  1342. {
  1343. return $this->_orderby_default;
  1344. }
  1345. /**
  1346. * Method to get a debug-message of the latest query
  1347. *
  1348. * @access protected
  1349. * @subpackage Yireo
  1350. * @param null
  1351. * @return string
  1352. */
  1353. public function getDbDebug()
  1354. {
  1355. return '<pre>'.str_replace('#__', $this->_db->getPrefix(), $this->_db->getQuery()).'</pre>';
  1356. }
  1357. /**
  1358. * Method to temporarily store an object in the current session
  1359. *
  1360. * @access public
  1361. * @subpackage Yireo
  1362. * @param null
  1363. * @return string
  1364. */
  1365. public function saveTmpSession($data)
  1366. {
  1367. $session = JFactory::getSession();
  1368. $session->set($this->_option_id, $data);
  1369. }
  1370. /**
  1371. * Method to temporarily store an object in the current session
  1372. *
  1373. * @access public
  1374. * @subpackage Yireo
  1375. * @param null
  1376. * @return string
  1377. */
  1378. public function loadTmpSession()
  1379. {
  1380. if ($this->isSingular()) {
  1381. $session = JFactory::getSession();
  1382. $data = $session->get($this->_option_id);
  1383. if (!empty($data)) {
  1384. foreach ($data as $name => $value) {
  1385. if (!empty($value)) {
  1386. $this->_data->$name = $value;
  1387. }
  1388. }
  1389. }
  1390. }
  1391. return;
  1392. }
  1393. /**
  1394. * Method to temporarily store an object in the current session
  1395. *
  1396. * @access public
  1397. * @subpackage Yireo
  1398. * @param null
  1399. * @return string
  1400. */
  1401. public function resetTmpSession()
  1402. {
  1403. $session = JFactory::getSession();
  1404. $session->clear($this->_option_id);
  1405. }
  1406. /**
  1407. * Method to reset all filters
  1408. *
  1409. * @access public
  1410. * @subpackage Yireo
  1411. * @param null
  1412. * @return string
  1413. */
  1414. public function resetFilters()
  1415. {
  1416. $this->_search = null;
  1417. $this->_where = array();
  1418. $this->_orderby = array();
  1419. $this->setState('limitstart', 0);
  1420. $this->setState('limit', 0);
  1421. }
  1422. /**
  1423. * Method to check if any errors are set
  1424. *
  1425. * @access public
  1426. * @subpackage Yireo
  1427. * @param null
  1428. * @return boolean
  1429. */
  1430. public function hasErrors()
  1431. {
  1432. $errors = $this->getErrors();
  1433. if (!empty($errors)) {
  1434. return true;
  1435. }
  1436. return false;
  1437. }
  1438. /**
  1439. * Method to set whether filtering is allowed
  1440. *
  1441. * @access public
  1442. * @subpackage Yireo
  1443. * @param boolean
  1444. * @return null
  1445. */
  1446. public function setAllowFilter($bool)
  1447. {
  1448. $this->_allow_filter = $bool;
  1449. }
  1450. /**
  1451. * Method to set whether the query should use LIMIT or not
  1452. *
  1453. * @access public
  1454. * @subpackage Yireo
  1455. * @param boolean
  1456. * @return null
  1457. */
  1458. public function setLimitQuery($bool)
  1459. {
  1460. $this->_limit_query = $bool;
  1461. }
  1462. /**
  1463. * Method to fetch database-results
  1464. *
  1465. * @access public
  1466. * @subpackage Yireo
  1467. * @param string $query
  1468. * @param string $type: object|objectList|result
  1469. * @return null
  1470. */
  1471. public function getDbResult($query, $type = 'object')
  1472. {
  1473. if($this->_cache == true) {
  1474. $cache = JFactory::getCache('lib_yireo_model');
  1475. $rs = $cache->call(array($this, '_getDbResult'), $query, $type);
  1476. } else {
  1477. $rs = $this->_getDbResult($query, $type);
  1478. }
  1479. return $rs;
  1480. }
  1481. /**
  1482. * Method to fetch database-results
  1483. *
  1484. * @access public
  1485. * @subpackage Yireo
  1486. * @param string $query
  1487. * @param string $type: object|objectList|result
  1488. * @return null
  1489. */
  1490. public function _getDbResult($query, $type = 'object')
  1491. {
  1492. // Set the query in the database-object
  1493. $this->_db->setQuery($query);
  1494. // Print the query if debugging is enabled
  1495. if (isset($this->_debug) && $this->_debug == true) {
  1496. JError::raiseNotice( 'Query', $this->getDbDebug());
  1497. }
  1498. // Fetch the database-result
  1499. if($type == 'objectList') {
  1500. $rs = $this->_db->loadObjectList();
  1501. } elseif($type == 'result') {
  1502. $rs = $this->_db->loadResult();
  1503. } else {
  1504. $rs = $this->_db->loadObject();
  1505. }
  1506. // Print an error when an error occurs
  1507. if (isset($this->_debug) && $this->_debug == true && $this->_db->getErrorMsg()) {
  1508. JError::raiseWarning( 'DB error', $this->_db->getErrorMsg());
  1509. }
  1510. // Return the result
  1511. return $rs;
  1512. }
  1513. /**
  1514. * Method to return the meta-data of this model
  1515. *
  1516. * @access protected
  1517. * @subpackage Yireo
  1518. * @param null
  1519. * @return array
  1520. */
  1521. protected function getMetadata()
  1522. {
  1523. return array(
  1524. 'table' => $this->_entity,
  1525. );
  1526. }
  1527. /**
  1528. * Method to determine the component-name
  1529. *
  1530. * @access protected
  1531. * @subpackage Yireo
  1532. * @param null
  1533. * @return string

Large files files are truncated, but you can click here to view the full file