Jump to:

23471 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Checking page content before saving

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

Page: 1
Go to End
Author Topic: 385 Views
  • simples
    Avatar
    Community Member
    76 Posts

    Checking page content before saving Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5497 Posts

    Re: Checking page content before saving Link to this post

    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.

  • simples
    Avatar
    Community Member
    76 Posts

    Re: Checking page content before saving Link to this post

    Hi Willr,

    Thank you for this valuable insight.

    385 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.