/** * @inheritdoc */ protected function buildNavigationData($mode) { $result = array(); $itemLimit = intval($this->config('navigation.item-limit')); $dateFormat = $this->config('common.date-format'); foreach ($this->getRepository('Item')->findLatestItems($itemLimit) as $item) { /** @var \Sitegear\Module\News\Model\Item $item */ $values = array('headline' => $item->getHeadline(), 'datePublished' => $item->getDatePublished()->format($dateFormat)); $result[] = array('url' => $this->getRouteUrl('item', $item->getUrlPath()), 'label' => StringUtilities::replaceTokens($this->config('navigation.item-link.label'), $values), 'tooltip' => StringUtilities::replaceTokens($this->config('navigation.item-link.tooltip'), $values)); } if ($this->config('navigation.all-news-link.display')) { $result[] = array('url' => sprintf('%s?more=1', $this->getRouteUrl('index')), 'label' => $this->config('navigation.all-news-link.label'), 'tooltip' => $this->config('navigation.all-news-link.tooltip')); } return $result; }
public function testReplaceTokens() { // Base case $this->assertEquals('', StringUtilities::replaceTokens('', array())); // Base case with ignored tokens $this->assertEquals('', StringUtilities::replaceTokens('', array('foo' => 'bar', 'baz' => 'xyzzy'))); // No change for no tokens. $this->assertEquals('This %foo% has 3 %bar% and is %baz%', StringUtilities::replaceTokens('This %foo% has 3 %bar% and is %baz%', array())); // Normal case $this->assertEquals('This sentence has 3 tokens and is well tested', StringUtilities::replaceTokens('This %foo% has 3 %bar% and is %baz%', array('foo' => 'sentence', 'bar' => 'tokens', 'baz' => 'well tested'))); $this->assertEquals('This car has 3 turbos and is stupidly fast', StringUtilities::replaceTokens('This %foo% has 3 %bar% and is %baz%', array('foo' => 'car', 'bar' => 'turbos', 'baz' => 'stupidly fast'))); // Repeated tokens. $this->assertEquals('This sentence has 2 tokens. This sentence has 2 tokens.', StringUtilities::replaceTokens('This %foo% has 2 %bar%. This %foo% has 2 %bar%.', array('foo' => 'sentence', 'bar' => 'tokens'))); $this->assertEquals('This sentence has 2 tokens. 2 tokens has this sentence.', StringUtilities::replaceTokens('This %foo% has 2 %bar%. 2 %bar% has this %foo%.', array('foo' => 'sentence', 'bar' => 'tokens'))); // Ignored tokens. $this->assertEquals('This %foo% has 3 tokens and is well tested', StringUtilities::replaceTokens('This %foo% has 3 %bar% and is %baz%', array('foo' => array('ignored' => 'sentence'), 'bar' => 'tokens', 'baz' => 'well tested'))); }
/** * @inheritdoc */ public function getAdjustmentLabel() { $rateLabel = sprintf('%s%%', $this->config('rate')); return StringUtilities::replaceTokens($this->config('label'), array('rate' => $rateLabel)); }
/** * Recursive method for navigation generation. * * @param int $mode * @param int $maxDepth * @param null|int|\Sitegear\Module\Locations\Model\Region $parent * * @return array */ private function buildNavigationDataImpl($mode, $maxDepth, $parent = null) { $result = array(); foreach ($this->getRepository('Region')->findByParent($parent) as $region) { /** @var \Sitegear\Module\Locations\Model\Region $region */ $tooltip = StringUtilities::replaceTokens($this->config('navigation.tooltip-format'), array('regionName' => $region->getName())); $regionResult = array('url' => $this->getRouteUrl('region', $region->getUrlPath()), 'label' => $region->getName(), 'tooltip' => $tooltip); if ($mode === self::NAVIGATION_DATA_MODE_EXPANDED || $maxDepth !== 1) { // >1 means more levels before the limit is reached, <=0 means no limit $subRegions = $this->buildNavigationDataImpl($mode, max(0, $maxDepth - 1), $region); if (!empty($subRegions)) { $regionResult['children'] = $subRegions; } } $result[] = $regionResult; } return $result; }
/** * Create the values array for the given attribute. * * @param array $attribute * @param string $noValueOption * @param string $valueFormat * * @return array */ private function buildAddTrolleyItemFormAttributeFieldValues(array $attribute, $noValueOption, $valueFormat) { $values = array(); // Add the 'no value' value. if (!is_null($noValueOption)) { $values[] = array('value' => '', 'label' => $noValueOption); } // Add the other values. foreach ($attribute['values'] as $value) { $label = StringUtilities::replaceTokens($valueFormat, array('value' => sprintf('$%s', number_format($value['value'] / 100, 2)), 'label' => $value['label'])); $values[] = array('value' => strval($value['id']), 'label' => $label); } return $values; }
/** * Recursive method for navigation generation. * * @param int $mode * @param int $maxDepth * @param null|int|\Sitegear\Module\Products\Model\Category $parent * * @return array */ private function buildNavigationDataImpl($mode, $maxDepth, $parent = null) { $result = array(); foreach ($this->getRepository('Category')->findByParent($parent) as $category) { /** @var \Sitegear\Module\Products\Model\Category $category */ $tooltip = \Sitegear\Util\StringUtilities::replaceTokens($this->config('navigation.tooltip'), array('categoryName' => $category->getName())); $categoryResult = array('url' => $this->getRouteUrl('category', $category->getUrlPath()), 'label' => $category->getName(), 'tooltip' => $tooltip); if ($mode === self::NAVIGATION_DATA_MODE_EXPANDED || $maxDepth !== 1) { // >1 means more levels before the limit is reached, <=0 means no limit $subCategories = $this->buildNavigationDataImpl($mode, max(0, $maxDepth - 1), $category); if (!empty($subCategories)) { $categoryResult['children'] = $subCategories; } } $result[] = $categoryResult; } return $result; }
/** * Set the quantity of the trolley item at the given index. The quantity must be greater than zero. * * @param $index * @param $quantity * * @throws \DomainException * @throws \OutOfBoundsException */ public function modifyItem($index, $quantity) { LoggerRegistry::debug('Trolley::modifyItem({index}, {quantity})', array('index' => TypeUtilities::describe($index), 'quantity' => TypeUtilities::describe($quantity))); if ($quantity < 1) { throw new \DomainException('Trolley cannot modify trolley item to a zero or negative quantity; use removeTrolleyItem instead.'); } $data = $this->getData(); if ($index < 0 || $index >= sizeof($data)) { throw new \OutOfBoundsException(sprintf('Trolley cannot modify trolley item with index (%d) out-of-bounds', $index)); } // Modify the session data. $item = $data[$index]; /** @var TransactionItem $item */ $item->setQuantity($quantity); $data[$index] = $item; $this->setData($data); // Notify on next page load. $this->customerModule->getEngine()->pageMessages()->add(StringUtilities::replaceTokens($this->messages['item-modified'], array('label' => $item->getLabel(), 'quantity' => $quantity)), 'success'); }
/** * Perform a login recovery action for the given email address. * * @param string $email * * @throws \InvalidArgumentException */ public function recoverLogin($email) { LoggerRegistry::debug('UserIntegrationModule::recoverLogin({email})', array('email' => TypeUtilities::describe($email))); // TODO Implement token storage $token = TokenGenerator::generateToken(); $url = $this->getRouteUrl('recover-login-token', array('token' => $token)); $siteInfo = $this->getEngine()->getSiteInfo(); $subject = sprintf('Login Recovery from %s', $siteInfo->getDisplayName()); $addresses = array('sender' => $siteInfo->getAdministratorEmail(), 'to' => $email); $data = array('name' => $siteInfo->getDisplayName(), 'adminName' => $siteInfo->getAdministratorName(), 'adminEmail' => $siteInfo->getAdministratorEmail(), 'email' => $email, 'url' => $url); $type = $this->config('recover-login.notification.type'); $contentType = $this->config('recover-login.notification.content-type'); $content = $this->config('recover-login.notification.content'); switch ($type) { case 'tokens': $body = StringUtilities::replaceTokens($content, $data); $this->getEngine()->swiftMailer()->send($subject, $addresses, $body, $contentType); break; case 'template': $this->getEngine()->swiftMailer()->sendTemplate(new Request(), $subject, $addresses, $content, $data, $contentType); break; default: throw new \InvalidArgumentException(sprintf('Could not process login recovery due to invalid configuration: invalid email type "%s" specified', $type)); } $this->getEngine()->pageMessages()->add($this->config('recover-login.messages.success'), 'success'); }