/lib/Molinos/Admin/ListHandler.php
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
- <?php
- /**
- * ????? ??? ?????? ?????? ??? ? ???????????????? ??????????.
- *
- * @package Molinos_CMS
- * @subpackage Admin
- * @author Justin Forest <justin.forest@gmail.com>
- * @copyright 2006-2011 molinos.ru
- * @license http://www.gnu.org/copyleft/gpl.html GPL
- * @link http://code.google.com/p/molinos-cms/wiki/Molinos_Admin_ListHandler
- */
- class Molinos_Admin_ListHandler extends Molinos_Core_RequestHandler
- {
- public function get()
- {
- $args = func_get_args();
- return $this->serveNodeList($this->route, array_shift($args));
- }
- /**
- * ?????????? ?????? ?????????? ?? ????????? ???????? ????????.
- */
- protected function serveNodeList(array $route, $type = null)
- {
- $filter = $options = array();
- if (null !== $type)
- $filter['class'] = $type;
- elseif (!empty($route['types']))
- $filter['class'] = explode(',', $route['types']);
- elseif ($tmp = $this->arg('type'))
- $filter['class'] = $tmp;
- if ($tmp = $this->arg('author'))
- $filter['uid'] = $tmp;
- if (!empty($route['nolimit']))
- $filter['#limit'] = 'none';
- else
- $filter['#limit'] = $this->arg('limit', 10);
- if ($tmp = $this->arg('search'))
- $filter['#search'] = $tmp;
- if (isset($route['listsort']))
- $options['sort'] = $route['listsort'];
- if (!empty($route['listtitle']))
- $options['title'] = t($route['listtitle']);
- elseif (!empty($route['title']))
- $options['title'] = t($route['title']);
- foreach (preg_split('/,\s*/', @$route['listoptions'], -1, PREG_SPLIT_NO_EMPTY) as $option) {
- switch ($option) {
- case 'nolimit':
- $filter['#limit'] = 'none';
- break;
- case 'nocreate':
- $options['create'] = false;
- break;
- case 'nopublishing':
- $options['nopublishing'] = true;
- break;
- case 'nohidden':
- $filter['published'] = true;
- break;
- default:
- $options[$option] = true;
- break;
- }
- }
- if (!empty($route['docurl']))
- $options['docurl'] = $route['docurl'];
- if (!empty($route['submiturl']))
- $options['create'] = str_replace('destination=CURRENT', 'destination=' . urlencode($this->request->url), $route['submiturl']);
- $options['recurse'] = (bool)$this->arg('recurse');
- if (isset($filter['#limit']) and 'none' != $filter['#limit'])
- $options['limit'] = $filter['#limit'];
- if (isset($filter['#limit']) and $tmp = intval($this->arg('skip')))
- $filter['#offset'] = $tmp;
- $options['can'] = $this->arg('can', empty($route['defaultcan']) ? 'all' : $route['defaultcan']);
- return $this->sendList($filter, $options);
- }
- /**
- * ?????????? ?????? ??????????.
- */
- protected function sendList(array $filter, array $options)
- {
- $output = '';
- $nodeapi = Molinos_Core_API::getInstance('node');
- $user = Molinos_Core_API::getInstance('auth')->getUser();
- if (isset($options['nolang'])) {
- // ????? ??? ?? ????? ?????.
- $filter['lang'] = array_reverse(array_unique(array_merge(array($this->ctx->lang), Molinos_I18n_Utils::getEnabledLanguages())));
- } else {
- $filter['lang'] = preg_split('/[,\s]+/', $this->arg('lang', $this->ctx->lang));
- }
- // ?????????? ?????????????? ????, ???? ?? ??????? ?????????.
- if (empty($filter['class']))
- $filter['class'] = Molinos_Schema_Nodes_Type::getList($this->ctx, empty($filter['deleted']));
- // ????????? ??????????? ??? ?????? ????.
- $filter['class'] = array_values(array_intersect($user->getAccess(Molinos_Auth_ACL::READ), (array)$filter['class']));
- if ($tmp = $this->arg('scope'))
- $filter['scope'] = $tmp;
- // ?????? ?? ????????.
- if ($ids = preg_split('/\s+/', $this->arg('tags'), -1, PREG_SPLIT_NO_EMPTY)) {
- if (!empty($options['recurse']) and is_array($tmp = $this->unfoldIds($ids)))
- $ids = $tmp;
- $filter['tags'] = $ids;
- $output .= Molinos_Base_XML::wrap('filters', $nodeapi->findXML(array(
- 'id' => $ids,
- )));
- }
- $this->addTagFilter($filter, $options);
- // ???? ???? ?? ??????? ???????? ????????? ??????? ?????? ?? ?????????.
- if (!array_key_exists('deleted', $filter))
- $filter['deleted'] = 0;
- // ????????? ??????????.
- if ($tmp = $this->arg('sort'))
- $filter['#sort'] = $tmp;
- elseif (isset($options['sort']))
- $filter['#sort'] = $options['sort'];
- else
- $filter['#sort'] = '-id';
- if (!array_key_exists('create', $options)) {
- // ?????????? ??????????? ????: ??????? ???????? ?????? ?? ??? ????? ??????????,
- // ???? ?? ????????? ?????? ?????? ?? submit, ???? ???? ?? ?????????? ?????.
- // ??? ????? ????? ??????, ??? ???????????? ?????? ????? ????????????? ??????
- // ?????, ??? ?????????, ? ?? ????? ?????? ??????????? ????? ????????? ?????? ????.
- $create = array();
- foreach ($filter['class'] as $class)
- if ($user->hasAccess(Molinos_Auth_ACL::CREATE, $class))
- $create[] = 'submit/' . $class . '?destination=' . urlencode($this->ctx->request->uri);
- if (1 == count($create))
- $options['create'] = $create[0];
- elseif ($create)
- $options['create'] = 'submit?destination=' . urlencode($this->ctx->request->uri);
- }
- $options['onetype'] = (1 == count($filter['class']));
- $this->applyCannedOptions($options, $filter);
- // ??????? ?????? ?????????? ???.
- $options['total'] = $nodeapi->count($filter);
- $options['skip'] = $this->arg('skip');
- $output .= $nodeapi->findPagedXML($filter, $options);
- $output .= $this->getTypeNames();
- return new Molinos_Base_Page($output, array(
- 'lang' => $filter['lang'][0],
- ));
- }
- /**
- * ?????????? ??????? ?? ????????, ???? ??????? ???? ?? ???????.
- *
- * @url http://code.google.com/p/molinos-cms/issues/detail?id=1267
- */
- protected function addTagFilter(array &$filter, array &$options)
- {
- if (!empty($filter['tags']))
- return;
- if (!empty($options['notaglimit']))
- return;
- if (isset($filter['#search']) and false !== strpos(' ' . $filter['#search'], ' tags:'))
- return;
- if ($tapi = Molinos_Core_API::getInstance('taxonomy', true)) {
- $filter['tags'] = $tapi->getPermittedSections();
- if ($uid = Molinos_Core_API::getInstance('auth')->getUser()->id)
- $filter['#own'] = $uid;
- }
- }
- protected function getTypeNames()
- {
- $result = '';
- $typeNames = Molinos_Core_API::getInstance('node')
- ->getSortedList('type', 'title', 'name');
- foreach ($typeNames as $name => $title)
- $result .= Molinos_Base_XML::em('type', array(
- 'name' => $name,
- '#text' => Molinos_Base_XML::cdata($title),
- ));
- return Molinos_Base_XML::em('types', $result);
- }
- protected function unfoldIds($ids)
- {
- if ($api = Molinos_Core_API::getInstance('taxonomy', true))
- return $api->unfoldIds($ids);
- }
- /**
- * ?????????? ????????????? ????????.
- *
- * ???? ? ????????? can ?????? ???????????? ??????, ????????? ???
- * ? ???????? ???????.
- *
- * @param array &$options ???????? ??????.
- * @param array &$filter ????????? ???????.
- *
- * @uses Molinos_Admin_Handlers_CanXML::getCans() ??? ?????????
- * ????????????? ????????.
- */
- protected function applyCannedOptions(array &$options, array &$filter)
- {
- if (array_key_exists('can', $options)) {
- foreach ($all = Molinos_Admin_Handlers_CanXML::getCans($this->ctx, $this->path) as $k => $v) {
- if ($k == $options['can']) {
- Molinos_Core_Logger::getInstance()->debug("Applying canned options: " . var_export($v, true));
- foreach (array('options', 'filter') as $k)
- if (isset($v[$k]) and is_array($v[$k]))
- $$k = array_merge($$k, $v[$k]);
- if (isset($v['message']))
- $options['message'] = $v['message'];
- }
- }
- }
- }
- }