Front-End Editing and Permalinks

By Perchpole - August 8, 2014

Hello, All

For the last few weeks I've been working on a way to add and update records through a front-end form. I've overcome a couple of issues along the way  and it seems to be coming together really well.

The only recurring issue is the way CMSB saves records. When you save a record in the back-end editor CMSB does all sorts of magic in the background. When you update though a front-end form, on the other hand, none of that magic seems to happen! This is particularly true in the case of categories.

Thankfully Dave gave me some code which appeared to solve many of the problems:

  // force update of category metadata for sorting (this may need to be updated in a much future version)
  $categoryTablename = 'category';
  $GLOBALS['escapedTableName'] = mysql_escape( $TABLE_PREFIX . $categoryTablename );
  $GLOBALS['schema']           = loadSchema($categoryTablename); // fake currently loaded schema
  if (!$GLOBALS['schema']) { die("Invalid table name '" .htmlencode($categoryTablename). "'"); }
  include_once(SCRIPT_DIR . '/lib/menus/default/common.php'); // load category menu functions 
  updateCategoryMetadata();


This forces things like breadcrumbs and lineage to update when creating a new category through the front-end form.

However, it does not address the bugbear of all my problems: permalinks.

When I add a category through the front-end form the permalinks field does not update automatically. I am required to go into the category via the back-end editor and save it manually. Only then does the permalink field update.

Obviously this defeats the entire purpose of having front-end editing capability!

Is there a fix?

:0/

Perchpole

By Perchpole - August 14, 2014

Hi, Greg -

Thanks for this. I very much appreciate your help.

I've tried to add your code to my existing code - but it doesn't seem to work.

  $tablename                        = 'pages';
  $recordNum                        = null;
  $where                            = null;
  $colsToValues                     = array(); // these fields get automatically mysql escaped unless you end fieldname with =
  $colsToValues['createdDate=']     = 'NOW()';
  $colsToValues['createdByUserNum'] = @$CURRENT_USER['num'];
  $colsToValues['name']             = 'New Page';
  $colsToValues['permalink']        = "newpage";
          
  $newRecordNum = mysql_insert($tablename, $colsToValues, true);
         
  $isNewRecord       = true;
  $oldRecord         = array();
  
  $GLOBALS['escapedTableName'] = mysql_escape( $TABLE_PREFIX . $tablename );
  $GLOBALS['schema']           = loadSchema($tablename); // fake currently loaded schema
  if (!$GLOBALS['schema']) { die("Invalid table name '" .htmlencode($tablename). "'"); }
  include_once(SCRIPT_DIR . '/lib/menus/default/common.php'); // load category menu functions 
  
  permalink_cms_onSaveUpdatePermalinks($tableName, $isNewRecord, $oldRecord, $newRecordNum);
  updateCategoryMetadata();

Note: This is the save code for creating a completely new page.

This code only works if I comment out the permalink_cms_onSaveUpdatePermalinks line.

What have I missed?

:0/

Perch

By gregThomas - August 14, 2014

Hey Perch,

I think you might need to set the permalink into the request array, but everything else looks correct.

  $tablename                        = 'pages';
  $recordNum                        = null;
  $where                            = null;
  $colsToValues                     = array(); // these fields get automatically mysql escaped unless you end fieldname with =
  $colsToValues['createdDate=']     = 'NOW()';
  $colsToValues['createdByUserNum'] = @$CURRENT_USER['num'];
  $colsToValues['name']             = 'New Page';
  $colsToValues['permalink']        = "newpage";
  $_REQUEST['permalink']            = $colsToValues['permalink'];
          
  $newRecordNum = mysql_insert($tablename, $colsToValues, true);
          
  $isNewRecord       = true;
  $oldRecord         = array();
  
  $GLOBALS['escapedTableName'] = mysql_escape( $TABLE_PREFIX . $tablename );
  $GLOBALS['schema']           = loadSchema($tablename); // fake currently loaded schema
  if (!$GLOBALS['schema']) { die("Invalid table name '" .htmlencode($tablename). "'"); }
  include_once(SCRIPT_DIR . '/lib/menus/default/common.php'); // load category menu functions 
  
  permalink_cms_onSaveUpdatePermalinks($tableName, $isNewRecord, $oldRecord, $newRecordNum);
  updateCategoryMetadata();

The function pulls the new permalink name directly from the REQUEST array, so we have to fake the POST/GET variable by setting it manually. 

Cheers!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - August 14, 2014

Thanks, greg - but it still won't run!

The bottom of the code looks like this now...

  ......
  $colsToValues['permalink']        = "newpage";
  $_REQUEST['permalink']            = $colsToValues['permalink'];
 
  $newRecordNum = mysql_insert($tablename, $colsToValues, true);
 
  $isNewRecord = true;
  $oldRecord   = array();
 
  $GLOBALS['escapedTableName'] = mysql_escape( $TABLE_PREFIX . $tablename );
  $GLOBALS['schema']           = loadSchema($tablename); // fake currently loaded schema
  if (!$GLOBALS['schema']) { die("Invalid table name '" .htmlencode($tablename). "'"); }
  include_once(SCRIPT_DIR . '/lib/menus/default/common.php'); // load category menu functions
  updateCategoryMetadata();
 
  mysql_update($tableName, $newRecordNum, null, array('permalink' => $_REQUEST['permalink']));
  permalink_cms_onSaveUpdatePermalinks($tableName, $isNewRecord, $oldRecord, $newRecordNum);

I added the hi-lighted lines - but something isn't right!

:0/

Perch

By gregThomas - August 14, 2014

Hey Perch, 

Are you getting any errors? Or is it generating a 'false positive', where no errors are displayed, but it isn't actually updating the record?

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - August 14, 2014

Greg -

With the permalink_cms_onSaveUpdatePermalinks line in the code it simply will not run.

If the line is removed a new page is added as expected.

:0/

Perch

By gregThomas - August 14, 2014

When I was testing I had a similar issue, which I resolved by enabling the permalinks plugin, have you got the permalinks plugin enabled at the moment?

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - August 14, 2014

Yes!

By Perchpole - August 14, 2014

Greg -

I think I've got it. The script wasn't running because I was providing a default name for the permalink. You can only do this once! If you try again it will reject the name because it already exists in the database.

What I need to do is (in the short term) find a way to append a random value to the end of my given permalink - to make it unique.

I'm creating a new page every time - so what would work best?

new-page_000001

new-page-000002

etc...?

Perch