PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_contact/models/contact.php

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