/** * Loads the data. * * @param array $data Data array with parameters. */ public function loadData(&$data) { $serviceManager = ServiceUtil::getManager(); $controllerHelper = new Reviews_Util_Controller($serviceManager); $utilArgs = array('name' => 'detail'); if (!isset($data['objectType']) || !in_array($data['objectType'], $controllerHelper->getObjectTypes('contentType', $utilArgs))) { $data['objectType'] = $controllerHelper->getDefaultObjectType('contentType', $utilArgs); } $this->objectType = $data['objectType']; if (!isset($data['id'])) { $data['id'] = null; } if (!isset($data['displayMode'])) { $data['displayMode'] = 'embed'; } $this->id = $data['id']; $this->displayMode = $data['displayMode']; }
/** * This method provides a generic item detail view. * * @param string $ot Treated object type. * @param string $tpl Name of alternative template (for alternative display options, feeds and xml output) * @param boolean $raw Optional way to display a template instead of fetching it (needed for standalone output) * * @return mixed Output. */ public function display() { $controllerHelper = new Reviews_Util_Controller($this->serviceManager); // parameter specifying which type of objects we are treating $objectType = $this->request->query->filter('ot', 'review', FILTER_SANITIZE_STRING); $utilArgs = array('controller' => 'user', 'action' => 'display'); if (!in_array($objectType, $controllerHelper->getObjectTypes('controllerAction', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('controllerAction', $utilArgs); } $this->throwForbiddenUnless(SecurityUtil::checkPermission($this->name . ':' . ucwords($objectType) . ':', '::', ACCESS_READ), LogUtil::getErrorMsgPermission()); $entityClass = $this->name . '_Entity_' . ucwords($objectType); $repository = $this->entityManager->getRepository($entityClass); $repository->setControllerArguments(array()); $idFields = ModUtil::apiFunc($this->name, 'selection', 'getIdFields', array('ot' => $objectType)); // retrieve identifier of the object we wish to view $idValues = $controllerHelper->retrieveIdentifier($this->request, array(), $objectType, $idFields); $hasIdentifier = $controllerHelper->isValidIdentifier($idValues); // check for unique permalinks (without id) $hasSlug = false; $slug = ''; if ($hasIdentifier === false) { $entityClass = $this->name . '_Entity_' . ucwords($objectType); $meta = $this->entityManager->getClassMetadata($entityClass); $hasSlug = $meta->hasField('slug') && $meta->isUniqueField('slug'); if ($hasSlug) { $slug = $this->request->query->filter('slug', '', FILTER_SANITIZE_STRING); $hasSlug = !empty($slug); } } $hasIdentifier |= $hasSlug; $this->throwNotFoundUnless($hasIdentifier, $this->__('Error! Invalid identifier received.')); $entity = ModUtil::apiFunc($this->name, 'selection', 'getEntity', array('ot' => $objectType, 'id' => $idValues, 'slug' => $slug)); $this->throwNotFoundUnless($entity != null, $this->__('No such item.')); unset($idValues); $entity->initWorkflow(); // build ModUrl instance for display hooks; also create identifier for permission check $currentUrlArgs = array('ot' => $objectType); $instanceId = ''; foreach ($idFields as $idField) { $currentUrlArgs[$idField] = $entity[$idField]; if (!empty($instanceId)) { $instanceId .= '_'; } $instanceId .= $entity[$idField]; } $currentUrlArgs['id'] = $instanceId; if (isset($entity['slug'])) { $currentUrlArgs['slug'] = $entity['slug']; } $currentUrlObject = new Zikula_ModUrl($this->name, 'user', 'display', ZLanguage::getLanguageCode(), $currentUrlArgs); $this->throwForbiddenUnless(SecurityUtil::checkPermission($this->name . ':' . ucwords($objectType) . ':', $instanceId . '::', ACCESS_READ), LogUtil::getErrorMsgPermission()); $viewHelper = new Reviews_Util_View($this->serviceManager); $templateFile = $viewHelper->getViewTemplate($this->view, 'user', $objectType, 'display', array()); // set cache id $component = $this->name . ':' . ucwords($objectType) . ':'; $instance = $instanceId . '::'; $accessLevel = ACCESS_READ; if (SecurityUtil::checkPermission($component, $instance, ACCESS_COMMENT)) { $accessLevel = ACCESS_COMMENT; } if (SecurityUtil::checkPermission($component, $instance, ACCESS_EDIT)) { $accessLevel = ACCESS_EDIT; } $this->view->setCacheId($objectType . '|' . $instanceId . '|a' . $accessLevel); // assign output data to view object. $this->view->assign($objectType, $entity)->assign('currentUrlObject', $currentUrlObject)->assign($repository->getAdditionalTemplateParameters('controllerAction', $utilArgs)); //$controllerHelper = new Reviews_Util_Controller($this->serviceManager); $controllerHelper->addView($entity['id']); // fetch and return the appropriate template return $viewHelper->processTemplate($this->view, 'user', $objectType, 'display', array(), $templateFile); }
/** * Popup selector for Scribite plugins. * Finds items of a certain object type. * * @param string $objectType The object type. * @param string $editor Name of used Scribite editor. * @param string $sort Sorting field. * @param string $sortdir Sorting direction. * @param int $pos Current pager position. * @param int $num Amount of entries to display. * * @return output The external item finder page */ public function finder() { PageUtil::addVar('stylesheet', ThemeUtil::getModuleStylesheet('Reviews')); $getData = $this->request->query; $controllerHelper = new Reviews_Util_Controller($this->serviceManager); $objectType = $getData->filter('objectType', 'review', FILTER_SANITIZE_STRING); $utilArgs = array('controller' => 'external', 'action' => 'finder'); if (!in_array($objectType, $controllerHelper->getObjectTypes('controller', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('controllerType', $utilArgs); } $this->throwForbiddenUnless(SecurityUtil::checkPermission('Reviews:' . ucwords($objectType) . ':', '::', ACCESS_COMMENT), LogUtil::getErrorMsgPermission()); $entityClass = 'Reviews_Entity_' . ucwords($objectType); $repository = $this->entityManager->getRepository($entityClass); $repository->setControllerArguments(array()); $editor = $getData->filter('editor', '', FILTER_SANITIZE_STRING); if (empty($editor) || !in_array($editor, array('xinha', 'tinymce'))) { return $this->__('Error: Invalid editor context given for external controller action.'); } // fetch selected categories to reselect them in the output // the actual filtering is done inside the repository class $categoryIds = ModUtil::apiFunc('Reviews', 'category', 'retrieveCategoriesFromRequest', array('ot' => $objectType, 'source' => 'GET')); $sort = $getData->filter('sort', '', FILTER_SANITIZE_STRING); if (empty($sort) || !in_array($sort, $repository->getAllowedSortingFields())) { $sort = $repository->getDefaultSortingField(); } $sortdir = $getData->filter('sortdir', '', FILTER_SANITIZE_STRING); $sdir = strtolower($sortdir); if ($sdir != 'asc' && $sdir != 'desc') { $sdir = 'asc'; } $sortParam = $sort . ' ' . $sdir; // the current offset which is used to calculate the pagination $currentPage = (int) $getData->filter('pos', 1, FILTER_VALIDATE_INT); // the number of items displayed on a page for pagination $resultsPerPage = (int) $getData->filter('num', 0, FILTER_VALIDATE_INT); if ($resultsPerPage == 0) { $resultsPerPage = $this->getVar('pageSize', 20); } $where = ''; list($entities, $objectCount) = $repository->selectWherePaginated($where, $sortParam, $currentPage, $resultsPerPage); foreach ($entities as $k => $entity) { $entity->initWorkflow(); } $view = Zikula_View::getInstance('Reviews', false); $view->assign('editorName', $editor)->assign('objectType', $objectType)->assign('items', $entities)->assign('sort', $sort)->assign('sortdir', $sdir)->assign('currentPage', $currentPage)->assign('pager', array('numitems' => $objectCount, 'itemsperpage' => $resultsPerPage)); // assign category properties $properties = null; if (in_array($objectType, $this->categorisableObjectTypes)) { $properties = ModUtil::apiFunc('Reviews', 'category', 'getAllProperties', array('ot' => $objectType)); } $view->assign('properties', $properties)->assign('catIds', $categoryIds); return $view->display('external/' . $objectType . '/find.tpl'); }
/** * form custom url string * * @author Mark West * @return string custom url string */ public function encodeurl($args) { // check if we have the required input if (!isset($args['modname']) || !isset($args['func'])) { throw new \InvalidArgumentException(__('Invalid arguments array received.')); } // set default values if (!isset($args['type'])) { $args['type'] = 'user'; } if (!isset($args['args'])) { $args['args'] = array(); } // return if function url scheme is not being customised $customFuncs = array('view', 'display'); if (!in_array($args['func'], $customFuncs)) { return false; } // initialise url routing rules $routerFacade = new Reviews_RouterFacade(); // get router itself for convenience $router = $routerFacade->getRouter(); // initialise object type $controllerHelper = new Reviews_Util_Controller($this->serviceManager); $utilArgs = array('controller' => 'user', 'action' => 'encodeurl'); $allowedObjectTypes = $controllerHelper->getObjectTypes('api', $utilArgs); $objectType = isset($args['args']['ot']) && in_array($args['args']['ot'], $allowedObjectTypes) ? $args['args']['ot'] : $controllerHelper->getDefaultObjectType('api', $utilArgs); // initialise group folder $groupFolder = $routerFacade->getGroupingFolderFromObjectType($objectType, $args['func'], $args['args']); // start pre processing // convert object type to group folder $args['args']['ot'] = $groupFolder; // handle special templates $displayDefaultEnding = System::getVar('shorturlsext', ''); $endingPrefix = $args['func'] == 'view' ? '.' : ''; foreach (array('csv', 'rss', 'atom', 'xml', 'pdf', 'json', 'kml') as $ending) { if (!isset($args['args']['use' . $ending . 'ext'])) { continue; } if ($args['args']['use' . $ending . 'ext'] == '1') { $args['args'][$args['func'] . 'ending'] = $endingPrefix . $ending; } unset($args['args']['use' . $ending . 'ext']); } // fallback to default templates if (!isset($args['args'][$args['func'] . 'ending'])) { if ($args['func'] == 'view') { // category list /* if (isset($args['args']['prop'])) { $vars = $args['args']['prop']; if (isset($args['args']['cat'])) { $vars .= '/'.$args['args']['cat']; } // letter list } elseif (isset($args['args']['letter'])) { $vars = 'letter/'.$args['args']['letter']; } if (isset($args['args']['page']) && $args['args']['page'] != 1) { $vars .= (empty($vars) ? '' : '/').'page/'.$args['args']['page']; }*/ $args['args'][$args['func'] . 'ending'] = ''; //'/'; } else { if ($args['func'] == 'display') { $args['args'][$args['func'] . 'ending'] = $displayDefaultEnding; } } } if ($args['func'] == 'view') { // TODO filter views (e.g. /orders/customer/mr-smith.csv) /** $filterEntities = array('customer', 'region', 'federalstate', 'country'); foreach ($filterEntities as $filterEntity) { $filterField = $filterEntity . 'id'; if (!isset($args['args'][$filterField]) || !$args['args'][$filterField]) { continue; } $filterId = $args['args'][$filterField]; unset($args['args'][$filterField]); $filterGroupFolder = $routerFacade->getGroupingFolderFromObjectType($filterEntity, 'display', $args['args']); $filterSlug = $routerFacade->getFormattedSlug($filterEntity, 'display', $args['args'], $filterId); $result .= $filterGroupFolder . '/' . $filterSlug .'/'; break; } */ } elseif ($args['func'] == 'display') { // determine given id $id = 0; foreach (array('id', strtolower($objectType) . 'id', 'objectid') as $idFieldName) { if (isset($args['args'][$idFieldName])) { $id = $args['args'][$idFieldName]; unset($args['args'][$idFieldName]); } } if (ModUtil::getVar('Reviews', 'addcategorytitletopermalink') == 1 && ModUtil::getVar('Reviews', 'enablecategorization') == 1) { if ($id > 0) { $modelHelper = new Reviews_Util_Model($this->serviceManager); $repository = $modelHelper->getReviewRepository(); $thisreview = $repository->selectById($id); $categories = $thisreview->getCategories(); $name = $categories[0]->getCategory()->getName(); $displayname = $categories[0]->getCategory()->getDisplayName(); } $lang = ZLanguage::getLanguageCode(); $cat = ''; if ($name != '') { $cat = $name; } if ($displayname != '') { $cat = $displayname[$lang]; } $args['args']['cat'] = $cat; } // check if we have a valid slug given if (isset($args['args']['slug']) && (!$args['args']['slug'] || $args['args']['slug'] == $id)) { unset($args['args']['slug']); } // try to determine missing slug if (!isset($args['args']['slug'])) { $slug = ''; if ($id > 0) { $slug = $routerFacade->getFormattedSlug($objectType, $args['func'], $args['args'], $id); } if (!empty($slug) && $slug != $id) { // add slug expression $args['args']['slug'] = $slug; } } // check if we have one now if (!isset($args['args']['slug'])) { // readd id as fallback $args['args']['id'] = $id; } } // add func as first argument $routerArgs = array_merge(array('func' => $args['func']), $args['args']); // now create url based on params $result = $router->generate(null, $routerArgs); // post processing if ($args['func'] == 'view' && !empty($args['args']['viewending']) || $args['func'] == 'display') { // check if url ends with a trailing slash if (substr($result, -1) == '/') { // remove the trailing slash $result = substr($result, 0, strlen($result) - 1); } } // enforce url name of the module, but do only 1 replacement to avoid changing other params $modInfo = ModUtil::getInfoFromName('Reviews'); $result = preg_replace('/' . $modInfo['name'] . '/', $modInfo['url'], $result, 1); $result = preg_replace('#' . 'review/' . '#', '', $result, 1); $result = preg_replace('=' . '\\+' . '=', ' ', $result); return $result; }
/** * This method provides a generic handling of all edit requests. * * @param string $ot Treated object type. * @param string $tpl Name of alternative template (for alternative display options, feeds and xml output) * @param boolean $raw Optional way to display a template instead of fetching it (needed for standalone output) * * @return mixed Output. */ public function edit() { $controllerHelper = new Reviews_Util_Controller($this->serviceManager); // parameter specifying which type of objects we are treating $objectType = $this->request->query->filter('ot', 'review', FILTER_SANITIZE_STRING); $utilArgs = array('controller' => 'user', 'action' => 'edit'); if (!in_array($objectType, $controllerHelper->getObjectTypes('controllerAction', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('controllerAction', $utilArgs); } $this->throwForbiddenUnless(SecurityUtil::checkPermission($this->name . ':' . ucwords($objectType) . ':', '::', ACCESS_EDIT), LogUtil::getErrorMsgPermission()); // create new Form reference $view = FormUtil::newForm($this->name, $this); // build form handler class name $handlerClass = $this->name . '_Form_Handler_User_' . ucfirst($objectType) . '_Edit'; // determine the output template $viewHelper = new Reviews_Util_View($this->serviceManager); $template = $viewHelper->getViewTemplate($this->view, 'user', $objectType, 'edit', array()); // execute form using supplied template and page event handler return $view->execute($template, new $handlerClass()); }
/** * This method provides a generic handling of simple delete requests. * * @param string $ot Treated object type. * @param int $id Identifier of entity to be deleted. * @param boolean $confirmation Confirm the deletion, else a confirmation page is displayed. * @param string $tpl Name of alternative template (for alternative display options, feeds and xml output) * @param boolean $raw Optional way to display a template instead of fetching it (needed for standalone output) * * @return mixed Output. */ public function delete() { $controllerHelper = new Reviews_Util_Controller($this->serviceManager); // parameter specifying which type of objects we are treating $objectType = $this->request->query->filter('ot', 'review', FILTER_SANITIZE_STRING); $utilArgs = array('controller' => 'admin', 'action' => 'delete'); if (!in_array($objectType, $controllerHelper->getObjectTypes('controllerAction', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('controllerAction', $utilArgs); } $this->throwForbiddenUnless(SecurityUtil::checkPermission($this->name . ':' . ucwords($objectType) . ':', '::', ACCESS_ADMIN), LogUtil::getErrorMsgPermission()); $idFields = ModUtil::apiFunc($this->name, 'selection', 'getIdFields', array('ot' => $objectType)); // retrieve identifier of the object we wish to delete $idValues = $controllerHelper->retrieveIdentifier($this->request, array(), $objectType, $idFields); $hasIdentifier = $controllerHelper->isValidIdentifier($idValues); $this->throwNotFoundUnless($hasIdentifier, $this->__('Error! Invalid identifier received.')); $entity = ModUtil::apiFunc($this->name, 'selection', 'getEntity', array('ot' => $objectType, 'id' => $idValues)); $this->throwNotFoundUnless($entity != null, $this->__('No such item.')); $entity->initWorkflow(); $workflowHelper = new Reviews_Util_Workflow($this->serviceManager); $deleteActionId = 'delete'; $deleteAllowed = false; $actions = $workflowHelper->getActionsForObject($entity); if ($actions === false || !is_array($actions)) { return LogUtil::registerError($this->__('Error! Could not determine workflow actions.')); } foreach ($actions as $actionId => $action) { if ($actionId != $deleteActionId) { continue; } $deleteAllowed = true; break; } if (!$deleteAllowed) { return LogUtil::registerError($this->__('Error! It is not allowed to delete this entity.')); } $confirmation = (bool) $this->request->request->filter('confirmation', false, FILTER_VALIDATE_BOOLEAN); if ($confirmation) { $this->checkCsrfToken(); $hookAreaPrefix = $entity->getHookAreaPrefix(); $hookType = 'validate_delete'; // Let any hooks perform additional validation actions $hook = new Zikula_ValidationHook($hookAreaPrefix . '.' . $hookType, new Zikula_Hook_ValidationProviders()); $validators = $this->notifyHooks($hook)->getValidators(); if (!$validators->hasErrors()) { // execute the workflow action $success = $workflowHelper->executeAction($entity, $deleteActionId); if ($success) { $this->registerStatus($this->__('Done! Item deleted.')); } // Let any hooks know that we have created, updated or deleted an item $hookType = 'process_delete'; $hook = new Zikula_ProcessHook($hookAreaPrefix . '.' . $hookType, $entity->createCompositeIdentifier()); $this->notifyHooks($hook); // An item was deleted, so we clear all cached pages this item. $cacheArgs = array('ot' => $objectType, 'item' => $entity); ModUtil::apiFunc($this->name, 'cache', 'clearItemCache', $cacheArgs); // redirect to the list of the current object type return $this->redirect(ModUtil::url($this->name, 'admin', 'view', array('ot' => $objectType))); } } $entityClass = $this->name . '_Entity_' . ucwords($objectType); $repository = $this->entityManager->getRepository($entityClass); // set caching id $this->view->setCaching(Zikula_View::CACHE_DISABLED); // assign the object we loaded above $this->view->assign($objectType, $entity)->assign($repository->getAdditionalTemplateParameters('controllerAction', $utilArgs)); // fetch and return the appropriate template $viewHelper = new Reviews_Util_View($this->serviceManager); return $viewHelper->processTemplate($this->view, 'admin', $objectType, 'delete', array()); }
/** * Determines object type using controller util methods. * * @param string $args['ot'] The object type to retrieve (optional). * @param string $methodName Name of calling method. * * @return string the object type. */ protected function determineObjectType(array $args = array(), $methodName = '') { $objectType = isset($args['ot']) ? $args['ot'] : ''; $controllerHelper = new Reviews_Util_Controller($this->serviceManager); $utilArgs = array('api' => 'selection', 'action' => $methodName); if (!in_array($objectType, $controllerHelper->getObjectTypes('api', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('api', $utilArgs); } return $objectType; }
/** * Checks whether a field value is a duplicate or not. * * @param string $ot Treated object type. * @param string $fn Name of field to be checked. * @param string $v The value to be checked for uniqueness. * @param string $ex Optional identifier to be excluded from search. * * @return Zikula_Response_Ajax */ public function checkForDuplicate() { $this->checkAjaxToken(); $this->throwForbiddenUnless(SecurityUtil::checkPermission($this->name . '::Ajax', '::', ACCESS_EDIT)); $postData = $this->request->request; $objectType = $postData->filter('ot', 'review', FILTER_SANITIZE_STRING); $controllerHelper = new Reviews_Util_Controller($this->serviceManager); $utilArgs = array('controller' => 'ajax', 'action' => 'checkForDuplicate'); if (!in_array($objectType, $controllerHelper->getObjectTypes('controllerAction', $utilArgs))) { $objectType = $controllerHelper->getDefaultObjectType('controllerAction', $utilArgs); } $fieldName = $postData->filter('fn', '', FILTER_SANITIZE_STRING); $value = $postData->get('v', ''); if (empty($fieldName) || empty($value)) { return new Zikula_Response_Ajax_BadData($this->__('Error: invalid input.')); } // check if the given field is existing and unique $uniqueFields = array(); switch ($objectType) { case 'review': $uniqueFields = array('slug'); break; } if (!count($uniqueFields) || !in_array($fieldName, $uniqueFields)) { return new Zikula_Response_Ajax_BadData($this->__('Error: invalid input.')); } $exclude = $postData->get('ex', ''); $entityClass = 'Reviews_Entity_' . ucfirst($objectType); $object = new $entityClass(); $result = false; switch ($objectType) { case 'review': $repository = $this->entityManager->getRepository($entityClass); switch ($fieldName) { case 'slug': $entity = $repository->selectBySlug($value, false, $exclude); $result = $entity != null && isset($entity['slug']); break; } break; } // return response $result = array('isDuplicate' => $result); return new Zikula_Response_Ajax($result); }
/** * Loads the data. * * @param array $data Data array with parameters. */ public function loadData(&$data) { $serviceManager = ServiceUtil::getManager(); $controllerHelper = new Reviews_Util_Controller($serviceManager); $utilArgs = array('name' => 'list'); if (!isset($data['objectType']) || !in_array($data['objectType'], $controllerHelper->getObjectTypes('contentType', $utilArgs))) { $data['objectType'] = $controllerHelper->getDefaultObjectType('contentType', $utilArgs); } $this->objectType = $data['objectType']; if (!isset($data['sorting'])) { $data['sorting'] = 'default'; } if (!isset($data['amount'])) { $data['amount'] = 1; } if (!isset($data['template'])) { $data['template'] = 'itemlist_' . $this->objectType . '_display.tpl'; } if (!isset($data['customTemplate'])) { $data['customTemplate'] = ''; } if (!isset($data['filter'])) { $data['filter'] = ''; } $this->sorting = $data['sorting']; $this->amount = $data['amount']; $this->template = $data['template']; $this->customTemplate = $data['customTemplate']; $this->filter = $data['filter']; $this->categorisableObjectTypes = array('review'); // fetch category properties $this->catRegistries = array(); $this->catProperties = array(); if (in_array($this->objectType, $this->categorisableObjectTypes)) { $idFields = ModUtil::apiFunc('Reviews', 'selection', 'getIdFields', array('ot' => $this->objectType)); $this->catRegistries = ModUtil::apiFunc('Reviews', 'category', 'getAllPropertiesWithMainCat', array('ot' => $this->objectType, 'arraykey' => $idFields[0])); $this->catProperties = ModUtil::apiFunc('Reviews', 'category', 'getAllProperties', array('ot' => $this->objectType)); } if (!isset($data['catIds'])) { $primaryRegistry = ModUtil::apiFunc('Reviews', 'category', 'getPrimaryProperty', array('ot' => $this->objectType)); $data['catIds'] = array($primaryRegistry => array()); // backwards compatibility if (isset($data['catId'])) { $data['catIds'][$primaryRegistry][] = $data['catId']; unset($data['catId']); } } elseif (!is_array($data['catIds'])) { $data['catIds'] = explode(',', $data['catIds']); } foreach ($this->catRegistries as $registryId => $registryCid) { $propName = ''; foreach ($this->catProperties as $propertyName => $propertyId) { if ($propertyId == $registryId) { $propName = $propertyName; break; } } if (isset($data['catids' . $propName])) { $data['catIds'][$propName] = $data['catids' . $propName]; } if (!is_array($data['catIds'][$propName])) { if ($data['catIds'][$propName]) { $data['catIds'][$propName] = array($data['catIds'][$propName]); } else { $data['catIds'][$propName] = array(); } } } $this->catIds = $data['catIds']; }