CommonTreeDropdown::import PHP Method

import() public method

Import a dropdown - check if already exists
public import ( array $input ) : the
$input array array of value to import (name or completename, ...)
return the ID of the new or existing dropdown (0 on failure)
    function import(array $input)
    {
        if (isset($input['name'])) {
            return parent::import($input);
        }
        if (!isset($input['completename']) || empty($input['completename'])) {
            return -1;
        }
        // Import a full tree from completename
        $names = explode('>', $input['completename']);
        $fk = $this->getForeignKeyField();
        $i = count($names);
        $parent = 0;
        foreach ($names as $name) {
            $i--;
            $name = trim($name);
            if (empty($name)) {
                // Skip empty name (completename starting/endind with >, double >, ...)
                continue;
            }
            $tmp['name'] = $name;
            $tmp[$fk] = $parent;
            if (isset($input['is_recursive'])) {
                $tmp['is_recursive'] = $input['is_recursive'];
            }
            if (isset($input['entities_id'])) {
                $tmp['entities_id'] = $input['entities_id'];
            }
            if (!$i) {
                // Other fields (comment, ...) only for last node of the tree
                foreach ($input as $key => $val) {
                    if ($key != 'completename') {
                        $tmp[$key] = $val;
                    }
                }
            }
            $parent = parent::import($tmp);
        }
        return $parent;
    }