Backend\Core\Engine\Model::insertExtra PHP Method

insertExtra() public static method

Insert extra
public static insertExtra ( string $type, string $module, string $action = null, string $label = null, array $data = null, boolean $hidden = false, integer $sequence = null ) : integer
$type string What type do you want to insert, 'homepage', 'block' or 'widget'.
$module string The module you are inserting this extra for.
$action string The action this extra will use.
$label string Label which will be used when you want to connect this block.
$data array Containing extra variables.
$hidden boolean Should this extra be visible in frontend or not?
$sequence integer
return integer The new extra id
    public static function insertExtra($type, $module, $action = null, $label = null, $data = null, $hidden = false, $sequence = null)
    {
        $type = (string) $type;
        $module = (string) $module;
        // if action and label are empty, fallback to module
        $action = $action == null ? $module : (string) $action;
        $label = $label == null ? $module : (string) $label;
        // check if type is allowed
        if (!in_array($type, self::$allowedExtras)) {
            throw new Exception('Type is not allowed, choose from "' . implode(', ', self::$allowedExtras) . '".');
        }
        // get database
        $db = self::get('database');
        // sequence not given
        if ($sequence == null) {
            // redefine sequence: get maximum sequence for module
            $sequence = $db->getVar('SELECT MAX(i.sequence) + 1
                 FROM modules_extras AS i
                 WHERE i.module = ?', array($module));
            // sequence could not be found for module
            if (is_null($sequence)) {
                // redefine sequence: maximum sequence overall
                $sequence = $db->getVar('SELECT CEILING(MAX(i.sequence) / 1000) * 1000
                     FROM modules_extras AS i');
            }
        }
        // build extra
        $extra = array('module' => $module, 'type' => $type, 'label' => $label, 'action' => $action, 'data' => serialize((array) $data), 'hidden' => $hidden ? 'Y' : 'N', 'sequence' => $sequence);
        // return id for inserted extra
        return $db->insert('modules_extras', $extra);
    }

Usage Example

 /**
  * Insert an item in the database
  *
  * @param array $item
  * @return int
  */
 public static function insert(array $item)
 {
     $item['created_on'] = BackendModel::getUTCDate();
     $item['edited_on'] = BackendModel::getUTCDate();
     $db = BackendModel::get('database');
     // insert extra
     $item['extra_id'] = BackendModel::insertExtra('widget', 'Instagram', 'InstagramFeed');
     $item['id'] = (int) $db->insert('instagram_users', $item);
     // update extra (item id is now known)
     BackendModel::updateExtra($item['extra_id'], 'data', array('id' => $item['id'], 'extra_label' => \SpoonFilter::ucfirst(Language::lbl('Instagram', 'InstagramFeed')) . ': ' . $item['username'], 'edit_url' => BackendModel::createURLForAction('Edit', 'Instagram', null) . '&id=' . $item['id']));
     return $item['id'];
 }
All Usage Examples Of Backend\Core\Engine\Model::insertExtra