Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Checking page content before saving


Reply


3 Posts   414 Views

Avatar
simples

Community Member, 77 Posts

5 September 2012 at 4:44am

Edited: 05/09/2012 4:48am

Hi,

I have just resorted to adding the following code to the save function in LeftAndMain.php to check page content before saving. From a user's point of view this is very neat because if a check fails a red message appears where a green Saved message normally appears. From a development point of view however it is bad because such code will get wiped out whenever I upgrade the cms code. I obviously could improve this by separating out the functions so that all which is left is the calling code which has been added to the existing save function. This calling code will still get wiped however.

Anyway to help explain what I have done the extract below shows what I have added to the existing Version 2.4 cms code. The added code is shown in bold.


private function checkPageContent($object){
   foreach($object as $key=>$field){
      if($key=='Title')$Title=$field->dataValue();
      if($key=='MenuTitle')$MenuTitle=$field->dataValue();
      if($key=='MetaTitle')$MetaTitle=$field->dataValue();
    if($key=='MetaDescription')$MetaDescription=$field->dataValue();
    if($key=='Content')$Content=$field->dataValue();
   }
   // ----
   // Checks if title has been added
   // ----
   if(strlen($Title)<=1)return "Error: Page was not saved. A title needs to be added.";
   // ----
   // Checks if menutitle has been added
   // ----
   if(strlen($MenuTitle)<=1)return "Error: Page was not saved. A navigation label needs to be added.";
   // ----
   // Checks if title has been added
   // ----
   if(strlen($MetaTitle)<=1)return "Error: Page was not saved. A meta title to be added.";
   // ----
   // Checks if description has been added
   // ----
   if(strlen($MetaDescription)<=1)return "Error: Page was not saved. A meta description needs to be added.";
   // ----
   // Checks if a picture has been added
   // ----
   $links=$this->returnImageLinks($Content);
   if(count($links)==0)return "Error: Page was not saved. An image needs to be added to the page content.";
   // ----
   return;
}

private function returnImageLinks($html){
   $links=array();
   $doc=new DOMDocument();
   $doc->loadHTML($html);
   $elements=$doc->getElementsByTagName('img');
   for($i=0;$i<$elements->length;$i++){
    $src=$elements->item($i)->getAttribute('src');
    $links[]=$src;
   }
   return $links;
}


   /**
    * Save and Publish page handler
    */
   public function save($urlParams, $form) {
      $className = $this->stat('tree_class');
      $SQL_id = Convert::raw2sql($_REQUEST['ID']);
      if(substr($SQL_id,0,3) != 'new') {
         $record = DataObject::get_one($className, "\"$className\".\"ID\" = {$SQL_id}");
         if($record && !$record->canEdit()) return Security::permissionFailure($this);
      } else {
         if(!singleton($this->stat('tree_class'))->canCreate()) return Security::permissionFailure($this);
         $record = $this->getNewItem($SQL_id, false);
      }

      // We don't want to save a new version if there are no changes
      $dataFields_new = $form->Fields()->dataFields();

   if(substr($SQL_id,0,3) != 'new') {
    if($record->ClassName=='NewsPage'){
      $error=$this->checkPageContent($dataFields_new);
      if(strlen($error)>0){
          FormResponse::status_message($error, "bad");
          return FormResponse::respond();
      }
    }
   }

Does anyone know a way in which this could have been done without editing the core cms code?

Thanks.

Avatar
Willr

Forum Moderator, 5513 Posts

7 September 2012 at 10:20pm

A clean way would be to add an extension hook to that LeftAndMain function say 'onSave($dataFields, $form)'. Then put the rest of your changes in an extension. Then at least you have 1 line of change to manage.

You could try replace the LeftAndMain class with Object::useCustomClass() but not sure that'll give you much luck.

Avatar
simples

Community Member, 77 Posts

7 September 2012 at 10:51pm

Hi Willr,

Thank you for this valuable insight.