LISE API Auto Generate Alias and URL Topic is solved

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
webform
Power Poster
Power Poster
Posts: 460
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

LISE API Auto Generate Alias and URL

Post by webform »

I'm submitting to a LISE Instance in frontend via FormBuilder and a UDT found here on the forum.

And it works more or less as expected. But does not generate an alias or URL on creation based on the instance template for alias and URLs.
I've tried to use GenerateAlias() and GenerateSlug() and i do at least get the item_id inserted in the alias field, but nothing else. URL field is still empty.

My full UDT:

Code: Select all

/**
 * Load wanted ListItExtended instance, where you wan't to save items.
 * If instance can't be loaded, it will silently return.
 */
$mod = cmsms()->GetModuleInstance('LISELessons');
if(!is_object($mod))
    return;
 
/**
 * Intitate item with identifier 'alias', $params['title'] comes from FormBuilder.
 * Do duplication check with 'item_id', silently return, if item already in database.
 */    
$alias = munge_string_to_url($params['title'], true);
$obj = $mod->LoadItemByIdentifier('alias', $alias);
 
if($obj->item_id > 0)
    return;
 
/**
 * Fill previously initiated ListIt2Item object with values from form submission.
 * NOTICE: All params that are not known by ListIt2Item object are going to ignored.
 */        
$obj->title = $params['title'];
$obj->alias = $mod->GenerateAlias($obj->title, 'LISELessons');
$obj->slug = $mod->GenerateSlug('LISELessons');
 
foreach($params as $key => $value) {
    if(isset($obj->fielddefs[$key])){
        if($value != "[unspecified]"){
            $obj->$key = $value;
        }
    }
}
 
/**
 * Save this object to database by using ListItExtended API.
 */        
$mod->SaveItem($obj);
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1924
Joined: Mon Jan 29, 2007 4:47 pm

Re: LISE API Auto Generate Alias and URL

Post by Jo Morg »

Although, for most uses, that UDT would work fine (usually a slug is not required for items unless some form of frontend access is required), there are several considerations to have in mind when doing a frontend submission to LISE:

- security: make sure the submitters have any moderation mechanism (including using MAMS or other user access controller);
- aliases format: aliases MUST be unique as much as Ids, and in the form of $regex = '/^[a-zA-Z_][a-zA-Z0-9_]*$/';
- LISE old API is being deprecated and is being replaced by a better and more versatile one;

Now the old API was not maintained and probably would need fixing but it's not worth the effort now that a new one has been created, and, to be honest, there was also no attempt to do it given the lack of documentation and its use being too inconsistent. One of the things on my ToDo list is documenting as well as possible the new API.

Given the above I would try this:

Code: Select all

use LISE\api as LISE_API;
/**
 * Load wanted LISE instance, where you wan't to save items.
 * If instance can't be loaded, it will silently return.
 */
$mod =  \cms_utils::get_module('LISELessons');
if(!is_object($mod))
  return;

/**
 * now use the new LISE API
 */

$alias = LISE_API::GenerateAlias($params['title'], 'LISELessons');

/**
 * Initialize item with identifier 'alias', $params['title'] comes from FormBuilder.
 * Do duplication check with 'item_id', silently return, if item already in database.
 */
#$alias = munge_string_to_url($params['title'], true);
$obj = $mod->LoadItemByIdentifier('alias', $alias);

if($obj->item_id > 0)
  return;

/**
 * Fill previously initiated LISEItem object with values from form submission.
 * NOTICE: All params that are not known by LISEItem object are going to be ignored.
 */
$obj->title = $params['title'];
$obj->alias = $alias; # we already have one
$obj->slug = LISE_API::GenerateSlug($params['title'], 'LISELessons');

foreach($params as $key => $value) {
  if(isset($obj->fielddefs[$key])){
    if($value != "[unspecified]"){
      $obj->$key = $value;
    }
  }
}

/**
 * Save this object to database by using LISE API.
 */
$mod->SaveItem($obj);
It's not yet fully new API compliant but it should work. Also note: I didn't test this snippet so there might be typos or bugs, but for reference look at LISE/lib/class.api.php
HTH
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
webform
Power Poster
Power Poster
Posts: 460
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: LISE API Auto Generate Alias and URL

Post by webform »

Thanks a lot, Jo.

I do get an error when i try to save the UDT for the first line:

Code: Select all

syntax error, unexpected 'use'  (T_USE) in /var/www/lab/admin/editusertag.php(93) : eval()'d code on line 1
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1924
Joined: Mon Jan 29, 2007 4:47 pm

Re: LISE API Auto Generate Alias and URL

Post by Jo Morg »

mmm yeah the the UDTs work makes it a bit more difficult to use name spaces... how about this:

Code: Select all

#use LISE\api as LISE_API;
/**
 * Load wanted LISE instance, where you wan't to save items.
 * If instance can't be loaded, it will silently return.
 */
$mod =  \cms_utils::get_module('LISELessons');
if(!is_object($mod))
  return;

/**
 * now use the new LISE API
 */

$alias = LISE\api::GenerateAlias($params['title'], 'LISELessons');

/**
 * Initialize item with identifier 'alias', $params['title'] comes from FormBuilder.
 * Do duplication check with 'item_id', silently return, if item already in database.
 */
#$alias = munge_string_to_url($params['title'], true);
$obj = $mod->LoadItemByIdentifier('alias', $alias);

if($obj->item_id > 0)
  return;

/**
 * Fill previously initiated LISEItem object with values from form submission.
 * NOTICE: All params that are not known by LISEItem object are going to be ignored.
 */
$obj->title = $params['title'];
$obj->alias = $alias; # we already have one
$obj->slug = LISE\api::GenerateSlug($params['title'], 'LISELessons');

foreach($params as $key => $value) {
  if(isset($obj->fielddefs[$key])){
    if($value != "[unspecified]"){
      $obj->$key = $value;
    }
  }
}

/**
 * Save this object to database by using LISE API.
 */
$mod->SaveItem($obj);
I just commented out the statement for clarity and used and alternative syntax. At least it does save now :)
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
webform
Power Poster
Power Poster
Posts: 460
Joined: Sat Nov 25, 2006 3:39 pm
Location: Copenhagen, Denmark

Re: LISE API Auto Generate Alias and URL

Post by webform »

Doh! :-[

Thanks! I did wonder if it just was a comment.
Post Reply

Return to “Modules/Add-Ons”