PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_fbnews/tables/facebooknewsitem.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 208 lines | 109 code | 28 blank | 71 comment | 29 complexity | 3c1bfcea9697dcd1b833828e7fd667cb MD5 | raw file
  1. <?php
  2. /**
  3. * @version 1.0.0
  4. * @package com_fbnews
  5. * @copyright Copyright (C) 2013. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. * @author Andrew Pastor <pastor399@gmail.com> - www.part-one.net
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. /**
  12. * facebooknewsitem Table class
  13. */
  14. class FbnewsTablefacebooknewsitem extends JTable {
  15. /**
  16. * Constructor
  17. *
  18. * @param JDatabase A database connector object
  19. */
  20. public function __construct(&$db) {
  21. parent::__construct('#__fbnews', 'id', $db);
  22. }
  23. /**
  24. * Overloaded bind function to pre-process the params.
  25. *
  26. * @param array Named array
  27. * @return null|string null is operation was satisfactory, otherwise returns an error
  28. * @see JTable:bind
  29. * @since 1.5
  30. */
  31. public function bind($array, $ignore = '') {
  32. $input = JFactory::getApplication()->input;
  33. $task = $input->getString('task', '');
  34. if(($task == 'save' || $task == 'apply') && (!JFactory::getUser()->authorise('core.edit.state','com_fbnews') && $array['state'] == 1)){
  35. $array['state'] = 0;
  36. }
  37. if (isset($array['params']) && is_array($array['params'])) {
  38. $registry = new JRegistry();
  39. $registry->loadArray($array['params']);
  40. $array['params'] = (string) $registry;
  41. }
  42. if (isset($array['metadata']) && is_array($array['metadata'])) {
  43. $registry = new JRegistry();
  44. $registry->loadArray($array['metadata']);
  45. $array['metadata'] = (string) $registry;
  46. }
  47. if(!JFactory::getUser()->authorise('core.admin', 'com_fbnews.facebooknewsitem.'.$array['id'])){
  48. $actions = JFactory::getACL()->getActions('com_fbnews','facebooknewsitem');
  49. $default_actions = JFactory::getACL()->getAssetRules('com_fbnews.facebooknewsitem.'.$array['id'])->getData();
  50. $array_jaccess = array();
  51. foreach($actions as $action){
  52. $array_jaccess[$action->name] = $default_actions[$action->name];
  53. }
  54. $array['rules'] = $this->JAccessRulestoArray($array_jaccess);
  55. }
  56. //Bind the rules for ACL where supported.
  57. if (isset($array['rules']) && is_array($array['rules'])) {
  58. $this->setRules($array['rules']);
  59. }
  60. return parent::bind($array, $ignore);
  61. }
  62. /**
  63. * This function convert an array of JAccessRule objects into an rules array.
  64. * @param type $jaccessrules an arrao of JAccessRule objects.
  65. */
  66. private function JAccessRulestoArray($jaccessrules){
  67. $rules = array();
  68. foreach($jaccessrules as $action => $jaccess){
  69. $actions = array();
  70. foreach($jaccess->getData() as $group => $allow){
  71. $actions[$group] = ((bool)$allow);
  72. }
  73. $rules[$action] = $actions;
  74. }
  75. return $rules;
  76. }
  77. /**
  78. * Overloaded check function
  79. */
  80. public function check() {
  81. //If there is an ordering column and this is a new row then get the next ordering value
  82. if (property_exists($this, 'ordering') && $this->id == 0) {
  83. $this->ordering = self::getNextOrder();
  84. }
  85. return parent::check();
  86. }
  87. /**
  88. * Method to set the publishing state for a row or list of rows in the database
  89. * table. The method respects checked out rows by other users and will attempt
  90. * to checkin rows that it can after adjustments are made.
  91. *
  92. * @param mixed An optional array of primary key values to update. If not
  93. * set the instance property value is used.
  94. * @param integer The publishing state. eg. [0 = unpublished, 1 = published]
  95. * @param integer The user id of the user performing the operation.
  96. * @return boolean True on success.
  97. * @since 1.0.4
  98. */
  99. public function publish($pks = null, $state = 1, $userId = 0) {
  100. // Initialise variables.
  101. $k = $this->_tbl_key;
  102. // Sanitize input.
  103. JArrayHelper::toInteger($pks);
  104. $userId = (int) $userId;
  105. $state = (int) $state;
  106. // If there are no primary keys set check to see if the instance key is set.
  107. if (empty($pks)) {
  108. if ($this->$k) {
  109. $pks = array($this->$k);
  110. }
  111. // Nothing to set publishing state on, return false.
  112. else {
  113. $this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
  114. return false;
  115. }
  116. }
  117. // Build the WHERE clause for the primary keys.
  118. $where = $k . '=' . implode(' OR ' . $k . '=', $pks);
  119. // Determine if there is checkin support for the table.
  120. if (!property_exists($this, 'checked_out') || !property_exists($this, 'checked_out_time')) {
  121. $checkin = '';
  122. } else {
  123. $checkin = ' AND (checked_out = 0 OR checked_out = ' . (int) $userId . ')';
  124. }
  125. // Update the publishing state for rows with the given primary keys.
  126. $this->_db->setQuery(
  127. 'UPDATE `' . $this->_tbl . '`' .
  128. ' SET `state` = ' . (int) $state .
  129. ' WHERE (' . $where . ')' .
  130. $checkin
  131. );
  132. $this->_db->query();
  133. // Check for a database error.
  134. if ($this->_db->getErrorNum()) {
  135. $this->setError($this->_db->getErrorMsg());
  136. return false;
  137. }
  138. // If checkin is supported and all rows were adjusted, check them in.
  139. if ($checkin && (count($pks) == $this->_db->getAffectedRows())) {
  140. // Checkin each row.
  141. foreach ($pks as $pk) {
  142. $this->checkin($pk);
  143. }
  144. }
  145. // If the JTable instance value is in the list of primary keys that were set, set the instance.
  146. if (in_array($this->$k, $pks)) {
  147. $this->state = $state;
  148. }
  149. $this->setError('');
  150. return true;
  151. }
  152. /**
  153. * Define a namespaced asset name for inclusion in the #__assets table
  154. * @return string The asset name
  155. *
  156. * @see JTable::_getAssetName
  157. */
  158. protected function _getAssetName() {
  159. $k = $this->_tbl_key;
  160. return 'com_fbnews.facebooknewsitem.' . (int) $this->$k;
  161. }
  162. /**
  163. * Returns the parrent asset's id. If you have a tree structure, retrieve the parent's id using the external key field
  164. *
  165. * @see JTable::_getAssetParentId
  166. */
  167. protected function _getAssetParentId($table = null, $id = null){
  168. // We will retrieve the parent-asset from the Asset-table
  169. $assetParent = JTable::getInstance('Asset');
  170. // Default: if no asset-parent can be found we take the global asset
  171. $assetParentId = $assetParent->getRootId();
  172. // The item has the component as asset-parent
  173. $assetParent->loadByName('com_fbnews');
  174. // Return the found asset-parent-id
  175. if ($assetParent->id){
  176. $assetParentId=$assetParent->id;
  177. }
  178. return $assetParentId;
  179. }
  180. }