/** * Constructor * * Grab local copies of various MVC objects * * @return void */ public function __construct() { $front = \Zend\Controller\Front::getInstance(); $modules = $front->getControllerDirectory(); if (empty($modules)) { $e = new View\Exception('Action helper depends on valid front controller instance'); $e->setView($this->view); throw $e; } $request = $front->getRequest(); $response = $front->getResponse(); if (empty($request) || empty($response)) { $e = new View\Exception('Action view helper requires both a registered request and response object in the front controller instance'); $e->setView($this->view); throw $e; } $this->request = clone $request; $this->response = clone $response; $this->dispatcher = clone $front->getDispatcher(); $this->defaultModule = $front->getDefaultModule(); }
/** * Sets ACL role(s) to use when iterating pages * * Implements {@link Zend_View_Helper_Navigation_Helper::setRole()}. * * @param mixed $role [optional] role to * set. Expects a string, * an instance of type * {@link Zend_Acl_Role_Interface}, * or null. Default is * null, which will set * no role. * @throws \Zend\View\Exception if $role is invalid * @return \Zend\View\Helper\Navigation\HelperAbstract fluent interface, * returns self */ public function setRole($role = null) { if (null === $role || is_string($role) || $role instanceof Acl\Role) { $this->_role = $role; } else { $e = new View\Exception(sprintf('$role must be a string, null, or an instance of ' . 'Zend_Acl_Role_Interface; %s given', gettype($role))); $e->setView($this->view); throw $e; } return $this; }
/** * Renders the given $container by invoking the partial view helper * * The container will simply be passed on as a model to the view script, * so in the script it will be available in <code>$this->container</code>. * * @param \Zend\Navigation\Container $container [optional] container to * pass to view script. * Default is to use the * container registered in the * helper. * @param string|array $partial [optional] partial view * script to use. Default is * to use the partial * registered in the helper. * If an array is given, it is * expected to contain two * values; the partial view * script to use, and the * module where the script can * be found. * @return string helper output */ public function renderPartial(Container $container = null, $partial = null) { if (null === $container) { $container = $this->getContainer(); } if (null === $partial) { $partial = $this->getPartial(); } if (empty($partial)) { $e = new View\Exception( 'Unable to render menu: No partial view script provided' ); $e->setView($this->view); throw $e; } // put breadcrumb pages in model $model = array('pages' => array()); if ($active = $this->findActive($container)) { $active = $active['page']; $model['pages'][] = $active; while ($parent = $active->getParent()) { if ($parent instanceof AbstractPage) { $model['pages'][] = $parent; } else { break; } if ($parent === $container) { // break if at the root of the given container break; } $active = $parent; } $model['pages'] = array_reverse($model['pages']); } if (is_array($partial)) { if (count($partial) != 2) { $e = new View\Exception( 'Unable to render menu: A view partial supplied as ' . 'an array must contain two values: partial view ' . 'script and module where script can be found' ); $e->setView($this->view); throw $e; } return $this->view->broker('partial')->direct($partial[0], $partial[1], $model); } return $this->view->broker('partial')->direct($partial, null, $model); }
/** * Create item for alternate link item * * @param array $args * @return stdClass */ public function createDataAlternate(array $args) { if (3 > count($args)) { $e = new View\Exception(sprintf('Alternate tags require 3 arguments; %s provided', count($args))); $e->setView($this->view); throw $e; } $rel = 'alternate'; $href = array_shift($args); $type = array_shift($args); $title = array_shift($args); if (0 < count($args) && is_array($args[0])) { $extras = array_shift($args); $extras = (array) $extras; if (isset($extras['media']) && is_array($extras['media'])) { $extras['media'] = implode(',', $extras['media']); } } $href = (string) $href; $type = (string) $type; $title = (string) $title; $attributes = compact('rel', 'href', 'type', 'title', 'extras'); return $this->createData($attributes); }
/** * Returns a DOMDocument containing the Sitemap XML for the given container * * @param \Zend\Navigation\Container $container [optional] container to get * breadcrumbs from, defaults * to what is registered in the * helper * @return DOMDocument DOM representation of the * container * @throws \Zend\View\Exception if schema validation is on * and the sitemap is invalid * according to the sitemap * schema, or if sitemap * validators are used and the * loc element fails validation */ public function getDomSitemap(Container $container = null) { if (null === $container) { $container = $this->getContainer(); } // check if we should validate using our own validators if ($this->getUseSitemapValidators()) { // create validators $locValidator = new \Zend\Validator\Sitemap\Loc(); $lastmodValidator = new \Zend\Validator\Sitemap\Lastmod(); $changefreqValidator = new \Zend\Validator\Sitemap\Changefreq(); $priorityValidator = new \Zend\Validator\Sitemap\Priority(); } // create document $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = $this->getFormatOutput(); // ...and urlset (root) element $urlSet = $dom->createElementNS(self::SITEMAP_NS, 'urlset'); $dom->appendChild($urlSet); // create iterator $iterator = new \RecursiveIteratorIterator($container, \RecursiveIteratorIterator::SELF_FIRST); $maxDepth = $this->getMaxDepth(); if (is_int($maxDepth)) { $iterator->setMaxDepth($maxDepth); } $minDepth = $this->getMinDepth(); if (!is_int($minDepth) || $minDepth < 0) { $minDepth = 0; } // iterate container foreach ($iterator as $page) { if ($iterator->getDepth() < $minDepth || !$this->accept($page)) { // page should not be included continue; } // get absolute url from page if (!($url = $this->url($page))) { // skip page if it has no url (rare case) continue; } // create url node for this page $urlNode = $dom->createElementNS(self::SITEMAP_NS, 'url'); $urlSet->appendChild($urlNode); if ($this->getUseSitemapValidators() && !$locValidator->isValid($url)) { $e = new View\Exception(sprintf('Encountered an invalid URL for Sitemap XML: "%s"', $url)); $e->setView($this->view); throw $e; } // put url in 'loc' element $urlNode->appendChild($dom->createElementNS(self::SITEMAP_NS, 'loc', $url)); // add 'lastmod' element if a valid lastmod is set in page if (isset($page->lastmod)) { $lastmod = strtotime((string) $page->lastmod); // prevent 1970-01-01... if ($lastmod !== false) { $lastmod = date('c', $lastmod); } if (!$this->getUseSitemapValidators() || $lastmodValidator->isValid($lastmod)) { $urlNode->appendChild($dom->createElementNS(self::SITEMAP_NS, 'lastmod', $lastmod)); } } // add 'changefreq' element if a valid changefreq is set in page if (isset($page->changefreq)) { $changefreq = $page->changefreq; if (!$this->getUseSitemapValidators() || $changefreqValidator->isValid($changefreq)) { $urlNode->appendChild($dom->createElementNS(self::SITEMAP_NS, 'changefreq', $changefreq)); } } // add 'priority' element if a valid priority is set in page if (isset($page->priority)) { $priority = $page->priority; if (!$this->getUseSitemapValidators() || $priorityValidator->isValid($priority)) { $urlNode->appendChild($dom->createElementNS(self::SITEMAP_NS, 'priority', $priority)); } } } // validate using schema if specified if ($this->getUseSchemaValidation()) { if (!@$dom->schemaValidate(self::SITEMAP_XSD)) { $e = new View\Exception(sprintf('Sitemap is invalid according to XML Schema at "%s"', self::SITEMAP_XSD)); $e->setView($this->view); throw $e; } } return $dom; }
/** * Override offsetSet * * @param string|int $index Set script of file offset * @param mixed $value * @return void */ public function offsetSet($index, $value) { if (!$this->_isValid($value)) { $e = new View\Exception('Invalid argument passed to offsetSet(); please use one of the helper methods, offsetSetScript() or offsetSetFile()'); $e->setView($this->view); throw $e; } $this->_isValid($value); return $this->getContainer()->offsetSet($index, $value); }
/** * Finds a view script from the available directories. * * @param $name string The base name of the script. * @return void */ protected function _script($name) { if ($this->isLfiProtectionOn() && preg_match('#\\.\\.[\\\\/]#', $name)) { $e = new Exception('Requested scripts may not include parent directory traversal ("../", "..\\" notation)'); $e->setView($this); throw $e; } if (0 == count($this->_path['script'])) { $e = new Exception('no view script directory set; unable to determine location for view script'); $e->setView($this); throw $e; } foreach ($this->_path['script'] as $dir) { if (is_readable($dir . $name)) { return $dir . $name; } } $message = "script '{$name}' not found in path (" . implode(PATH_SEPARATOR, $this->_path['script']) . ")"; $e = new Exception($message); $e->setView($this); throw $e; }
/** * Returns the set locale for translations * * @throws Zend_View_Exception When no \Zend\Translator\Translator instance was set * @return string|\Zend\Locale\Locale */ public function getLocale() { $translate = $this->getTranslator(); if ($translate === null) { $e = new View\Exception('You must set an instance of Zend_Translate or Zend_Translate_Adapter'); $e->setView($this->view); throw $e; } return $translate->getLocale(); }
/** * Render the provided pages. This checks if $view->paginator is set and, * if so, uses that. Also, if no scrolling style or partial are specified, * the defaults will be used (if set). * * @param \Zend\Paginator\Paginator (Optional) $paginator * @param string $scrollingStyle (Optional) Scrolling style * @param string $partial (Optional) View partial * @param array|string $params (Optional) params to pass to the partial * @return string * @throws \Zend\View\Exception */ public function direct(Paginator\Paginator $paginator = null, $scrollingStyle = null, $partial = null, $params = null) { if ($paginator === null) { if (isset($this->view->paginator) and $this->view->paginator !== null and $this->view->paginator instanceof Paginator\Paginator) { $paginator = $this->view->paginator; } else { $e = new View\Exception('No paginator instance provided or incorrect type'); $e->setView($this->view); throw $e; } } if ($partial === null) { if (self::$_defaultViewPartial === null) { $e = new View\Exception('No view partial provided and no default set'); $e->setView($this->view); throw $e; } $partial = self::$_defaultViewPartial; } $pages = get_object_vars($paginator->getPages($scrollingStyle)); if ($params !== null) { $pages = array_merge($pages, (array) $params); } if (is_array($partial)) { if (count($partial) != 2) { $e = new View\Exception('A view partial supplied as an array must contain two values: the filename and its module'); $e->setView($this->view); throw $e; } if ($partial[1] !== null) { return $this->view->partial($partial[0], $partial[1], $pages); } $partial = $partial[0]; } return $this->view->partial($partial, $pages); }
/** * Renders the given $container by invoking the partial view helper * * The container will simply be passed on as a model to the view script * as-is, and will be available in the partial script as 'container', e.g. * <code>echo 'Number of pages: ', count($this->container);</code>. * * @param \Zend\Navigation\Container $container [optional] container to * pass to view script. Default * is to use the container * registered in the helper. * @param string|array $partial [optional] partial view * script to use. Default is to * use the partial registered * in the helper. If an array * is given, it is expected to * contain two values; the * partial view script to use, * and the module where the * script can be found. * @return string helper output */ public function renderPartial(Container $container = null, $partial = null) { if (null === $container) { $container = $this->getContainer(); } if (null === $partial) { $partial = $this->getPartial(); } if (empty($partial)) { $e = new View\Exception('Unable to render menu: No partial view script provided'); $e->setView($this->view); throw $e; } $model = array('container' => $container); if (is_array($partial)) { if (count($partial) != 2) { $e = new View\Exception('Unable to render menu: A view partial supplied as ' . 'an array must contain two values: partial view ' . 'script and module where script can be found'); $e->setView($this->view); throw $e; } return $this->view->partial($partial[0], $partial[1], $model); } return $this->view->partial($partial, null, $model); }
/** * Sets a translation Adapter for translation * * @param Zend_Translator|\Zend\Translator\Adapter\Adapter $translate * @return \Zend\View\Helper\HeadTitle */ public function setTranslator($translate) { if ($translate instanceof \Zend\Translator\Adapter) { $this->_translator = $translate; } elseif ($translate instanceof \Zend\Translator\Translator) { $this->_translator = $translate->getAdapter(); } else { $e = new \Zend\View\Exception("You must set an instance of Zend_Translator or Zend_Translator_Adapter"); $e->setView($this->view); throw $e; } return $this; }
/** * Renders the given $page as a link element, with $attrib = $relation * * @param \Zend\Navigation\AbstractPage $page the page to render the link for * @param string $attrib the attribute to use for $type, * either 'rel' or 'rev' * @param string $relation relation type, muse be one of; * alternate, appendix, bookmark, * chapter, contents, copyright, * glossary, help, home, index, next, * prev, section, start, stylesheet, * subsection * @return string rendered link element * @throws \Zend\View\Exception if $attrib is invalid */ public function renderLink(AbstractPage $page, $attrib, $relation) { if (!in_array($attrib, array('rel', 'rev'))) { $e = new View\Exception(sprintf( 'Invalid relation attribute "%s", must be "rel" or "rev"', $attrib)); $e->setView($this->view); throw $e; } if (!$href = $page->getHref()) { return ''; } // TODO: add more attribs // http://www.w3.org/TR/html401/struct/links.html#h-12.2 $attribs = array( $attrib => $relation, 'href' => $href, 'title' => $page->getLabel() ); return '<link' . $this->_htmlAttribs($attribs) . $this->getClosingBracket(); }
/** * Build meta HTML string * * @param string $type * @param string $typeValue * @param string $content * @param array $modifiers * @return string */ public function itemToString(\stdClass $item) { if (!in_array($item->type, $this->_typeKeys)) { $e = new View\Exception(sprintf('Invalid type "%s" provided for meta', $item->type)); $e->setView($this->view); throw $e; } $type = $item->type; $modifiersString = ''; foreach ($item->modifiers as $key => $value) { if ($this->view->broker('doctype')->isHtml5() && $key == 'scheme') { throw new View\Exception('Invalid modifier ' . '"scheme" provided; not supported by HTML5'); } if (!in_array($key, $this->_modifierKeys)) { continue; } $modifiersString .= $key . '="' . $this->_escape($value) . '" '; } if (method_exists($this->view, 'broker')) { if ($this->view->broker('doctype')->isHtml5() && $type == 'charset') { $tpl = $this->view->broker('doctype')->isXhtml() ? '<meta %s="%s"/>' : '<meta %s="%s">'; } elseif ($this->view->broker('doctype')->isXhtml()) { $tpl = '<meta %s="%s" content="%s" %s/>'; } else { $tpl = '<meta %s="%s" content="%s" %s>'; } } else { $tpl = '<meta %s="%s" content="%s" %s/>'; } $meta = sprintf($tpl, $type, $this->_escape($item->{$type}), $this->_escape($item->content), $modifiersString); return $meta; }
/** * Override set to enforce style creation * * @param mixed $value * @return void */ public function set($value) { if (!$this->_isValid($value)) { $e = new View\Exception('Invalid value passed to set; please use setStyle()'); $e->setView($this->view); throw $e; } return $this->getContainer()->set($value); }