本文整理汇总了PHP中URLSegmentFilter::filter方法的典型用法代码示例。如果您正苦于以下问题:PHP URLSegmentFilter::filter方法的具体用法?PHP URLSegmentFilter::filter怎么用?PHP URLSegmentFilter::filter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类URLSegmentFilter
的用法示例。
在下文中一共展示了URLSegmentFilter::filter方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onAfterWrite
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);
}
}
}
示例2: testReplacements
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'));
}
示例3: getCMSFields
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;
}
示例4: generateURLSegment
/**
* 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;
}
示例5: generateURLSegment
/**
* 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;
}
示例6: generateURLSegment
/**
* 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;
}
示例7: getFolderForClass
/**
* 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;
}
示例8: generateURLSegment
/**
* 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;
}
示例9: testRemovesBadCharactersWithMultibyteAllowed
public function testRemovesBadCharactersWithMultibyteAllowed()
{
$filter = new URLSegmentFilter();
$filter->setAllowMultibyte(true);
$this->assertEquals('url-with-bad-characters', $filter->filter('url?-with/-bad#-characters='));
}
示例10: onBeforeWrite
public function onBeforeWrite()
{
if ($this->Code) {
$filter = new URLSegmentFilter();
$this->Code = $filter->filter($this->Code);
}
parent::onBeforeWrite();
}
示例11: testReplacesTrailingDashes
public function testReplacesTrailingDashes()
{
$filter = new URLSegmentFilter();
$this->assertEquals('url-has-trailing-dashes', $filter->filter('url-has-trailing-dashes--'));
}
示例12: getGroupByName
/**
* @param string $name
* @return Group
*/
public static function getGroupByName($name)
{
if (!$name) {
return false;
}
Subsite::$disable_subsite_filter = true;
$urlfilter = new URLSegmentFilter();
$g = Group::get()->filter('Code', $urlfilter->filter($name))->first();
Subsite::$disable_subsite_filter = false;
return $g;
}
示例13: getHolder
/**
* @return BlogHolder
*/
protected function getHolder($record)
{
$filter = new URLSegmentFilter();
$urlSegment = $filter->filter($record['blog_path']);
$tree = $this->_cache_tree;
if (!$tree) {
$tree = BlogTree::get()->filter(array('Title' => 'Blogs'))->First();
if (!$tree) {
$tree = new BlogTree(array('Title' => 'Blogs', 'ParentID' => $this->parentId));
$tree->write();
if ($this->publish) {
$tree->publish('Stage', 'Live');
}
}
$this->_cache_tree = $tree;
}
$holder = isset($this->_cache_holders[$urlSegment]) ? $this->_cache_holders[$urlSegment] : null;
if (!$holder) {
$holder = BlogHolder::get()->filter(array('URLSegment' => $urlSegment, 'ParentID' => $tree->ID))->First();
if (!$holder) {
$holder = new BlogHolder(array('Title' => $record['blog_title'], 'URLSegment' => $urlSegment, 'ParentID' => $tree->ID));
$holder->write();
if ($this->publish) {
$holder->publish('Stage', 'Live');
}
}
$this->_cache_holders[$urlSegment] = $holder;
}
return $holder;
}