Dynamic Generation of Extra Field Definitions

A CMSMS developer sponsored me to create a UDT that would create News extra field definitions (checkbox) whenever a new FEU user was created. The plan was to have the detail template compare the current user with one of the checked users in the news article.

Remember to register the UDT in the Event Manager. Direct updates of the database should be tested in a side install, not on a live site.

global $gCms;
$db = $gCms->getDb();
# Get username from the FEU trigger.
# You can get a list of parameters by:
# var_dump($params); exit; 
$name = $params['name'];

# Make sure this field def doesn't exist. Don't want a duplicate
$sql = "SELECT id FROM " . cms_db_prefix() . "module_news_fielddefs
      WHERE name='$name'
        LIMIT 1;";
$dbresult =& $db->Execute($sql);

if ($dbresult && $row=$dbresult->FetchRow()) {
  return; # Field definition already exists. Abort!
}

# Field definition doesn't exist. Insert a new one
$sql = "INSERT INTO " . cms_db_prefix() . "module_news_fielddefs
        (`name`, `type`, `max_length`, `create_date`, `modified_date`, `item_order`, `public`)
        VALUES (?, ?, ?, ?, ?, ?, ?);";
$dbresult = $db->Execute(
   $sql, array($name, 'checkbox', 255, 0, 0, 1, 1)
); # The example above is for a 'checkbox' field definition. To change, # generate a couple of test definitions, then look at the table in # phpMyAdmin to see how they look, then update the code. 
ALL-INKL.COM - Webhosting Server Hosting Domain Provider

Comments