PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Molinos/Admin/ListHandler.php

https://code.google.com/p/molinos-cms/
PHP | 238 lines | 161 code | 33 blank | 44 comment | 36 complexity | f24afc80edbe13c14a87931c9a56f44a MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * ????? ??? ?????? ?????? ??? ? ???????????????? ??????????.
  4. *
  5. * @package Molinos_CMS
  6. * @subpackage Admin
  7. * @author Justin Forest <justin.forest@gmail.com>
  8. * @copyright 2006-2011 molinos.ru
  9. * @license http://www.gnu.org/copyleft/gpl.html GPL
  10. * @link http://code.google.com/p/molinos-cms/wiki/Molinos_Admin_ListHandler
  11. */
  12. class Molinos_Admin_ListHandler extends Molinos_Core_RequestHandler
  13. {
  14. public function get()
  15. {
  16. $args = func_get_args();
  17. return $this->serveNodeList($this->route, array_shift($args));
  18. }
  19. /**
  20. * ?????????? ?????? ?????????? ?? ????????? ???????? ????????.
  21. */
  22. protected function serveNodeList(array $route, $type = null)
  23. {
  24. $filter = $options = array();
  25. if (null !== $type)
  26. $filter['class'] = $type;
  27. elseif (!empty($route['types']))
  28. $filter['class'] = explode(',', $route['types']);
  29. elseif ($tmp = $this->arg('type'))
  30. $filter['class'] = $tmp;
  31. if ($tmp = $this->arg('author'))
  32. $filter['uid'] = $tmp;
  33. if (!empty($route['nolimit']))
  34. $filter['#limit'] = 'none';
  35. else
  36. $filter['#limit'] = $this->arg('limit', 10);
  37. if ($tmp = $this->arg('search'))
  38. $filter['#search'] = $tmp;
  39. if (isset($route['listsort']))
  40. $options['sort'] = $route['listsort'];
  41. if (!empty($route['listtitle']))
  42. $options['title'] = t($route['listtitle']);
  43. elseif (!empty($route['title']))
  44. $options['title'] = t($route['title']);
  45. foreach (preg_split('/,\s*/', @$route['listoptions'], -1, PREG_SPLIT_NO_EMPTY) as $option) {
  46. switch ($option) {
  47. case 'nolimit':
  48. $filter['#limit'] = 'none';
  49. break;
  50. case 'nocreate':
  51. $options['create'] = false;
  52. break;
  53. case 'nopublishing':
  54. $options['nopublishing'] = true;
  55. break;
  56. case 'nohidden':
  57. $filter['published'] = true;
  58. break;
  59. default:
  60. $options[$option] = true;
  61. break;
  62. }
  63. }
  64. if (!empty($route['docurl']))
  65. $options['docurl'] = $route['docurl'];
  66. if (!empty($route['submiturl']))
  67. $options['create'] = str_replace('destination=CURRENT', 'destination=' . urlencode($this->request->url), $route['submiturl']);
  68. $options['recurse'] = (bool)$this->arg('recurse');
  69. if (isset($filter['#limit']) and 'none' != $filter['#limit'])
  70. $options['limit'] = $filter['#limit'];
  71. if (isset($filter['#limit']) and $tmp = intval($this->arg('skip')))
  72. $filter['#offset'] = $tmp;
  73. $options['can'] = $this->arg('can', empty($route['defaultcan']) ? 'all' : $route['defaultcan']);
  74. return $this->sendList($filter, $options);
  75. }
  76. /**
  77. * ?????????? ?????? ??????????.
  78. */
  79. protected function sendList(array $filter, array $options)
  80. {
  81. $output = '';
  82. $nodeapi = Molinos_Core_API::getInstance('node');
  83. $user = Molinos_Core_API::getInstance('auth')->getUser();
  84. if (isset($options['nolang'])) {
  85. // ????? ??? ?? ????? ?????.
  86. $filter['lang'] = array_reverse(array_unique(array_merge(array($this->ctx->lang), Molinos_I18n_Utils::getEnabledLanguages())));
  87. } else {
  88. $filter['lang'] = preg_split('/[,\s]+/', $this->arg('lang', $this->ctx->lang));
  89. }
  90. // ?????????? ?????????????? ????, ???? ?? ??????? ?????????.
  91. if (empty($filter['class']))
  92. $filter['class'] = Molinos_Schema_Nodes_Type::getList($this->ctx, empty($filter['deleted']));
  93. // ????????? ??????????? ??? ?????? ????.
  94. $filter['class'] = array_values(array_intersect($user->getAccess(Molinos_Auth_ACL::READ), (array)$filter['class']));
  95. if ($tmp = $this->arg('scope'))
  96. $filter['scope'] = $tmp;
  97. // ?????? ?? ????????.
  98. if ($ids = preg_split('/\s+/', $this->arg('tags'), -1, PREG_SPLIT_NO_EMPTY)) {
  99. if (!empty($options['recurse']) and is_array($tmp = $this->unfoldIds($ids)))
  100. $ids = $tmp;
  101. $filter['tags'] = $ids;
  102. $output .= Molinos_Base_XML::wrap('filters', $nodeapi->findXML(array(
  103. 'id' => $ids,
  104. )));
  105. }
  106. $this->addTagFilter($filter, $options);
  107. // ???? ???? ?? ??????? ???????? ????????? — ??????? ?????? ?? ?????????.
  108. if (!array_key_exists('deleted', $filter))
  109. $filter['deleted'] = 0;
  110. // ????????? ??????????.
  111. if ($tmp = $this->arg('sort'))
  112. $filter['#sort'] = $tmp;
  113. elseif (isset($options['sort']))
  114. $filter['#sort'] = $options['sort'];
  115. else
  116. $filter['#sort'] = '-id';
  117. if (!array_key_exists('create', $options)) {
  118. // ?????????? ??????????? ????: ??????? ???????? ?????? ?? ??? ????? ??????????,
  119. // ???? ?? ????????? — ?????? ?????? ?? submit, ???? ???? — ?? ?????????? ?????.
  120. // ??? ????? ????? ??????, ??? ???????????? ?????? ????? ????????????? ??????
  121. // ?????, ??? ?????????, ? ?? ????? ?????? ??????????? ????? ????????? ?????? ????.
  122. $create = array();
  123. foreach ($filter['class'] as $class)
  124. if ($user->hasAccess(Molinos_Auth_ACL::CREATE, $class))
  125. $create[] = 'submit/' . $class . '?destination=' . urlencode($this->ctx->request->uri);
  126. if (1 == count($create))
  127. $options['create'] = $create[0];
  128. elseif ($create)
  129. $options['create'] = 'submit?destination=' . urlencode($this->ctx->request->uri);
  130. }
  131. $options['onetype'] = (1 == count($filter['class']));
  132. $this->applyCannedOptions($options, $filter);
  133. // ??????? ?????? ?????????? ???.
  134. $options['total'] = $nodeapi->count($filter);
  135. $options['skip'] = $this->arg('skip');
  136. $output .= $nodeapi->findPagedXML($filter, $options);
  137. $output .= $this->getTypeNames();
  138. return new Molinos_Base_Page($output, array(
  139. 'lang' => $filter['lang'][0],
  140. ));
  141. }
  142. /**
  143. * ?????????? ??????? ?? ????????, ???? ??????? ???? ?? ???????.
  144. *
  145. * @url http://code.google.com/p/molinos-cms/issues/detail?id=1267
  146. */
  147. protected function addTagFilter(array &$filter, array &$options)
  148. {
  149. if (!empty($filter['tags']))
  150. return;
  151. if (!empty($options['notaglimit']))
  152. return;
  153. if (isset($filter['#search']) and false !== strpos(' ' . $filter['#search'], ' tags:'))
  154. return;
  155. if ($tapi = Molinos_Core_API::getInstance('taxonomy', true)) {
  156. $filter['tags'] = $tapi->getPermittedSections();
  157. if ($uid = Molinos_Core_API::getInstance('auth')->getUser()->id)
  158. $filter['#own'] = $uid;
  159. }
  160. }
  161. protected function getTypeNames()
  162. {
  163. $result = '';
  164. $typeNames = Molinos_Core_API::getInstance('node')
  165. ->getSortedList('type', 'title', 'name');
  166. foreach ($typeNames as $name => $title)
  167. $result .= Molinos_Base_XML::em('type', array(
  168. 'name' => $name,
  169. '#text' => Molinos_Base_XML::cdata($title),
  170. ));
  171. return Molinos_Base_XML::em('types', $result);
  172. }
  173. protected function unfoldIds($ids)
  174. {
  175. if ($api = Molinos_Core_API::getInstance('taxonomy', true))
  176. return $api->unfoldIds($ids);
  177. }
  178. /**
  179. * ?????????? ????????????? ????????.
  180. *
  181. * ???? ? ????????? can ?????? ???????????? ??????, ????????? ???
  182. * ? ???????? ???????.
  183. *
  184. * @param array &$options ???????? ??????.
  185. * @param array &$filter ????????? ???????.
  186. *
  187. * @uses Molinos_Admin_Handlers_CanXML::getCans() ??? ?????????
  188. * ????????????? ????????.
  189. */
  190. protected function applyCannedOptions(array &$options, array &$filter)
  191. {
  192. if (array_key_exists('can', $options)) {
  193. foreach ($all = Molinos_Admin_Handlers_CanXML::getCans($this->ctx, $this->path) as $k => $v) {
  194. if ($k == $options['can']) {
  195. Molinos_Core_Logger::getInstance()->debug("Applying canned options: " . var_export($v, true));
  196. foreach (array('options', 'filter') as $k)
  197. if (isset($v[$k]) and is_array($v[$k]))
  198. $$k = array_merge($$k, $v[$k]);
  199. if (isset($v['message']))
  200. $options['message'] = $v['message'];
  201. }
  202. }
  203. }
  204. }
  205. }