PageRenderTime 30ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/library/XenResource/Search/DataHandler/Update.php

https://gitlab.com/thoinv/forums
PHP | 373 lines | 243 code | 47 blank | 83 comment | 27 complexity | 4d2c9161877ce097f602d056b54c8e6a MD5 | raw file
  1. <?php
  2. /**
  3. * Handles searching of resource updates.
  4. */
  5. class XenResource_Search_DataHandler_Update extends XenForo_Search_DataHandler_Abstract
  6. {
  7. protected $_resourceModel;
  8. /**
  9. * Inserts into (or replaces a record) in the index.
  10. *
  11. * @see XenForo_Search_DataHandler_Abstract::_insertIntoIndex()
  12. */
  13. protected function _insertIntoIndex(XenForo_Search_Indexer $indexer, array $data, array $parentData = null)
  14. {
  15. $metadata = array();
  16. $metadata['resource'] = $data['resource_id'];
  17. if ($parentData)
  18. {
  19. $metadata['rescat'] = $parentData['resource_category_id'];
  20. $userId = $parentData['user_id'];
  21. if ($data['resource_update_id'] == $parentData['description_update_id'] || !$parentData['description_update_id'])
  22. {
  23. $data['message'] .= ' ' . $parentData['tag_line'];
  24. $metadata['is_resource'] = 1;
  25. }
  26. if (!empty($parentData['prefix_id']))
  27. {
  28. $metadata['resprefix'] = $parentData['prefix_id'];
  29. }
  30. }
  31. else
  32. {
  33. $userId = 0;
  34. }
  35. $indexer->insertIntoIndex(
  36. 'resource_update', $data['resource_update_id'],
  37. $data['title'], $data['message'],
  38. $data['post_date'], $userId, $data['resource_id'], $metadata
  39. );
  40. }
  41. /**
  42. * Updates a record in the index.
  43. *
  44. * @see XenForo_Search_DataHandler_Abstract::_updateIndex()
  45. */
  46. protected function _updateIndex(XenForo_Search_Indexer $indexer, array $data, array $fieldUpdates)
  47. {
  48. $indexer->updateIndex('resource_update', $data['resource_update_id'], $fieldUpdates);
  49. }
  50. /**
  51. * Deletes one or more records from the index.
  52. *
  53. * @see XenForo_Search_DataHandler_Abstract::_deleteFromIndex()
  54. */
  55. protected function _deleteFromIndex(XenForo_Search_Indexer $indexer, array $dataList)
  56. {
  57. $updateIds = array();
  58. foreach ($dataList AS $data)
  59. {
  60. if (is_array($data))
  61. {
  62. $updateIds[] = $data['resource_update_id'];
  63. }
  64. else
  65. {
  66. $updateIds[] = $data;
  67. }
  68. }
  69. $indexer->deleteFromIndex('resource_update', $updateIds);
  70. }
  71. /**
  72. * Rebuilds the index for a batch.
  73. *
  74. * @see XenForo_Search_DataHandler_Abstract::rebuildIndex()
  75. */
  76. public function rebuildIndex(XenForo_Search_Indexer $indexer, $lastId, $batchSize)
  77. {
  78. $updateIds = $this->_getUpdateModel()->getUpdateIdsInRange($lastId, $batchSize);
  79. if (!$updateIds)
  80. {
  81. return false;
  82. }
  83. $this->quickIndex($indexer, $updateIds);
  84. return max($updateIds);
  85. }
  86. /**
  87. * Rebuilds the index for the specified content.
  88. * @see XenForo_Search_DataHandler_Abstract::quickIndex()
  89. */
  90. public function quickIndex(XenForo_Search_Indexer $indexer, array $contentIds)
  91. {
  92. $updates = $this->_getUpdateModel()->getUpdatesByIds($contentIds, array(
  93. 'join' => XenResource_Model_Update::FETCH_RESOURCE
  94. ));
  95. foreach ($updates AS $update)
  96. {
  97. $this->insertIntoIndex($indexer, $update, $update);
  98. }
  99. return true;
  100. }
  101. /**
  102. * Gets the type-specific data for a collection of results of this content type.
  103. *
  104. * @see XenForo_Search_DataHandler_Abstract::getDataForResults()
  105. */
  106. public function getDataForResults(array $ids, array $viewingUser, array $resultsGrouped)
  107. {
  108. return $this->_getUpdateModel()->getUpdatesByIds($ids, array(
  109. 'join' => XenResource_Model_Update::FETCH_RESOURCE |
  110. XenResource_Model_Update::FETCH_RESOURCE_VERSION |
  111. XenResource_Model_Update::FETCH_CATEGORY |
  112. XenResource_Model_Update::FETCH_USER,
  113. 'permissionCombinationId' => $viewingUser['permission_combination_id']
  114. ));
  115. }
  116. /**
  117. * Determines if this result is viewable.
  118. *
  119. * @see XenForo_Search_DataHandler_Abstract::canViewResult()
  120. */
  121. public function canViewResult(array $result, array $viewingUser)
  122. {
  123. $categoryPermissions = XenForo_Permission::unserializePermissions($result['category_permission_cache']);
  124. return $this->_getUpdateModel()->canViewUpdateAndContainer(
  125. $result, $result, $result, $null, $viewingUser, $categoryPermissions
  126. );
  127. }
  128. /**
  129. * Prepares a result for display.
  130. *
  131. * @see XenForo_Search_DataHandler_Abstract::prepareResult()
  132. */
  133. public function prepareResult(array $result, array $viewingUser)
  134. {
  135. return $this->_getUpdateModel()->prepareUpdate($result, $result, $result, $viewingUser);
  136. }
  137. /**
  138. * Gets the date of the result (from the result's content).
  139. *
  140. * @see XenForo_Search_DataHandler_Abstract::getResultDate()
  141. */
  142. public function getResultDate(array $result)
  143. {
  144. return $result['post_date'];
  145. }
  146. /**
  147. * Renders a result to HTML.
  148. *
  149. * @see XenForo_Search_DataHandler_Abstract::renderResult()
  150. */
  151. public function renderResult(XenForo_View $view, array $result, array $search)
  152. {
  153. return $view->createTemplateObject('search_result_resource_update', array(
  154. 'update' => $result,
  155. 'resource' => $result,
  156. 'search' => $search
  157. ));
  158. }
  159. /**
  160. * Returns an array of content types handled by this class
  161. *
  162. * @see XenForo_Search_DataHandler_Abstract::getSearchContentTypes()
  163. */
  164. public function getSearchContentTypes()
  165. {
  166. return array('resource_update');
  167. }
  168. /**
  169. * Get type-specific constraints from input.
  170. *
  171. * @param XenForo_Input $input
  172. *
  173. * @return array
  174. */
  175. public function getTypeConstraintsFromInput(XenForo_Input $input)
  176. {
  177. $constraints = array();
  178. $categories = $input->filterSingle('categories', XenForo_Input::UINT, array('array' => true));
  179. if ($categories && !in_array(0, $categories))
  180. {
  181. if ($input->inRequest('child_categories'))
  182. {
  183. $includeChildren = $input->filterSingle('child_categories', XenForo_Input::UINT);
  184. }
  185. else
  186. {
  187. $includeChildren = true;
  188. }
  189. if ($includeChildren)
  190. {
  191. $descendants = array_keys(XenForo_Model::create('XenResource_Model_Category')->getDescendantsOfCategoryIds($categories));
  192. $categories = array_merge($categories, $descendants);
  193. }
  194. $categories = array_unique($categories);
  195. $constraints['rescat'] = implode(' ', $categories);
  196. if (!$constraints['rescat'])
  197. {
  198. unset($constraints['rescat']); // just 0
  199. }
  200. }
  201. $prefixes = $input->filterSingle('prefixes', XenForo_Input::UINT, array('array' => true));
  202. if ($prefixes && reset($prefixes))
  203. {
  204. $prefixes = array_unique($prefixes);
  205. $constraints['resprefix'] = implode(' ', $prefixes);
  206. if (!$constraints['resprefix'])
  207. {
  208. unset($constraints['resprefix']); // just 0
  209. }
  210. }
  211. if ($input->filterSingle('is_resource', XenForo_Input::UINT)) {
  212. $constraints['is_resource'] = true;
  213. }
  214. return $constraints;
  215. }
  216. /**
  217. * Process a type-specific constraint.
  218. *
  219. * @see XenForo_Search_DataHandler_Abstract::processConstraint()
  220. */
  221. public function processConstraint(XenForo_Search_SourceHandler_Abstract $sourceHandler, $constraint, $constraintInfo, array $constraints)
  222. {
  223. switch ($constraint)
  224. {
  225. case 'rescat':
  226. if ($constraintInfo)
  227. {
  228. return array(
  229. 'metadata' => array('rescat', preg_split('/\D+/', strval($constraintInfo))),
  230. );
  231. }
  232. break;
  233. case 'resprefix':
  234. if ($constraintInfo)
  235. {
  236. return array(
  237. 'metadata' => array('resprefix', preg_split('/\D+/', strval($constraintInfo))),
  238. );
  239. }
  240. case 'is_resource':
  241. return array(
  242. 'metadata' => array('is_resource', array(1))
  243. );
  244. }
  245. return false;
  246. }
  247. /**
  248. * Gets the search form controller response for this type.
  249. *
  250. * @see XenForo_Search_DataHandler_Abstract::getSearchFormControllerResponse()
  251. */
  252. public function getSearchFormControllerResponse(XenForo_ControllerPublic_Abstract $controller, XenForo_Input $input, array $viewParams)
  253. {
  254. /** @var $resourceModel XenResource_Model_Resource */
  255. $resourceModel = XenForo_Model::create('XenResource_Model_Resource');
  256. if (!$resourceModel->canViewResources($error))
  257. {
  258. return $controller->responseNoPermission();
  259. }
  260. $params = $input->filterSingle('c', XenForo_Input::ARRAY_SIMPLE);
  261. if (!empty($params['rescat']))
  262. {
  263. $viewParams['search']['categories'] = array_fill_keys(explode(' ', $params['rescat']), true);
  264. }
  265. else
  266. {
  267. $viewParams['search']['categories'] = array();
  268. }
  269. $viewParams['search']['child_categories'] = true;
  270. $viewParams['categories'] = XenForo_Model::create('XenResource_Model_Category')->getViewableCategories();
  271. if (!empty($params['prefix']))
  272. {
  273. $viewParams['search']['prefixes'] = array_fill_keys(explode(' ', $params['prefix']), true);
  274. }
  275. else
  276. {
  277. $viewParams['search']['prefixes'] = array();
  278. }
  279. /** @var $prefixModel XenResource_Model_Prefix */
  280. $prefixModel = XenForo_Model::create('XenResource_Model_Prefix');
  281. $viewParams['prefixes'] = $prefixModel->getPrefixesByGroups();
  282. if ($viewParams['prefixes'])
  283. {
  284. $visiblePrefixes = $prefixModel->getVisiblePrefixIds();
  285. foreach ($viewParams['prefixes'] AS $key => $prefixes)
  286. {
  287. foreach ($prefixes AS $prefixId => $prefix)
  288. {
  289. if (!isset($visiblePrefixes[$prefixId]))
  290. {
  291. unset($prefixes[$prefixId]);
  292. }
  293. }
  294. if (!count($prefixes))
  295. {
  296. unset($viewParams['prefixes'][$key]);
  297. }
  298. }
  299. }
  300. $viewParams['search']['is_resource'] = !empty($params['is_resource']);
  301. return $controller->responseView('XenResource_ViewPublic_Search_Form_ResourceUpdate', 'search_form_resource_update', $viewParams);
  302. }
  303. /**
  304. * @return XenResource_Model_Resource
  305. */
  306. protected function _getResourceModel()
  307. {
  308. if (!$this->_resourceModel)
  309. {
  310. $this->_resourceModel = XenForo_Model::create('XenResource_Model_Resource');
  311. }
  312. return $this->_resourceModel;
  313. }
  314. /**
  315. * @return XenResource_Model_Update
  316. */
  317. protected function _getUpdateModel()
  318. {
  319. if (!$this->_resourceModel)
  320. {
  321. $this->_resourceModel = XenForo_Model::create('XenResource_Model_Update');
  322. }
  323. return $this->_resourceModel;
  324. }
  325. }