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.