protected function _makeDisplayGroup(Centurion_Form $form, $data, $class) { if (isset($data['noFieldset']) && $data['noFieldset'] === true) { $form->addInDisplayGroup($data['elements'], $class, array('class' => 'form-' . substr($class, 1))); foreach ($data['elements'] as $key => $element) { if (null !== ($element = $form->getElement($element))) { $element->setLabel(null); $element->removeDecorator('label'); } } return true; } if (!isset($data['label'])) { $name = uniqid(); } else { $name = Centurion_Inflector::slugify($data['label']); } $name = $name . '_group'; $form->addDisplayGroup($data['elements'], $name, array('class' => 'form-group')); $displayGroup = $form->getDisplayGroup($name); if (isset($data['label']) && is_string($data['label'])) { $displayGroup->setLegend($data['label']); if (isset($data['description'])) { $displayGroup->setDescription($data['description']); } } else { $displayGroup->setDescription(' '); } $form->addInDisplayGroup(array($name), $class, array('class' => 'form-' . substr($class, 1))); }
public function preSave() { $slugParts = $this->_row->getSlugifyName(); $slugifiedParts = array(); if (count(array_intersect($this->_row->getModifiedFields(), (array) $slugParts)) || $this->isNew()) { foreach ((array) $slugParts as $part) { $slugifiedParts[] = Centurion_Inflector::slugify($this->_row->{$part}); } $slug = implode('-', $slugifiedParts); $currentSlug = $this->_row->slug; if ($separatorPos = strpos($this->_row->slug, '_')) { $currentSlug = substr($currentSlug, 0, $separatorPos); } $name = $this->_row->getTable()->info('name'); $select = $this->getTable()->select(); $filters = array(); if (method_exists($this->_row, 'getFilterFieldsForSlug')) { $filterField = $this->_row->getFilterFieldsForSlug(); if (is_array($filterField)) { foreach ($filterField as $field) { $filters[$field] = $this->_row->{$field}; } } } if (!$this->_row->isNew()) { foreach ($this->_getPrimaryKey() as $key => $value) { $filters[Centurion_Db_Table_Select::OPERATOR_NEGATION . $key] = $value; } } $filters['slug__' . Centurion_Db_Table_Select::OPERATOR_CONTAINS] = $slug . '%'; $rows = $select->filter($filters)->fetchAll(); if ($rows->count() > 0) { $identicalSlugIds = array(); foreach ($rows as $row) { if ($separatorPos = strpos($row->slug, '_')) { $identicalSlugIds[] = substr($row->slug, ++$separatorPos); } } if (count($identicalSlugIds) > 0) { $i = 1; while (in_array($i, $identicalSlugIds)) { $i++; } $slug .= '_' . $i; } } $this->_row->slug = $slug; } }
public function isValid($data) { if (!$data['url']) { if ($this->hasInstance()) { $data['url'] = '/' . $this->getInstance()->slug; } elseif ($data['title']) { $data['url'] = '/' . Centurion_Inflector::slugify($data['title']); } } $params = array(); // @todo: should be done with a trait ? if (isset($data['language_id'])) { $params['language_id'] = $data['language_id']; } $this->getElement('url')->getValidator('Centurion_Form_Model_Validator_AlreadyTaken')->mergeParams($params); return parent::isValid($data); }
public function slugify($text) { return Centurion_Inflector::slugify($text); }
/** * Assembles user submitted parameters forming a URL path defined by this route * * @param array $data An array of variable and value pairs used as parameters * @param boolean $reset Whether or not to set route defaults with those provided in $data * @return string Route path with user submitted parameters */ public function assemble($data = array(), $reset = false, $encode = false, $partial = false) { if ($this->_isTranslated) { $translator = $this->getTranslator(); if (isset($data['@locale'])) { $locale = $data['@locale']; unset($data['@locale']); } else { $locale = $this->getLocale(); } } $url = array(); $flag = false; foreach ($this->_parts as $key => $part) { $name = isset($this->_variables[$key]) ? $this->_variables[$key] : null; $useDefault = false; if (isset($name) && array_key_exists($name, $data) && $data[$name] === null) { $useDefault = true; } if (isset($name)) { if (isset($data[$name]) && !$useDefault) { $value = $data[$name]; unset($data[$name]); } elseif (!$reset && !$useDefault && isset($this->_values[$name])) { $value = $this->_values[$name]; } elseif (!$reset && !$useDefault && isset($this->_wildcardData[$name])) { $value = $this->_wildcardData[$name]; } elseif (isset($this->_defaults[$name])) { $value = $this->_defaults[$name]; } else { require_once 'Zend/Controller/Router/Exception.php'; throw new Zend_Controller_Router_Exception($name . ' is not specified'); } if ($this->_isTranslated && in_array($name, $this->_translatable)) { $url[$key] = Centurion_Inflector::slugify($translator->translate($value, $locale)); } else { $url[$key] = $value; } } elseif ($part != '*') { if ($this->_isTranslated && substr($part, 0, 1) === '@') { if (substr($part, 1, 1) !== '@') { $url[$key] = Centurion_Inflector::slugify($translator->translate(substr($part, 1), $locale)); } else { $url[$key] = substr($part, 1); } } else { if (substr($part, 0, 2) === '@@') { $part = substr($part, 1); } $url[$key] = $part; } } else { $defaults = $this->getDefaults(); foreach ($this->_wildcardData as $key => $val) { if (substr($key, 0, 1) !== '@' && isset($defaults['@' . $key])) { $this->_wildcardData['@' . $key] = $val; unset($this->_wildcardData[$key]); } } if (!$reset) { $data += $data + $this->_wildcardData; } $dataTemp = array(); foreach ($defaults as $key => $val) { if (isset($data[$key])) { $dataTemp[$key] = $data[$key]; unset($data[$key]); } } $data = $dataTemp + $data; foreach ($data as $var => $value) { if ($value !== null && (!isset($defaults[$var]) || $value != $defaults[$var])) { if ($this->_isTranslated && substr($var, 0, 1) === '@') { $url[$key++] = Centurion_Inflector::slugify($translator->translate(substr($var, 1), $locale)); } else { if (isset($defaults['@' . $var])) { $data['@' . $var] = $value; unset($data[$var]); continue; } $url[$key++] = $var; } $url[$key++] = $value; $flag = true; } } } } $return = ''; foreach (array_reverse($url, true) as $key => $value) { $defaultValue = null; if (isset($this->_variables[$key])) { $defaultValue = $this->getDefault($this->_variables[$key]); if ($this->_isTranslated && $defaultValue !== null && isset($this->_translatable[$this->_variables[$key]])) { $defaultValue = Centurion_Inflector::slugify($translator->translate($defaultValue, $locale)); } } if ($flag || $value !== $defaultValue || $partial) { if ($encode) { $value = rawurlencode($value); } $return = $this->_urlDelimiter . $value . $return; $flag = true; } } return trim($return, $this->_urlDelimiter); }
public function preSave() { // Get the column used to generate the slug $slugParts = (array) $this->_row->getSlugifyName(); $slugifiedParts = array(); $modifiedFields = $this->_row->getModifiedFields(); if (isset($modifiedFields['slug'])) { return; } // If one of the column used to generate the slug has been modified OR if the row is new : generate a slug if (count(array_intersect(array_keys($modifiedFields), $slugParts)) || $this->isNew()) { foreach ($slugParts as $part) { $partValue = $this->_row->{$part}; if (null == $partValue) { continue; } // Slugify each value of selected columns $slugifiedParts[] = Centurion_Inflector::slugify($partValue); } // Assemble the slugified values to get a slug $slug = implode($this->_slugColumnsSeparator, $slugifiedParts); if (null == $slug) { $slug = $this->_slugColumnsSeparator; } // Get the current slug of the row ($currentSlug = null if the row is new) $currentSlug = $this->_row->slug; /** * Get the original slug * * Explanation : If a slug is already taken by another row, the generated slug is {expected_slug}_# * where # is an incremental value of the number of occurrences of this slug */ if ($separatorPos = strpos($this->_row->slug, $this->_slugIteratorSeparator)) { $currentSlug = substr($currentSlug, 0, $separatorPos); } // Get the table name $name = $this->_row->getTable()->info('name'); $select = $this->getTable()->select(); $filters = array(); /** * A function named getFilterFieldsForSlug could be defined in the row, * to filter the search of duplicate field by a given column. * Exemple if my table have a category_id, i could tell slug trait, to have uniq slug in a category. * In that case 2 row, of different category, could have the same slug. */ if (method_exists($this->_row, 'getFilterFieldsForSlug')) { $filterField = $this->_row->getFilterFieldsForSlug(); if (is_array($filterField)) { foreach ($filterField as $field) { $filters[$field] = $this->_row->{$field}; } } } /** * We ignore the current row for duplicate slug search */ if (!$this->_row->isNew()) { foreach ($this->_getPrimaryKey() as $key => $value) { $filters[Centurion_Db_Table_Select::OPERATOR_NEGATION . $key] = $value; } } // Generate the array used for filters method to find identical slug $filters['slug__' . Centurion_Db_Table_Select::OPERATOR_CONTAINS] = $slug . '%'; // Get all the rows of the table with the same slug $rows = $select->filter($filters)->fetchAll(); // If the slug we want to use is already taken generate a new one with the syntax : {slug}_# if ($rows->count() > 0) { $identicalSlugIds = array(); foreach ($rows as $row) { if ($separatorPos = strpos($row->slug, $this->_slugIteratorSeparator)) { $identicalSlugIds[] = substr($row->slug, ++$separatorPos); } } /** * Generate the suffix "_#" for the slug * * /!\ During the generation of suffix _#, if we have a a free slot between two suffix * Ex : We have a table with three row, their respective slug are : slug, slug_1 & slug_3 * We fill this slot with the current generated slug and we get : * slug, slug_1, slug_3, slug_2 (= current generated slug) */ $i = 1; if (count($identicalSlugIds) > 0) { while (in_array($i, $identicalSlugIds)) { $i++; } } // Add suffix "_#" at the end of the generated slug $slug .= $this->_slugIteratorSeparator . $i; } $this->_row->slug = $slug; } }
/** * Test slug when this one is composed of several columns */ public function testSlugResultWithEmptyColumn() { /** * @Given an empty table that implement slug * @And that this table have 2 columns that implement is used to create the slug */ $table = new Asset_Model_DbTable_WithMultiColumnsForSlug(); $table->select()->fetchAll()->delete(); /** * @If i a create a new row * @And i fill the 2 columns */ $titleSubtitleRow = $table->createRow(); $titleSubtitleRow->title = 'Title slug test'; $titleSubtitleRow->subtitle = 'Subtitle slug test'; $titleSubtitleRow->save(); /** * @Except that the slug is composed by the 2 columns, slugify, concatened by */ $this->assertEquals(Centurion_Inflector::slugify('Title slug test') . '-' . Centurion_Inflector::slugify('Subtitle slug test'), $titleSubtitleRow->slug, 'The slug isn\'t equals to what we expect'); /** * In this case the slug is composed of the title * * We except : * - slug = Centurion_Inflector::slugify('Titre de test') */ $titleRow = $table->createRow(); $titleRow->title = 'Only title slug test'; $titleRow->save(); $this->assertEquals(Centurion_Inflector::slugify('Only title slug test'), $titleRow->slug, 'The slug isn\'t equals to what we expect'); /** * In this case the slug is composed of the subtitle * * We except : * - slug = Centurion_Inflector::slugify('SubTitre de test') */ $subtitleRow = $table->createRow(); $subtitleRow->subtitle = 'Only subtitle slug test'; $subtitleRow->save(); $this->assertEquals(Centurion_Inflector::slugify('Only subtitle slug test'), $subtitleRow->slug, 'The slug isn\'t equals to what we expect'); }
protected function _preSave() { parent::_preSave(); if ($this->title) { $this->slug = Centurion_Inflector::slugify($this->title); } else { $this->slug = Centurion_Inflector::slugify($this->slug); } }