PageRenderTime 68ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_contact/models/contact.php

https://github.com/rietn/minima
PHP | 312 lines | 194 code | 52 blank | 66 comment | 32 complexity | edd7b467c67bf87cf20e44cf3df80f47 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: contact.php 21097 2011-04-07 15:38:03Z dextercowley $
  4. * @package Joomla.Site
  5. * @subpackage com_contact
  6. * @copyright Copyright (C) 2005 - 2011 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.modelform');
  12. jimport('joomla.application.component.modelitem');
  13. jimport('joomla.event.dispatcher');
  14. jimport('joomla.plugin.helper');
  15. /**
  16. * @package Joomla.Site
  17. * @subpackage com_contact
  18. * @since 1.5
  19. */
  20. class ContactModelContact extends JModelForm
  21. {
  22. /**
  23. * @since 1.6
  24. */
  25. protected $view_item = 'contact';
  26. protected $_item = null;
  27. /**
  28. * Model context string.
  29. *
  30. * @var string
  31. */
  32. protected $_context = 'com_contact.contact';
  33. /**
  34. * Method to auto-populate the model state.
  35. *
  36. * Note. Calling getState in this method will result in recursion.
  37. *
  38. * @since 1.6
  39. */
  40. protected function populateState()
  41. {
  42. $app = JFactory::getApplication('site');
  43. // Load state from the request.
  44. $pk = JRequest::getInt('id');
  45. $this->setState('contact.id', $pk);
  46. // Load the parameters.
  47. $params = $app->getParams();
  48. $this->setState('params', $params);
  49. $user = JFactory::getUser();
  50. if ((!$user->authorise('core.edit.state', 'com_contact')) && (!$user->authorise('core.edit', 'com_contact'))){
  51. $this->setState('filter.published', 1);
  52. $this->setState('filter.archived', 2);
  53. }
  54. }
  55. /**
  56. * Method to get the contact form.
  57. *
  58. * The base form is loaded from XML and then an event is fired
  59. *
  60. *
  61. * @param array $data An optional array of data for the form to interrogate.
  62. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  63. * @return JForm A JForm object on success, false on failure
  64. * @since 1.6
  65. */
  66. public function getForm($data = array(), $loadData = true)
  67. {
  68. // Get the form.
  69. $form = $this->loadForm('com_contact.contact', 'contact', array('control' => 'jform', 'load_data' => true));
  70. if (empty($form)) {
  71. return false;
  72. }
  73. $id = $this->getState('contact.id');
  74. $params = $this->getState('params');
  75. $contact = $this->_item[$id];
  76. $params->merge($contact->params);
  77. if(!$params->get('show_email_copy', 0)){
  78. $form->removeField('contact_email_copy');
  79. }
  80. return $form;
  81. }
  82. protected function loadFormData()
  83. {
  84. $data = (array)JFactory::getApplication()->getUserState('com_contact.contact.data', array());
  85. return $data;
  86. }
  87. /**
  88. * Gets a list of contacts
  89. * @param array
  90. * @return mixed Object or null
  91. */
  92. public function &getItem($pk = null)
  93. {
  94. // Initialise variables.
  95. $pk = (!empty($pk)) ? $pk : (int) $this->getState('contact.id');
  96. if ($this->_item === null) {
  97. $this->_item = array();
  98. }
  99. if (!isset($this->_item[$pk])) {
  100. try
  101. {
  102. $db = $this->getDbo();
  103. $query = $db->getQuery(true);
  104. $query->select($this->getState('item.select', 'a.*') . ','
  105. . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
  106. . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END AS catslug ');
  107. $query->from('#__contact_details AS a');
  108. // Join on category table.
  109. $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access');
  110. $query->join('LEFT', '#__categories AS c on c.id = a.catid');
  111. // Join over the categories to get parent category titles
  112. $query->select('parent.title as parent_title, parent.id as parent_id, parent.path as parent_route, parent.alias as parent_alias');
  113. $query->join('LEFT', '#__categories as parent ON parent.id = c.parent_id');
  114. $query->where('a.id = ' . (int) $pk);
  115. // Filter by start and end dates.
  116. $nullDate = $db->Quote($db->getNullDate());
  117. $nowDate = $db->Quote(JFactory::getDate()->toMySQL());
  118. // Filter by published state.
  119. $published = $this->getState('filter.published');
  120. $archived = $this->getState('filter.archived');
  121. if (is_numeric($published)) {
  122. $query->where('(a.published = ' . (int) $published . ' OR a.published =' . (int) $archived . ')');
  123. $query->where('(a.publish_up = ' . $nullDate . ' OR a.publish_up <= ' . $nowDate . ')');
  124. $query->where('(a.publish_down = ' . $nullDate . ' OR a.publish_down >= ' . $nowDate . ')');
  125. }
  126. $db->setQuery($query);
  127. $data = $db->loadObject();
  128. if ($error = $db->getErrorMsg()) {
  129. throw new JException($error);
  130. }
  131. if (empty($data)) {
  132. throw new JException(JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
  133. }
  134. // Check for published state if filter set.
  135. if (((is_numeric($published)) || (is_numeric($archived))) && (($data->published != $published) && ($data->published != $archived)))
  136. {
  137. JError::raiseError(404, JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'));
  138. }
  139. // Convert parameter fields to objects.
  140. $registry = new JRegistry;
  141. $registry->loadJSON($data->params);
  142. $data->params = clone $this->getState('params');
  143. $data->params->merge($registry);
  144. $registry = new JRegistry;
  145. $registry->loadJSON($data->metadata);
  146. $data->metadata = $registry;
  147. // Compute access permissions.
  148. if ($access = $this->getState('filter.access')) {
  149. // If the access filter has been set, we already know this user can view.
  150. $data->params->set('access-view', true);
  151. }
  152. else {
  153. // If no access filter is set, the layout takes some responsibility for display of limited information.
  154. $user = JFactory::getUser();
  155. $groups = $user->getAuthorisedViewLevels();
  156. if ($data->catid == 0 || $data->category_access === null) {
  157. $data->params->set('access-view', in_array($data->access, $groups));
  158. }
  159. else {
  160. $data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
  161. }
  162. }
  163. $this->_item[$pk] = $data;
  164. }
  165. catch (JException $e)
  166. {
  167. $this->setError($e);
  168. $this->_item[$pk] = false;
  169. }
  170. }
  171. if ($this->_item[$pk])
  172. {
  173. if ($extendedData = $this->getContactQuery($pk)) {
  174. $this->_item[$pk]->articles = $extendedData->articles;
  175. $this->_item[$pk]->profile = $extendedData->profile;
  176. }
  177. }
  178. return $this->_item[$pk];
  179. }
  180. protected function getContactQuery($pk = null)
  181. {
  182. // TODO: Cache on the fingerprint of the arguments
  183. $db = $this->getDbo();
  184. $user = JFactory::getUser();
  185. $pk = (!empty($pk)) ? $pk : (int) $this->getState('contact.id');
  186. $query = $db->getQuery(true);
  187. if ($pk) {
  188. $query->select('a.*, cc.access as category_access, cc.title as category_name, '
  189. . ' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(\':\', a.id, a.alias) ELSE a.id END as slug, '
  190. . ' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(\':\', cc.id, cc.alias) ELSE cc.id END AS catslug ');
  191. $query->from('#__contact_details AS a');
  192. $query->join('INNER', '#__categories AS cc on cc.id = a.catid');
  193. $query->where('a.id = ' . (int) $pk);
  194. $published = $this->getState('filter.published');
  195. $archived = $this->getState('filter.archived');
  196. if (is_numeric($published)) {
  197. $query->where('a.published IN (1,2)');
  198. $query->where('cc.published IN (1,2)');
  199. }
  200. $groups = implode(',', $user->getAuthorisedViewLevels());
  201. $query->where('a.access IN ('.$groups.')');
  202. try {
  203. $db->setQuery($query);
  204. $result = $db->loadObject();
  205. if ($error = $db->getErrorMsg()) {
  206. throw new Exception($error);
  207. }
  208. if (empty($result)) {
  209. throw new JException(JText::_('COM_CONTACT_ERROR_CONTACT_NOT_FOUND'), 404);
  210. }
  211. // If we are showing a contact list, then the contact parameters take priority
  212. // So merge the contact parameters with the merged parameters
  213. if ($this->getState('params')->get('show_contact_list')) {
  214. $registry = new JRegistry;
  215. $registry->loadJSON($result->params);
  216. $this->getState('params')->merge($registry);
  217. }
  218. } catch (Exception $e) {
  219. $this->setError($e);
  220. return false;
  221. }
  222. if ($result) {
  223. $user = JFactory::getUser();
  224. $groups = implode(',', $user->getAuthorisedViewLevels());
  225. //get the content by the linked user
  226. $query = $db->getQuery(true);
  227. $query->select('id, title, state, access, created');
  228. $query->from('#__content');
  229. $query->where('created_by = '.(int)$result->user_id);
  230. $query->where('access IN ('. $groups.')');
  231. $query->order('state DESC, created DESC');
  232. if (is_numeric($published)) {
  233. $query->where('state IN (1,2)');
  234. }
  235. $db->setQuery($query, 0, 10);
  236. $articles = $db->loadObjectList();
  237. $result->articles = $articles;
  238. //get the profile information for the linked user
  239. if ($result) {
  240. require_once JPATH_ADMINISTRATOR.'/components/com_users/models/user.php';
  241. $userModel = JModel::getInstance('User','UsersModel',array('ignore_request' => true));
  242. $data = $userModel->getItem((int)$result->user_id);
  243. JPluginHelper::importPlugin('user');
  244. $form = new JForm('com_users.profile');
  245. // Get the dispatcher.
  246. $dispatcher = JDispatcher::getInstance();
  247. // Trigger the form preparation event.
  248. $dispatcher->trigger('onContentPrepareForm', array($form, $data));
  249. // Trigger the data preparation event.
  250. $dispatcher->trigger('onContentPrepareData', array('com_users.profile', $data));
  251. // Load the data into the form after the plugins have operated.
  252. $form->bind($data);
  253. $result->profile = $form;
  254. }
  255. $this->contact = $result;
  256. return $result;
  257. }
  258. }
  259. }
  260. }