/** * Returns a value for $name * * @param string $name A name indentifying a value in this stack. * @return A value for $name */ public function lazyGet($name) { // \MUtil_Echo::track($name, isset($this->_object->$name), \MUtil_Lazy::rise($this->_object->$name), $this->_object->getLazyValue($name)); $value = $this->_object->__get($name); if ($value instanceof \MUtil_Lazy_LazyInterface) { return \MUtil_Lazy::rise($value); } return $value; }
/** * Return true when then $source does NOT contain all items in the parameterFilter * * @param \Gems_Menu_ParameterCollector $source * @param boolean $raiseConditions * @param mixed $condition * @return boolean */ private function _applyParameterFilter(\Gems_Menu_ParameterCollector $source, $raiseConditions, &$condition) { if ($this->_parameterFilter) { foreach ($this->_parameterFilter as $name => $testValue) { $paramValue = $source->getMenuParameter($name); if ($paramValue instanceof \MUtil_Lazy_LazyInterface) { if ($raiseConditions) { $paramValue = \MUtil_Lazy::rise($paramValue); } else { if (is_array($testValue)) { $newCondition = new \MUtil_Lazy_Call('in_array', array($paramValue, $testValue)); } else { $newCondition = \MUtil_Lazy::comp($testValue, '==', $paramValue); } if ($condition instanceof \MUtil_Lazy_LazyInterface) { if ($condition instanceof \MUtil_Lazy_LazyAnd) { $condition->add($newCondition); } else { $condition = new \MUtil_Lazy_LazyAnd($condition, $newCondition); } } else { $condition = $newCondition; } continue; } } if (is_array($testValue)) { if (!in_array($paramValue, $testValue)) { if (\Gems_Menu::$verbose) { // Mutil_Echo::backtrace(); \MUtil_Echo::r($name . ' => ' . print_r($testValue, true) . ' !== ' . $paramValue, $this->get('label') . ' (' . $this->get('controller') . '/' . $this->get('action') . ')'); } return true; } } else { if ($testValue !== $paramValue) { if (\Gems_Menu::$verbose) { // Mutil_Echo::backtrace(); \MUtil_Echo::r($name . ' => ' . $testValue . ' !== ' . $paramValue, $this->get('label') . ' (' . $this->get('controller') . '/' . $this->get('action') . ')'); } return true; } } } } }
/** * This is the actual format function, copied from the Exhibitor for field * * @param type $name * @param type $result *@param \MUtil_Model_ModelAbstract $model * @return type */ private function _format($name, $result, $model) { static $view = null; if (!isset($this->_options[$name])) { $this->_options[$name] = $model->get($name, array('default', 'multiOptions', 'formatFunction', 'dateFormat', 'storageFormat', 'itemDisplay')); } $options = $this->_options[$name]; foreach ($options as $key => $value) { switch ($key) { case 'default': if (is_null($result)) { $result = $value; } break; case 'multiOptions': $multiOptions = $value; if (is_array($multiOptions)) { /* * Sometimes a field is an array and will be formatted later on using the * formatFunction -> handle each element in the array. */ if (is_array($result)) { foreach ($result as $key => $value) { if (array_key_exists($value, $multiOptions)) { $result[$key] = $multiOptions[$value]; } } } else { if (array_key_exists($result, $multiOptions)) { $result = $multiOptions[$result]; } } } break; case 'formatFunction': $callback = $value; $result = call_user_func($callback, $result); break; case 'dateFormat': if (array_key_exists('formatFunction', $options)) { // if there is a formatFunction skip the date formatting continue; } $dateFormat = $value; $storageFormat = $model->get($name, 'storageFormat'); $result = \MUtil_Date::format($result, $dateFormat, $storageFormat); break; case 'itemDisplay': $function = $value; if (is_callable($function)) { $result = call_user_func($function, $result); } elseif (is_object($function)) { if ($function instanceof \MUtil_Html_ElementInterface || method_exists($function, 'append')) { $object = clone $function; $result = $object->append($result); } } elseif (is_string($function)) { // Assume it is a html tag when a string $result = \MUtil_Html::create($function, $result); } default: break; } } if (is_object($result)) { // If it is Lazy, execute it if ($result instanceof \MUtil_Lazy_LazyInterface) { $result = \MUtil_Lazy::rise($result); } // If it is Html, render it if ($result instanceof \MUtil_Html_HtmlInterface) { if (is_null($view)) { $viewRenderer = \Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); if (null === $viewRenderer->view) { $viewRenderer->initView(); } $view = $viewRenderer->view; } $result = $result->render($view); } } return $result; }
/** * Converts an associative array to a string of tag attributes. * * @access public * * @param array $attribs From this array, each key-value pair is * converted to an attribute name and value. * * @return string The XHTML for the attributes. */ protected function _htmlAttribs($attribs) { if (isset($attribs['src'])) { $filename = \MUtil_Lazy::rise($attribs['src']); if ($dir = self::getImageDir($filename)) { if (!isset($attribs['width'], $attribs['height'])) { try { $info = getimagesize(self::getWebRoot() . $dir . $filename); if (!isset($attribs['width'])) { $attribs['width'] = $info[0]; } if (!isset($attribs['height'])) { $attribs['height'] = $info[1]; } } catch (\Exception $e) { if (\MUtil_Html::$verbose) { \MUtil_Echo::r($e, __CLASS__ . '->' . __FUNCTION__); } } } $attribs['src'] = $this->view->baseUrl() . $dir . $filename; } // \MUtil_Echo::r($attribs['src']); } return parent::_htmlAttribs($attribs); }
/** * Mark the searches in $value * * @param mixed $value Lazy, Html, Raw or string * @return \MUtil_Html_Raw */ public function mark($value) { if (!$this->_replaces) { // Late setting of search & replaces $searches = $this->_searches; $this->_searches = array(); // Do not use the $tag itself here: str_replace will then replace // the text of tag itself on later finds $topen = '<' . self::TAG_MARKER . '>'; $tclose = '</' . self::TAG_MARKER . '>'; foreach ((array) $searches as $search) { $searchHtml = $this->escape($search); $this->_searches[] = $searchHtml; $this->_replaces[] = $topen . $searchHtml . $tclose; } } if ($value instanceof \MUtil_Lazy_LazyInterface) { $value = \MUtil_Lazy::rise($value); } if ($value instanceof \MUtil_Html_Raw) { $values = array(); // Split into HTML Elements foreach ($value->getElements() as $element) { if (strlen($element)) { switch ($element[0]) { case '<': case '&': // No replace in element $values[] = $element; break; default: $values[] = $this->_findTags($element); } } } // \MUtil_Echo::r($values); return $value->setValue($this->_fillTags(implode('', $values))); } elseif ($value instanceof \MUtil_Html_HtmlElement) { foreach ($value as $key => $item) { // \MUtil_Echo::r($key); $value[$key] = $this->mark($item); } return $value; } elseif ($value || $value === 0) { // \MUtil_Echo::r($value); $valueHtml = $this->escape($value); $valueTemp = $this->_findTags($valueHtml); return new \MUtil_Html_Raw($this->_fillTags($valueTemp)); } }
protected function filterHtml($result) { if ($result instanceof \MUtil_Html_ElementInterface && !$result instanceof \MUtil_Html_Sequence) { if ($result instanceof \MUtil_Html_AElement) { $href = $result->href; $result = $href; } elseif ($result->count() > 0) { $result = $result[0]; } } if (is_object($result)) { // If it is Lazy, execute it if ($result instanceof \MUtil_Lazy_LazyInterface) { $result = \MUtil_Lazy::rise($result); } // If it is Html, render it if ($result instanceof \MUtil_Html_HtmlInterface) { $viewRenderer = \Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); if (null === $viewRenderer->view) { $viewRenderer->initView(); } $view = $viewRenderer->view; $result = $result->render($view); } } return $result; }
private static function _checkElement($element, $function) { if ($element instanceof \MUtil_Lazy_LazyInterface) { $element = \MUtil_Lazy::rise($element); } if ($element instanceof \Zend_Form_Element || $element instanceof \Zend_Form_DisplayGroup || $element instanceof \Zend_Form) { return $element; } throw new \MUtil_Html_HtmlException(sprintf(self::ARGUMENT_ERROR, get_class($element), __CLASS__ . '::' . $function . '()')); }
/** * Function to allow overloading of tag rendering only * * Renders the element tag with it's content into a html string * * The $view is used to correctly encode and escape the output * * @param \Zend_View_Abstract $view * @return string Correctly encoded and escaped html output */ protected function renderElement(\Zend_View_Abstract $view) { $this->_currentContent = array(); // If the label was assigned an element lazy, // now is the time to get it's value. foreach ($this->_content as $key => $value) { if ($value instanceof \MUtil_Lazy_LazyInterface) { $value = \MUtil_Lazy::rise($value); } if ($value instanceof \Zend_Form_Element) { if ($value instanceof \Zend_Form_Element_Hidden) { return null; } // Only a label when a label decorator exists, but we do not use that decorator $decorator = $value->getDecorator('Label'); if ($decorator) { if (false === $decorator->getOption('escape')) { $label = \MUtil_Html::raw($value->getLabel()); } else { $label = $value->getLabel(); } $class = $this->class ? \MUtil_Html::renderAny($view, $this->class) . ' ' : ''; if ($value->isRequired()) { $class .= $this->getRequiredClass(); $this->_currentContent[$key] = array($this->getRequiredPrefix(), $label, $this->getRequiredPostfix()); } else { $class .= $this->getOptionalClass(); $this->_currentContent[$key] = array($this->getOptionalPrefix(), $label, $this->getOptionalPostfix()); } parent::__set('class', $class); // Bypass existing property for drawing if ($id = $value->getId()) { parent::__set('for', $id); // Always overrule } else { parent::__unset('for'); } } } elseif ($value instanceof \Zend_Form_DisplayGroup) { return null; } else { $this->_currentContent[$key] = $value; } } return parent::renderElement($view); }
/** * Gets one or more values for a certain field name. * * Usage example, with these values set: * <code> * $this->set('field_x', 'label', 'label_x', 'size', 100, 'maxlength', 120, 'xyz', null); * </code> * * Retrieve one attribute: * <code> * $label = $this->get('field_x', 'label'); * </code> * Returns the label 'label_x' * * Retrieve another attribute: * <code> * $label = $this->get('field_x', 'xyz'); * </code> * Returns null * * Retrieve all attributes: * <code> * $fieldx = $this->get('field_x'); * </code> * Returns array('label' => 'label_x', 'size' => 100, 'maxlength' => 120) * Note: null value 'xyz' is not returned. * * Two options for retrieving specific attributes: * <code> * $list = $this->get('field_x', 'label', 'size', 'xyz'); * $list = $this->get('field_x' array('label', 'size', 'xyz')); * </code> * Both return array('label' => 'label_x', 'size' => 100) * Note: null value 'xyz' is not returned. * * @param string $name Field name * @param string|array|null $arrayOrKey1 Null or the name of a single attribute or an array of attribute names * @param string $key2 Optional a second attribute name. * @return mixed */ public function get($name, $arrayOrKey1 = null, $key2 = null) { $args = func_get_args(); $args = \MUtil_Ra::args($args, 1); if ($this->_model_used) { $this->_model_used[$name] = $name; } switch (count($args)) { case 0: if (isset($this->_model[$name])) { if ($this->_model[$name] instanceof \MUtil_Lazy_LazyInterface) { $result = \MUtil_Lazy::rise($this->_model[$name]); } else { $result = $this->_model[$name]; } if ($alias = $this->getAlias($name)) { $result = $result + $this->get($alias); } return $result; } else { return array(); } case 1: return $this->_getKeyValue($name, reset($args)); default: $results = array(); foreach ($args as $key) { $value = $this->_getKeyValue($name, $key); if (null !== $value) { $results[$key] = $value; } } return $results; } }