Hi Perch,
Have I added the new line correctly?
Looks good!
Currently my savePage does not "report" errors. (If there is a code problem it just doesn't work!)
1) In the unlikely event that there is a clash of permalinks, how would I feed the error back to my savePage - without breaking the anything (and losing the changes that could have been made to the page)?
The permalink_cms_onSaveUpdatePermalinks() function doesn't return the error message and instead, calls the die($error) function if there's an error.
So if you want to control the error reporting, you can copy the code and create your own version of this function that returns the error messages instead of dying. See the second step below.
2) What code would I use to check if a new permalink has been added to an input box and, if so, then update the required tables?
First, convert the input box value to SEO-friendly URL (converts spaces to dashes, removes non-alphanumeric characters):
$permalink = _permalink_generate_formatInputText($_REQUEST['permalink_input_box']);
Second, check $permalink for errors. The code below is derived from permalink_cms_onSaveErrorChecking() function (you can convert the code below to you own function):
// check if permalink already exists in database
$permalinkRecord = mysql_get('_permalinks', null, array('permalink' => $permalink));
$isAlreadyUsed = $permalinkRecord && $permalinkRecord['recordNum'] != $recordNum && ($tableName != '_permalinks');
// error checking
$errors = '';
$requiredPrefix = $GLOBALS['schema']['permalink']['defaultValue'];
if (!$permalink) { $errors .= "You must enter a value for 'permalink'!"; }
elseif (preg_match("!^/!", $requiredPrefix)) { $errors .= "Permalink prefix can not start with a slash! Change the 'Default Value' for permalink field in Field Editor."; }
elseif ($permalink == $requiredPrefix) { $errors .= "You must enter a value for 'permalink' after '" .htmlencode($requiredPrefix). "'!"; }
elseif (!startsWith($requiredPrefix, $permalink)) { $errors .= "Permalink must start with '" .htmlencode($requiredPrefix). "'!"; }
elseif (preg_match("|[^\w\-\./]|i", $permalink)) { $errors .= "Permalink can only contain these chars (a-z0-9/-.)!"; }
elseif (preg_match("!^/|/$!", $permalink)) { $errors .= "Permalink can not start or end with a slash!"; }
elseif (preg_match("!\.{2,}!", $permalink)) { $errors .= "Permalink can not include '..'!"; }
elseif (preg_match("!/{2,}!", $permalink)) { $errors .= "Permalink can not include '//'!"; }
elseif ($isAlreadyUsed) { $errors .= sprintf("Permalink '%s' is already in use, please choose another!", htmlencode($permalink)); }
Next, if there's no error, add/update the permalink of the record.
The permalink_add() function will take care of adding/updating the new permalink and will return an error if there is any.
If it doesn't return an error, you can continue with your other process, ie: update record.
if (!$errors) {
// add/update permalink
$errors = permalink_add($permalink, $tableName, $recordNum);
if (!$error) {
// do other stuff here, ie: update record
}
}
Or you can do your other process first then add/update the permalink.
Finally, if there's an error, display it on your page.
NOTE: The code above are just examples and were not tested so you might need to tweak it more.
Let me know any questions!
Thanks,
Daryl Maximo
PHP Programmer - interactivetools.com