/**
  * Generates a unique URLSegment from the title.
  *
  * @param int $increment
  *
  * @return string
  */
 public function generateURLSegment($increment = null)
 {
     $filter = new URLSegmentFilter();
     // Setting this to on. Because of the UI flow, it would be quite a lot of work
     // to support turning this off. (ie. the add by title flow would not work).
     // If this becomes a problem we can approach it then.
     // @see https://github.com/silverstripe/silverstripe-blog/issues/376
     $filter->setAllowMultibyte(true);
     $this->owner->URLSegment = $filter->filter($this->owner->Title);
     if (is_int($increment)) {
         $this->owner->URLSegment .= '-' . $increment;
     }
     // Postgres use '' instead of 0 as an emtpy blog ID
     // Without this all the tests fail
     if (!$this->owner->BlogID) {
         $this->owner->BlogID = 0;
     }
     $duplicate = DataList::create($this->owner->ClassName)->filter(array('URLSegment' => $this->owner->URLSegment, 'BlogID' => $this->owner->BlogID));
     if ($this->owner->ID) {
         $duplicate = $duplicate->exclude('ID', $this->owner->ID);
     }
     if ($duplicate->count() > 0) {
         if (is_int($increment)) {
             $increment += 1;
         } else {
             $increment = 0;
         }
         $this->owner->generateURLSegment((int) $increment);
     }
     return $this->owner->URLSegment;
 }
 /**
  * Generates a unique URLSegment from the title.
  *
  * @param int $increment
  *
  * @return string
  */
 public function generateURLSegment($increment = null)
 {
     $filter = new URLSegmentFilter();
     $this->owner->URLSegment = $filter->filter($this->owner->Title);
     if (is_int($increment)) {
         $this->owner->URLSegment .= '-' . $increment;
     }
     // Postgres use '' instead of 0 as an emtpy blog ID
     // Without this all the tests fail
     if (!$this->owner->BlogID) {
         $this->owner->BlogID = 0;
     }
     $duplicate = DataList::create($this->owner->ClassName)->filter(array('URLSegment' => $this->owner->URLSegment, 'BlogID' => $this->owner->BlogID));
     if ($this->owner->ID) {
         $duplicate = $duplicate->exclude('ID', $this->owner->ID);
     }
     if ($duplicate->count() > 0) {
         if (is_int($increment)) {
             $increment += 1;
         } else {
             $increment = 0;
         }
         $this->owner->generateURLSegment((int) $increment);
     }
     return $this->owner->URLSegment;
 }
 public function getCMSFields()
 {
     $fields = new FieldList([TextField::create('Title')]);
     if ($this->exists()) {
         $folderName = 'Documents';
         $config = $this->Page()->exists() ? $this->Page()->config()->get('page_documents') : null;
         if (is_array($config)) {
             if (isset($config['folder'])) {
                 $folderName = $config['folder'];
             }
             if (isset($config['section']) && $config['section']) {
                 $filter = new URLSegmentFilter();
                 $section = implode('-', array_map(function ($string) {
                     return ucfirst($string);
                 }, explode('-', $filter->filter($this->Title))));
                 $folderName .= '/' . $section;
             }
         }
         $fields->push(SortableUploadField::create('Documents', 'Documents')->setDescription('Drag documents by thumbnail to sort')->setFolderName($folderName));
     } else {
         $fields->push(LiteralField::create('DocumentsNotSaved', '<p>Save category to add documents</p>'));
     }
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     $class = $this->ownerBaseClass;
     $config = Config::inst()->forClass($class);
     // look for fields to use in slug
     $fields = array('Title');
     if ($config->slug_fields) {
         $fields = $config->slug_fields;
     }
     $needSlug = false;
     foreach ($fields as $field) {
         if ($this->owner->isChanged($field, 2)) {
             $needSlug = true;
             break;
         }
     }
     if (!$this->owner->Slug) {
         $needSlug = true;
     }
     // if we need a slug, compute it
     if ($needSlug && $this->owner->ID) {
         $slug = '';
         foreach ($fields as $field) {
             $slug .= ' ' . $this->owner->{$field};
         }
         $slug = trim($slug);
         $baseSlug = $slug;
         $filter = new URLSegmentFilter();
         $oldSlug = $this->owner->Slug;
         $newSlug = substr($filter->filter($slug), 0, 140);
         $this->owner->Slug = $newSlug;
         // check for existing slugs
         $count = 0;
         $record = self::getBySlug($class, $newSlug, $this->owner->ID);
         while ($record && $record->exists()) {
             $count++;
             $slug = $baseSlug . '-' . $count;
             $newSlug = $filter->filter($slug);
             $this->owner->Slug = $newSlug;
             $record = self::getBySlug($class, $newSlug, $this->owner->ID);
         }
         // prevent infinite loop because of onAfterWrite called multiple times
         if ($oldSlug == $newSlug) {
             return;
         }
         $this->owner->write();
         // store history
         if ($oldSlug && $oldSlug != $this->owner->Slug) {
             $count = SlugHistory::check($class, $oldSlug, $this->owner->ID);
             if ($count) {
                 // it already exists, no need to add twice
                 return;
             }
             SlugHistory::recordFromObject($this->owner, $oldSlug);
         }
     }
 }
예제 #5
0
 function testReplacements()
 {
     $f = new URLSegmentFilter();
     $this->assertEquals('tim-and-struppi', $f->filter('Tim&Struppi'));
     // Customize replacements
     $rs = $f->getReplacements();
     $rs['/&/u'] = '-und-';
     $f->setReplacements($rs);
     $this->assertEquals('tim-und-struppi', $f->filter('Tim&Struppi'));
 }
 /**
  * The gallery directory is created based on the page name
  * You can overwrite this behavior by extending this page
  * TODO this could also be made configurable
  * @return string
  */
 function getCalcAssetsFolderDirectory()
 {
     if ($this->ID) {
         $filter = URLSegmentFilter::create();
         return Config::inst()->get('GalleryExtension', 'gallery_folder') . '/' . $this->ID . '-' . $filter->filter($this->Title);
     }
 }
예제 #7
0
 public function onBeforeWrite()
 {
     if (!$this->URLSegment) {
         $filter = URLSegmentFilter::create();
         $this->URLSegment = $filter->filter($this->Title);
     }
     parent::onBeforeWrite();
 }
 /**
  * custom setter for urlsegment
  * runs param through urlsegment filter to sanitize any unwanted characters
  * calls existsInScope for uniqueness check, otherwise appends random number until unique
  */
 function setURLSegment($value)
 {
     $urlSegment = URLSegmentFilter::create()->filter($value);
     while ($this->existsInScope($urlSegment)) {
         $urlSegment = $urlSegment . rand(1, 9);
     }
     $this->owner->setField("URLSegment", $urlSegment);
 }
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if ($this->Title) {
         $filter = URLSegmentFilter::create();
         $this->URLSegment = $filter->filter($this->Title);
         //$this->URLSegment = SiteTree::GenerateURLSegment($this->Title);
     }
 }
 /** 
  * Generates a unique URLSegment from the title.
  *
  * @param $increment
  *
  * @return string URLSegment
  **/
 public function generateURLSegment($increment = null)
 {
     $filter = new URLSegmentFilter();
     $this->owner->URLSegment = $filter->filter($this->owner->Title);
     if (is_int($increment)) {
         $this->owner->URLSegment .= '-' . $increment;
     }
     // Check to see if the URLSegment already exists
     $duplicate = DataList::create($this->owner->ClassName)->filter(array("URLSegment" => $this->owner->URLSegment, "BlogID" => $this->owner->BlogID));
     if ($this->owner->ID) {
         $duplicate = $duplicate->exclude("ID", $this->owner->ID);
     }
     if ($duplicate->count() > 0) {
         $increment = is_int($increment) ? $increment + 1 : 0;
         $this->owner->generateURLSegment((int) $increment);
     }
     return $this->owner->URLSegment;
 }
 /**
  * Get folder for a given class
  * 
  * @param mixed $class
  * @return string
  */
 public static function getFolderForClass($class)
 {
     $folderName = 'Uploads';
     if (is_object($class)) {
         if (method_exists($class, 'hasMethod') && $class->hasMethod('BaseFolder')) {
             $folderName = $class->BaseFolder();
         } else {
             if ($class instanceof Page) {
                 $folderName = get_class($class);
             } else {
                 if ($class instanceof DataObject) {
                     $folderName = $class->baseTable();
                 } else {
                     if ($class instanceof DataExtension) {
                         $folderName = $class->getOwner()->baseTable();
                     } else {
                         $folderName = get_class($class);
                     }
                 }
             }
         }
     } else {
         if (is_string($class)) {
             $folderName = $class;
         }
     }
     if (class_exists('Subsite') && Config::inst()->get(__CLASS__, 'use_subsite_integration')) {
         $subsite = Subsite::currentSubsite();
         if ($subsite) {
             // Subsite extras integration$
             if ($subsite->hasField('BaseFolder')) {
                 $baseFolder = $subsite->BaseFolder;
             } else {
                 $filter = new URLSegmentFilter();
                 $baseFolder = $filter->filter($subsite->getTitle());
                 $baseFolder = str_replace(' ', '', ucwords(str_replace('-', ' ', $baseFolder)));
             }
             if (!empty($baseFolder)) {
                 $folderName = $baseFolder . '/' . $folderName;
             }
         }
     }
     return $folderName;
 }
예제 #12
0
 private function generateURLSegment($title)
 {
     $filter = URLSegmentFilter::create();
     $t = $filter->filter($title);
     // Fallback to generic page name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = "page-{$this->ID}";
     }
     return $t;
 }
 /**
  * Generate a unique URL segment based on the Member's name.
  *
  * @return string
  */
 public function generateURLSegment()
 {
     $filter = URLSegmentFilter::create();
     $name = $this->owner->FirstName . ' ' . $this->owner->Surname;
     $urlSegment = $filter->filter($name);
     if (!$urlSegment || $urlSegment == '-' || $urlSegment == '-1') {
         $urlSegment = 'profile-' . $this->owner->ID;
     }
     return $urlSegment;
 }
 /**
  * Generates a unique URLSegment from the title.
  *
  * @param int $increment
  *
  * @return string
  */
 public function generateURLSegment($increment = null)
 {
     $filter = new URLSegmentFilter();
     $this->owner->URLSegment = $filter->filter($this->owner->Title);
     if (is_int($increment)) {
         $this->owner->URLSegment .= '-' . $increment;
     }
     $duplicate = DataList::create($this->owner->ClassName)->filter(array('URLSegment' => $this->owner->URLSegment, 'BlogID' => $this->owner->BlogID));
     if ($this->owner->ID) {
         $duplicate = $duplicate->exclude('ID', $this->owner->ID);
     }
     if ($duplicate->count() > 0) {
         if (is_int($increment)) {
             $increment += 1;
         } else {
             $increment = 0;
         }
         $this->owner->generateURLSegment((int) $increment);
     }
     return $this->owner->URLSegment;
 }
 /**
  * Generate a URL segment based on the title provided.
  *
  * If {@link Extension}s wish to alter URL segment generation, they can do so by defining
  * updateURLSegment(&$url, $title).  $url will be passed by reference and should be modified.
  * $title will contain the title that was originally used as the source of this generated URL.
  * This lets extensions either start from scratch, or incrementally modify the generated URL.
  *
  * @param string $title the given title
  * @return string generated url segment
  */
 public function generateURLSegment($title)
 {
     $filter = URLSegmentFilter::create();
     $t = $filter->filter($title);
     // Fallback to generic page name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = $this->fallbackUrl();
     }
     // Hook for extensions
     $this->owner->extend('updateURLSegment', $t, $title);
     return $t;
 }
 public function generateSlug($title, $allowExtension = true)
 {
     $t = \URLSegmentFilter::create()->filter($title);
     // Fallback to generic page name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = "menu-{$this->ID}";
     }
     // Hook for extensions
     if ($allowExtension) {
         $this->extend('updateSlug', $t, $title);
     }
     return $t;
 }
 public function updateURLSegment(&$t, $title)
 {
     $filter = URLSegmentFilter::create();
     // use default transliterator
     $title = $filter->getTransliterator()->toASCII($title);
     // set cyrillic transliterator
     $filter->setTransliterator(CyrillicTransliterator::create());
     $t = $filter->filter($title);
     // Fallback to generic page name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = "page-" . $this->owner->ID;
     }
 }
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if ($this->isChanged('URLSegment')) {
         $this->URLSegment = URLSegmentFilter::create()->filter($this->URLSegment);
     }
     if (!$this->URLSegment) {
         $this->URLSegment = URLSegmentFilter::create()->filter($this->Title);
     }
     if (!$this->URLSegment) {
         $this->URLSegment = 'main';
     }
 }
 /**
  * Generate a URL segment based on the title provided.
  * 
  * @param string $title Page title.
  * @return string Generated url segment
  */
 protected function generateURLSegment($title)
 {
     $filter = URLSegmentFilter::create();
     $t = $filter->filter($title);
     // Fallback to generic name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = "{$this->owner->ClassName}-{$this->owner->ID}";
     } else {
         // Make sure that it does not already exists for another object
         $class = $this->owner->ClassName;
         $obj = $class::get()->filter(array("URLSegment" => $t))->exclude(array("ID" => $this->owner->ID))->first();
         if ($obj) {
             $t .= "-{$this->owner->ID}";
         }
     }
     return $t;
 }
 /**
  * COPIED FROM SITETREE
  *
  * Generate a URL segment based on the title provided.
  * 
  * @param string $title Product title
  * @return string Generated url segment
  */
 public function generateURLSegment($title)
 {
     $filter = URLSegmentFilter::create();
     $t = $filter->filter($title);
     // Fallback to generic page name if path is empty (= no valid, convertable characters)
     if (!$t || $t == '-' || $t == '-1') {
         $t = "page-{$this->ID}";
     }
     // Hook for extensions
     $this->extend('updateURLSegment', $t, $title);
     // Check to see if URLSegment exists already, if it does, append -* where * is COUNT()+1
     $seg = new SQLQuery('COUNT(*)');
     $seg->setFrom(get_class($this))->addWhere("`URLSegment` LIKE '%{$t}%'");
     $count = $seg->execute()->value();
     if ($count > 0) {
         $count++;
         return $t . "-" . $count;
     } else {
         return $t;
     }
 }
예제 #21
0
 /**
  * Based on the title of product it generates a URLSegment for it
  * so that SEO friendly Product pages can be generated.
  *
  * Credit: https://gist.github.com/Zauberfisch/9460395
  *
  * @return string URLSegment /product-title-x-y-z/
  */
 protected function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if (!$this->ProductDescription) {
         $this->ProductDescription = $this->ProductDescription;
     }
     $filter = URLSegmentFilter::create();
     if (!$this->URLSegment) {
         $this->URLSegment = $this->ProductDescription;
     }
     $this->URLSegment = $filter->filter($this->URLSegment);
     if (!$this->URLSegment) {
         $this->URLSegment = uniqid();
     }
     $count = 2;
     while (static::get_by_url_segment($this->URLSegment, $this->ID)) {
         // add a -n to the URLSegment if it already existed
         $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
         $count++;
     }
 }
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // set URLSegment
     if ($this->Title) {
         $filter = URLSegmentFilter::create();
         $this->URLSegment = $filter->filter($this->Title);
         //$this->URLSegment = SiteTree::GenerateURLSegment($this->Title);
     }
     // Some cleaning up
     //		$this->Title = trim( $this->Title ); // strip spaces
     //		// if multiple with same title, combine in this one & remove duplicates;
     //		$sameTitleTag = FilterCategory::get()->filter('Title', $this->Title)->exclude('ID',$this->ID);
     //		if( $sameTitleTag->count() && $this->ID ){ //only if editing existing (ID is set)
     //			foreach( $sameTitleTag as $duplicate ){
     //				debug::dump($duplicate->Pages());
     ////				foreach( $duplicate->Pages() as $page ){
     ////					//add each page to this cat/tag
     ////					//$this->Pages()->add( $page->ID );
     ////				}
     //			}
     //		}
 }
예제 #23
0
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $classname = strtolower($this->owner->ClassName);
     $filter = URLSegmentFilter::create();
     if ((!$this->owner->URLSegment || $this->owner->URLSegment == 'new-' . $classname) && $this->owner->Title != 'New ' . $classname) {
         $this->owner->URLSegment = $filter->filter($this->owner->Title);
     } else {
         if ($this->owner->isChanged('URLSegment')) {
             $segment = preg_replace('/[^A-Za-z0-9]+/', '-', $this->owner->URLSegment);
             $segment = preg_replace('/-+/', '-', $segment);
             if (!$segment) {
                 $segment = $classname . "-" . $this->owner->ID;
             }
             $this->owner->URLSegment = $segment;
         }
     }
     $count = 2;
     while ($this->LookForExistingURLSegment($this->owner->URLSegment)) {
         $this->owner->URLSegment = preg_replace('/-[0-9]+$/', null, $this->owner->URLSegment) . '-' . $count;
         $count++;
     }
 }
예제 #24
0
 public function testRemovesBadCharactersWithMultibyteAllowed()
 {
     $filter = new URLSegmentFilter();
     $filter->setAllowMultibyte(true);
     $this->assertEquals('url-with-bad-characters', $filter->filter('url?-with/-bad#-characters='));
 }
 /**
  * @param  bool $title
  * @return string
  */
 public function getGeneratedIdentifier($title = false)
 {
     return URLSegmentFilter::create()->filter($title ? $title : $this->owner->Title);
 }
예제 #26
0
 protected function validate()
 {
     $valid = parent::validate();
     if (!$valid->valid()) {
         return $valid;
     }
     if (is_subclass_of(Controller::curr(), "LeftAndMain")) {
         if (empty($this->Name)) {
             return $valid->error('Name is empty!');
         }
         if (empty($this->URL)) {
             return $valid->error('URL is empty!');
         }
         if (empty($this->URLSegment)) {
             $filter = URLSegmentFilter::create();
             $slug = $filter->filter($this->Name);
             // Fallback to generic page name if path is empty (= no valid, convertable characters)
             if (!$slug || $slug == '-' || $slug == '-1') {
                 return $valid->error(sprintf('invalid Autogenerated URLSegment (%s) ! please set one by hand.', $slug));
             }
             $this->URLSegment = $slug;
         }
         if (empty($this->URLSegment)) {
             return $valid->error('URLSegmen is empty!');
         }
         if ($this->LookForExistingURLSegment($this->URLSegment)) {
             return $valid->error(sprintf('invalid URLSegment: %s already exists! choose another one', $this->URLSegment));
         }
     }
     return $valid;
 }
예제 #27
0
 /**
  * Convert a string (normally a title) to a string suitable for using in
  * urls and other html attributes. Uses {@link URLSegmentFilter}.
  *
  * @param string 
  * @return string
  */
 public static function raw2url($title)
 {
     $f = URLSegmentFilter::create();
     return $f->filter($title);
 }
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // Only call on first creation, ir if title is changed
     if ($this->ID == 0 || $this->isChanged('Title') || !$this->URLSegment) {
         // Set the URL Segment, so it can be accessed via the controller
         $filter = URLSegmentFilter::create();
         $t = $filter->filter($this->Title);
         // Fallback to generic name if path is empty (= no valid, convertable characters)
         if (!$t || $t == '-' || $t == '-1') {
             $t = "category-{$this->ID}";
         }
         // Ensure that this object has a non-conflicting URLSegment value.
         $existing_cats = ProductCategory::get()->filter('URLSegment', $t)->count();
         $existing_products = Product::get()->filter('URLSegment', $t)->count();
         $existing_pages = class_exists('SiteTree') ? SiteTree::get()->filter('URLSegment', $t)->count() : 0;
         $count = (int) $existing_cats + (int) $existing_products + (int) $existing_pages;
         $this->URLSegment = $count ? $t . '-' . ($count + 1) : $t;
     }
 }
 /**
  * Taken from https://github.com/NightJar/ssrigging-slug/blob/master/code/Slug.php
  */
 public function Slug($regen = false)
 {
     $existing = $this->URLSlug;
     return $existing && !$regen ? $existing : URLSegmentFilter::create()->filter($this->Title);
 }
 /**
  * Generates an Url segment by a given string.
  * You can pass a DataObject as 2nd parameter, which's ClassName
  * and ID will be used if the filtering on title fails.
  * 
  * @param  string $title
  * @param  DataObject $instance
  * @return string
  */
 public static function generate_urlsegment($title, DataObject $instance = null)
 {
     $filter = URLSegmentFilter::create();
     $t = $filter->filter($title);
     if (!$t || $t == '-' || $t == '-1' && $instance != null) {
         $t = "{$instance->ClassName}-{$instance->ID}";
     }
     return $t;
 }