PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_contact/models/contact.php

https://github.com/3den/J-MediaGalleries
PHP | 343 lines | 172 code | 46 blank | 125 comment | 28 complexity | 97b7917a324b69fd31f38413bef92548 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: contact.php 19024 2010-10-02 18:53:34Z 3dentech $
  4. * @package Joomla.Administrator
  5. * @subpackage com_contact
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // No direct access
  10. defined('_JEXEC') or die;
  11. jimport('joomla.application.component.modeladmin');
  12. /**
  13. * Item Model for Contacts.
  14. *
  15. * @package Joomla.Administrator
  16. * @subpackage com_contact
  17. * @since 1.6
  18. */
  19. class ContactModelContact extends JModelAdmin
  20. {
  21. /**
  22. * Method to test whether a record can be deleted.
  23. *
  24. * @param object $record A record object.
  25. *
  26. * @return boolean True if allowed to delete the record. Defaults to the permission set in the component.
  27. * @since 1.6
  28. */
  29. protected function canDelete($record)
  30. {
  31. $user = JFactory::getUser();
  32. if ($record->catid) {
  33. return $user->authorise('core.delete', 'com_contact.category.'.(int) $record->catid);
  34. }
  35. else {
  36. return parent::canDelete($record);
  37. }
  38. }
  39. /**
  40. * Method to test whether a record can be deleted.
  41. *
  42. * @param object $record A record object.
  43. *
  44. * @return boolean True if allowed to change the state of the record. Defaults to the permission set in the component.
  45. * @since 1.6
  46. */
  47. protected function canEditState($record)
  48. {
  49. $user = JFactory::getUser();
  50. // Check against the category.
  51. if (!empty($record->catid)) {
  52. return $user->authorise('core.edit.state', 'com_contact.category.'.(int) $record->catid);
  53. }
  54. // Default to component settings if category not known.
  55. else {
  56. return parent::canEditState($record);
  57. }
  58. }
  59. /**
  60. * Returns a Table object, always creating it
  61. *
  62. * @param type $type The table type to instantiate
  63. * @param string $prefix A prefix for the table class name. Optional.
  64. * @param array $config Configuration array for model. Optional.
  65. *
  66. * @return JTable A database object
  67. * @since 1.6
  68. */
  69. public function getTable($type = 'Contact', $prefix = 'ContactTable', $config = array())
  70. {
  71. return JTable::getInstance($type, $prefix, $config);
  72. }
  73. /**
  74. * Method to get the row form.
  75. *
  76. * @param array $data Data for the form.
  77. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  78. *
  79. * @return mixed A JForm object on success, false on failure
  80. * @since 1.6
  81. */
  82. public function getForm($data = array(), $loadData = true)
  83. {
  84. jimport('joomla.form.form');
  85. JForm::addFieldPath('JPATH_ADMINISTRATOR/components/com_users/models/fields');
  86. // Get the form.
  87. $form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => $loadData));
  88. if (empty($form)) {
  89. return false;
  90. }
  91. // Modify the form based on access controls.
  92. if (!$this->canEditState((object) $data)) {
  93. // Disable fields for display.
  94. $form->setFieldAttribute('featured', 'disabled', 'true');
  95. $form->setFieldAttribute('ordering', 'disabled', 'true');
  96. $form->setFieldAttribute('published', 'disabled', 'true');
  97. // Disable fields while saving.
  98. // The controller has already verified this is a record you can edit.
  99. $form->setFieldAttribute('featured', 'filter', 'unset');
  100. $form->setFieldAttribute('ordering', 'filter', 'unset');
  101. $form->setFieldAttribute('published', 'filter', 'unset');
  102. }
  103. return $form;
  104. }
  105. /**
  106. * Method to get a single record.
  107. *
  108. * @param integer $pk The id of the primary key.
  109. *
  110. * @return mixed Object on success, false on failure.
  111. * @since 1.6
  112. */
  113. public function getItem($pk = null)
  114. {
  115. if ($item = parent::getItem($pk)) {
  116. // Convert the params field to an array.
  117. $registry = new JRegistry;
  118. $registry->loadJSON($item->metadata);
  119. $item->metadata = $registry->toArray();
  120. }
  121. return $item;
  122. }
  123. /**
  124. * Method to get the data that should be injected in the form.
  125. *
  126. * @return mixed The data for the form.
  127. * @since 1.6
  128. */
  129. protected function loadFormData()
  130. {
  131. // Check the session for previously entered form data.
  132. $data = JFactory::getApplication()->getUserState('com_contact.edit.contact.data', array());
  133. if (empty($data)) {
  134. $data = $this->getItem();
  135. }
  136. return $data;
  137. }
  138. /**
  139. * Method to perform batch operations on a category or a set of contacts.
  140. *
  141. * @param array $commands An array of commands to perform.
  142. * @param array $pks An array of category ids.
  143. *
  144. * @return boolean Returns true on success, false on failure.
  145. * @since 1.6
  146. */
  147. function batch($commands, $pks)
  148. {
  149. // Sanitize user ids.
  150. $pks = array_unique($pks);
  151. JArrayHelper::toInteger($pks);
  152. // Remove any values of zero.
  153. if (array_search(0, $pks, true)) {
  154. unset($pks[array_search(0, $pks, true)]);
  155. }
  156. if (empty($pks)) {
  157. $this->setError(JText::_('COM_CONTACT_NO_ITEM_SELECTED'));
  158. return false;
  159. }
  160. $done = false;
  161. if (!empty($commands['assetgroup_id'])) {
  162. if (!$this->_batchAccess($commands['assetgroup_id'], $pks)) {
  163. return false;
  164. }
  165. $done = true;
  166. }
  167. if (!empty($commands['menu_id'])) {
  168. $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
  169. if ($cmd == 'c' && !$this->_batchCopy($commands['menu_id'], $pks)) {
  170. return false;
  171. }
  172. else if ($cmd == 'm' && !$this->_batchMove($commands['menu_id'], $pks)) {
  173. return false;
  174. }
  175. $done = true;
  176. }
  177. if (!$done) {
  178. $this->setError('COM_MENUS_ERROR_INSUFFICIENT_BATCH_INFORMATION');
  179. return false;
  180. }
  181. return true;
  182. }
  183. /**
  184. * Batch access level changes for a group of rows.
  185. *
  186. * @param int $value The new value matching an Asset Group ID.
  187. * @param array $pks An array of row IDs.
  188. *
  189. * @return booelan True if successful, false otherwise and internal error is set.
  190. * @since 1.6
  191. */
  192. protected function _batchAccess($value, $pks)
  193. {
  194. $table = $this->getTable();
  195. foreach ($pks as $pk)
  196. {
  197. $table->reset();
  198. $table->load($pk);
  199. $table->access = (int) $value;
  200. if (!$table->store()) {
  201. $this->setError($table->getError());
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. /**
  208. * Prepare and sanitise the table prior to saving.
  209. *
  210. * @param JTable $table
  211. *
  212. * @return void
  213. * @since 1.6
  214. */
  215. protected function prepareTable(&$table)
  216. {
  217. jimport('joomla.filter.output');
  218. $date = JFactory::getDate();
  219. $user = JFactory::getUser();
  220. $table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
  221. $table->alias = JApplication::stringURLSafe($table->alias);
  222. if (empty($table->alias)) {
  223. $table->alias = JApplication::stringURLSafe($table->name);
  224. }
  225. if (empty($table->id)) {
  226. // Set the values
  227. //$table->created = $date->toMySQL();
  228. // Set ordering to the last item if not set
  229. if (empty($table->ordering)) {
  230. $db = JFactory::getDbo();
  231. $db->setQuery('SELECT MAX(ordering) FROM #__contact_details');
  232. $max = $db->loadResult();
  233. $table->ordering = $max+1;
  234. }
  235. }
  236. else {
  237. // Set the values
  238. //$table->modified = $date->toMySQL();
  239. //$table->modified_by = $user->get('id');
  240. }
  241. }
  242. /**
  243. * A protected method to get a set of ordering conditions.
  244. *
  245. * @param JTable $table A record object.
  246. *
  247. * @return array An array of conditions to add to add to ordering queries.
  248. * @since 1.6
  249. */
  250. protected function getReorderConditions($table = null)
  251. {
  252. $condition = array();
  253. $condition[] = 'catid = '.(int) $table->catid;
  254. return $condition;
  255. }
  256. /**
  257. * Method to toggle the featured setting of contacts.
  258. *
  259. * @param array $pks The ids of the items to toggle.
  260. * @param int $value The value to toggle to.
  261. *
  262. * @return boolean True on success.
  263. * @since 1.6
  264. */
  265. public function featured($pks, $value = 0)
  266. {
  267. // Sanitize the ids.
  268. $pks = (array) $pks;
  269. JArrayHelper::toInteger($pks);
  270. if (empty($pks)) {
  271. $this->setError(JText::_('COM_CONTACT_NO_ITEM_SELECTED'));
  272. return false;
  273. }
  274. $table = $this->getTable();
  275. try
  276. {
  277. $db = $this->getDbo();
  278. $db->setQuery(
  279. 'UPDATE #__contact_details AS a' .
  280. ' SET a.featured = '.(int) $value.
  281. ' WHERE a.id IN ('.implode(',', $pks).')'
  282. );
  283. if (!$db->query()) {
  284. throw new Exception($db->getErrorMsg());
  285. }
  286. }
  287. catch (Exception $e)
  288. {
  289. $this->setError($e->getMessage());
  290. return false;
  291. }
  292. $table->reorder();
  293. $cache = JFactory::getCache('com_contact');
  294. $cache->clean();
  295. return true;
  296. }
  297. }