Example #1
0
 protected function onBeforeWrite()
 {
     if (!$this->Sort && $this->ParentID) {
         $this->Sort = DB::query("SELECT MAX(Sort) + 1 FROM SiteTree WHERE ParentID = {$this->ParentID}")->value();
     }
     // Auto-set URLSegment
     if ((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
         $this->URLSegment = $this->generateURLSegment($this->Title);
         // Keep it clean
     } else {
         if (isset($this->changed['URLSegment']) && $this->changed['URLSegment']) {
             $segment = ereg_replace('[^A-Za-z0-9]+', '-', $this->URLSegment);
             $segment = ereg_replace('-+', '-', $segment);
             if (!$segment) {
                 $segment = "page-{$this->ID}";
             }
             $this->URLSegment = $segment;
         }
     }
     DataObject::set_context_obj($this);
     // Ensure URLSegment is unique
     $idFilter = $this->ID ? " AND `SiteTree`.ID <> '{$this->ID}'" : '';
     $count = 1;
     while (class_exists($this->URLSegment) || DataObject::get_one("SiteTree", "URLSegment = '{$this->URLSegment}' {$idFilter}")) {
         $count++;
         $this->URLSegment = ereg_replace('-[0-9]+$', '', $this->URLSegment) . "-{$count}";
     }
     DataObject::set_context_obj(null);
     // If the URLSegment has been changed, rewrite links
     if (isset($this->changed['URLSegment']) && $this->changed['URLSegment']) {
         if ($this->hasMethod('BackLinkTracking')) {
             $links = $this->BackLinkTracking();
             if ($links) {
                 foreach ($links as $link) {
                     $link->rewriteLink($this->original['URLSegment'] . '/', $this->URLSegment . '/');
                     $link->write();
                 }
             }
         }
     }
     // If priority is empty or invalid, set it to the default value
     if (!is_numeric($this->Priority) || ($this->Priority < 0 || $this->Priority > 1)) {
         $this->Priority = self::$defaults['Priority'];
     }
     parent::onBeforeWrite();
 }
Example #2
0
 protected function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // If Sort hasn't been set, make this page come after it's siblings
     if (!$this->Sort) {
         $parentID = $this->ParentID ? $this->ParentID : 0;
         $this->Sort = DB::query("SELECT MAX(\"Sort\") + 1 FROM \"SiteTree\" WHERE \"ParentID\" = {$parentID}")->value();
     }
     // If there is no URLSegment set, generate one from Title
     if ((!$this->URLSegment || $this->URLSegment == 'new-page') && $this->Title) {
         $this->URLSegment = $this->generateURLSegment($this->Title);
     } else {
         if ($this->isChanged('URLSegment')) {
             // Make sure the URLSegment is valid for use in a URL
             $segment = ereg_replace('[^A-Za-z0-9]+', '-', $this->URLSegment);
             $segment = ereg_replace('-+', '-', $segment);
             // If after sanitising there is no URLSegment, give it a reasonable default
             if (!$segment) {
                 $segment = "page-{$this->ID}";
             }
             $this->URLSegment = $segment;
         }
     }
     DataObject::set_context_obj($this);
     // Ensure that this object has a non-conflicting URLSegment value.
     $count = 2;
     while (!$this->validURLSegment()) {
         $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
         $count++;
     }
     DataObject::set_context_obj(null);
     $this->syncLinkTracking();
     // Check to see if we've only altered fields that shouldn't affect versioning
     $fieldsIgnoredByVersioning = array('HasBrokenLink', 'Status', 'HasBrokenFile', 'ToDo');
     $changedFields = array_keys($this->getChangedFields(true, 2));
     // This more rigorous check is inline with the test that write()
     // does to dedcide whether or not to write to the DB.  We use that
     // to avoid cluttering the system with a migrateVersion() call
     // that doesn't get used
     $oneChangedFields = array_keys($this->getChangedFields(true, 1));
     if ($oneChangedFields && !array_diff($changedFields, $fieldsIgnoredByVersioning)) {
         // This will have the affect of preserving the versioning
         $this->migrateVersion($this->Version);
     }
 }
Example #3
0
	protected function onBeforeWrite() {
		if(!$this->Sort && $this->ParentID) {
			$this->Sort = DB::query(
				"SELECT MAX(Sort) + 1 FROM SiteTree WHERE ParentID = $this->ParentID")->value();
		}

		// Auto-set URLSegment
		if((!$this->URLSegment || $this->URLSegment == 'new-page') &&
			 $this->Title) {
			$this->URLSegment = $this->generateURLSegment($this->Title);

		// Keep it clean
		} else if(isset($this->changed['URLSegment']) &&
							$this->changed['URLSegment']) {
			$segment = ereg_replace('[^A-Za-z0-9]+','-',$this->URLSegment);
			$segment = ereg_replace('-+','-',$segment);
			if(!$segment) {
				$segment = "page-$this->ID";
			}
			$this->URLSegment = $segment;
		}
		
		DataObject::set_context_obj($this);
		
		$idFilter = ($this->ID) ? "`SiteTree`.ID <> '$this->ID'" : '';

		$count = 1;
		while (
			(class_exists($this->URLSegment) && is_subclass_of($this->URLSegment, 'RequestHandler')) ||
			SiteTree::get_by_url($this->URLSegment, $idFilter)
		) {
			$count++;
			$this->URLSegment = ereg_replace('-[0-9]+$','', $this->URLSegment) . "-$count";
		}

		DataObject::set_context_obj(null);
		
		// If the URLSegment has been changed, rewrite links
		if(isset($this->changed['URLSegment']) && $this->changed['URLSegment']) {
			if($this->hasMethod('BackLinkTracking')) {
				$links = $this->BackLinkTracking();
				if($links) {
					foreach($links as $link) {
						$link->rewriteLink($this->original['URLSegment'] . '/',
															 $this->URLSegment . '/');
						$link->write();
					}
				}
			}
		}

		parent::onBeforeWrite();
	}