/** * Get this Writer's Twig_Environment; create if it doesn't exist; * * @return \Twig_Environment */ protected function getEnvironment() { if ($this->environment === null) { $loader = new \Twig_Loader_Filesystem($this->getTemplatesDirectories()); $this->environment = new \Twig_Environment($loader); $filter = new Twig_SimpleFilter('write', function (WritableInterface $host) { return $host->accept($this); }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('slugify', function ($string) { return StringUtils::slugify($string); }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('html_attribute', function ($string) { return preg_replace(array('/[^a-zA-Z0-9 -]/', '/^-|-$/'), array('', ''), $string); }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('md5', function ($string) { return md5($string); }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('stripForm', function ($string) { $string = str_replace("<form", "<div", $string); $string = preg_replace('#<div class="form-actions">(.*?)</div>#', '', $string); $string = str_replace("form-actions", "form-actions hidden", $string); $string = str_replace(" form-errors", " form-errors hidden", $string); $string = str_replace('"form-errors', '"form-errors hidden', $string); $string = str_replace("</form>", "</div>", $string); return $string; }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('saferaw', function ($string) { if ($string instanceof SafeString) { $string = (string) $string; } else { $string = htmlentities($string); } return $string; }); $this->environment->addFilter($filter); $filter = new Twig_SimpleFilter('writedata', function ($data) { $string = ' '; foreach ($data as $key => $value) { $key = htmlentities($key); $value = htmlentities($value); $string .= "data-{$key}=\"{$value}\" "; } return trim($string); }); $this->environment->addFilter($filter); $requestURI = array_key_exists("REQUEST_URI", $_SERVER) === true ? $_SERVER["REQUEST_URI"] : ""; $this->environment->addGlobal("requestURI", $requestURI); } return $this->environment; }
public function testFormatDollars() { $number = "2000.1"; $this->assertEquals('$2,000.10', StringUtils::formatDollars($number)); }
/** * @return WritableInterface * @throws \Exception If object id not provided, or object not found. */ protected function makeDetail() { /** @var boolean $idWasProvided */ $idWasProvided = static::getObjectId() !== null; /** @var ObjectWrapperInterface $object */ $object = $this->getObjectOr404(true); /** @var string $className */ $className = $this->getQuery()->getTitleCasedObjectName(); /** @var string $tableName */ $tableName = StringUtils::slugify($className); /** @var FormActionInterface $submitAction */ $submitAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Save')->addClass('save')->setTarget("\n athens.ajax.submitForm(\$(this).closest('form')[0], function(){\n athens.multi_panel.closePanel(1);\n athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n });\n return false;\n ")->build(); /** @var string $deleteUrl */ $deleteUrl = static::makeUrl([static::MODE_FIELD => static::MODE_DELETE, static::QUERY_INDEX_FIELD => $this->getQueryIndex(), static::OBJECT_ID_FIELD => $object->getPrimaryKey()]); /** @var FormActionInterface $deleteAction */ $deleteAction = FormActionBuilder::begin()->setType(FormActionBuilder::TYPE_JAVASCRIPT)->setLabel('Delete')->setTarget("\n if (confirm('Are you sure you want to delete this object?')) {\n athens.alert.makeAlert('Deleting object.', 'info');\n athens.ajax.post(\n '{$deleteUrl}',\n {},\n function() {},\n function() {\n athens.alert.makeAlert('Object deleted', 'success');\n athens.multi_panel.closePanel(1);\n athens.ajax_section.loadSection('object-manager-table-{$tableName}');\n }\n );\n }\n return false;\n ")->build(); $formBuilder = FormBuilder::begin()->setId('object-manager-detail-form')->addObject($object)->removeWritable($object->getPascalCasedObjectName() . '.Id')->addAction($submitAction); if ($idWasProvided === true) { $formBuilder->addAction($deleteAction); } $sectionBuilder = SectionBuilder::begin()->setId('object-manager-detail-form-wrapper')->addLabel($className)->addWritable($formBuilder->build()); return $sectionBuilder->build(); }
/** * Provides a slug representation of the field's textual label. * * @return string */ public function getLabelSlug() { return StringUtils::slugify($this->label); }
public function testSlugifyFilter() { $writer = new MockHTMLWriter(); $env = $writer->getEnvironment(); $template = "{{ var|slugify }}"; $var = "^a#%5m4ll3r^^7357!@ 57r1n6"; // Render the unsafe string $result = $env->createTemplate($template)->render(["var" => $var]); $this->assertEquals(StringUtils::slugify($var), $result); }