/src/administrator/components/com_privacy/models/requests.php

https://bitbucket.org/ke2083/transfans.co.uk-website · PHP · 190 lines · 96 code · 25 blank · 69 comment · 6 complexity · 3f80dd11032847346c0d6a1ceccddb10 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_privacy
  5. *
  6. * @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. use Joomla\CMS\Component\ComponentHelper;
  11. /**
  12. * Requests management model class.
  13. *
  14. * @since 3.9.0
  15. */
  16. class PrivacyModelRequests extends JModelList
  17. {
  18. /**
  19. * Constructor.
  20. *
  21. * @param array $config An optional associative array of configuration settings.
  22. *
  23. * @since 3.9.0
  24. */
  25. public function __construct($config = array())
  26. {
  27. if (empty($config['filter_fields']))
  28. {
  29. $config['filter_fields'] = array(
  30. 'id', 'a.id',
  31. 'email', 'a.email',
  32. 'requested_at', 'a.requested_at',
  33. 'request_type', 'a.request_type',
  34. 'status', 'a.status',
  35. );
  36. }
  37. parent::__construct($config);
  38. }
  39. /**
  40. * Method to get a JDatabaseQuery object for retrieving the data set from a database.
  41. *
  42. * @return JDatabaseQuery
  43. *
  44. * @since 3.9.0
  45. */
  46. protected function getListQuery()
  47. {
  48. // Create a new query object.
  49. $db = $this->getDbo();
  50. $query = $db->getQuery(true);
  51. // Select the required fields from the table.
  52. $query->select($this->getState('list.select', 'a.*'));
  53. $query->from($db->quoteName('#__privacy_requests', 'a'));
  54. // Filter by status
  55. $status = $this->getState('filter.status');
  56. if (is_numeric($status))
  57. {
  58. $query->where('a.status = ' . (int) $status);
  59. }
  60. // Filter by request type
  61. $requestType = $this->getState('filter.request_type', '');
  62. if ($requestType)
  63. {
  64. $query->where('a.request_type = ' . $db->quote($db->escape($requestType, true)));
  65. }
  66. // Filter by search in email
  67. $search = $this->getState('filter.search');
  68. if (!empty($search))
  69. {
  70. if (stripos($search, 'id:') === 0)
  71. {
  72. $query->where($db->quoteName('a.id') . ' = ' . (int) substr($search, 3));
  73. }
  74. else
  75. {
  76. $search = $db->quote('%' . $db->escape($search, true) . '%');
  77. $query->where('(' . $db->quoteName('a.email') . ' LIKE ' . $search . ')');
  78. }
  79. }
  80. // Handle the list ordering.
  81. $ordering = $this->getState('list.ordering');
  82. $direction = $this->getState('list.direction');
  83. if (!empty($ordering))
  84. {
  85. $query->order($db->escape($ordering) . ' ' . $db->escape($direction));
  86. }
  87. return $query;
  88. }
  89. /**
  90. * Method to get a store id based on model configuration state.
  91. *
  92. * This is necessary because the model is used by the component and
  93. * different modules that might need different sets of data or different
  94. * ordering requirements.
  95. *
  96. * @param string $id A prefix for the store id.
  97. *
  98. * @return string
  99. *
  100. * @since 3.9.0
  101. */
  102. protected function getStoreId($id = '')
  103. {
  104. // Compile the store id.
  105. $id .= ':' . $this->getState('filter.search');
  106. $id .= ':' . $this->getState('filter.status');
  107. $id .= ':' . $this->getState('filter.request_type');
  108. return parent::getStoreId($id);
  109. }
  110. /**
  111. * Method to auto-populate the model state.
  112. *
  113. * Note. Calling getState in this method will result in recursion.
  114. *
  115. * @param string $ordering An optional ordering field.
  116. * @param string $direction An optional direction (asc|desc).
  117. *
  118. * @return void
  119. *
  120. * @since 3.9.0
  121. */
  122. protected function populateState($ordering = 'a.id', $direction = 'desc')
  123. {
  124. // Load the filter state.
  125. $this->setState(
  126. 'filter.search',
  127. $this->getUserStateFromRequest($this->context . '.filter.search', 'filter_search')
  128. );
  129. $this->setState(
  130. 'filter.status',
  131. $this->getUserStateFromRequest($this->context . '.filter.status', 'filter_status', '', 'int')
  132. );
  133. $this->setState(
  134. 'filter.request_type',
  135. $this->getUserStateFromRequest($this->context . '.filter.request_type', 'filter_request_type', '', 'string')
  136. );
  137. // Load the parameters.
  138. $this->setState('params', JComponentHelper::getParams('com_privacy'));
  139. // List state information.
  140. parent::populateState($ordering, $direction);
  141. }
  142. /**
  143. * Method to return number privacy requests older than X days.
  144. *
  145. * @return integer
  146. *
  147. * @since 3.9.0
  148. */
  149. public function getNumberUrgentRequests()
  150. {
  151. // Load the parameters.
  152. $params = ComponentHelper::getComponent('com_privacy')->getParams();
  153. $notify = (int) $params->get('notify', 14);
  154. $now = JFactory::getDate()->toSql();
  155. $period = '-' . $notify;
  156. $db = $this->getDbo();
  157. $query = $db->getQuery(true)
  158. ->select('COUNT(*)');
  159. $query->from($db->quoteName('#__privacy_requests'));
  160. $query->where($db->quoteName('status') . ' = 1 ');
  161. $query->where($query->dateAdd($db->quote($now), $period, 'DAY') . ' > ' . $db->quoteName('requested_at'));
  162. $db->setQuery($query);
  163. return (int) $db->loadResult();
  164. }
  165. }