Content_Types_Manager::ensureTypes PHP Method

ensureTypes() public method

Ensure that an array of types exist in storage. Create any that don't, return type_ids for all.
public ensureTypes ( mixed $types ) : array
$types mixed An array of types or single type value. Values typed as an integer are assumed to already be an type_id.
return array An array of type_ids.
    public function ensureTypes($types)
    {
        if (!is_array($types)) {
            $types = array($types);
        }
        $typeIds = array();
        $typeName = array();
        // Anything already typed as an integer is assumed to be a type id.
        foreach ($types as $typeIndex => $type) {
            if (is_int($type)) {
                $typeIds[$typeIndex] = $type;
            } else {
                $typeName[$type] = $typeIndex;
            }
        }
        try {
            // Get the ids for any types that already exist.
            if (count($typeName)) {
                $rows = $this->_db->selectAssoc('SELECT type_id, type_name FROM ' . $this->_t('types') . ' WHERE type_name IN (' . implode(',', array_map(array($this->_db, 'quoteString'), array_keys($typeName))) . ')');
                foreach ($rows as $id => $type) {
                    $typeIndex = $typeName[$type];
                    unset($typeName[$type]);
                    $typeIds[$typeIndex] = (int) $id;
                }
            }
            // Create any types that didn't already exist
            foreach ($typeName as $type => $typeIndex) {
                $typeIds[$typeIndex] = intval($this->_db->insert('INSERT INTO ' . $this->_t('types') . ' (type_name) VALUES (' . $this->_db->quoteString($type) . ')'));
            }
        } catch (Horde_Db_Exception $e) {
            throw new Content_Exception($e);
        }
        return $typeIds;
    }

Usage Example

Beispiel #1
0
 /**
  * Convenience method - if $object is an array, it is taken as an array of
  * 'object' and 'type' to pass to objectManager::ensureObjects() if it's a
  * scalar value, it's taken as the object_id and simply returned.
  */
 protected function _ensureObject($object)
 {
     if (is_array($object)) {
         $object = current($this->_objectManager->ensureObjects($object['object'], (int) current($this->_typeManager->ensureTypes($object['type']))));
     }
     return (int) $object;
 }
All Usage Examples Of Content_Types_Manager::ensureTypes
Content_Types_Manager