/** * 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; }
/** * Ensure that an array of objects exist in storage. Create any that don't, * return object_ids for all. All objects in the $objects array must be * of the same content type. * * @param mixed $objects An array of objects (or single obejct value). * Values typed as an integer are assumed to already * be an object_id. * @param mixed $type Either a string type_name or integer type_id * * @return array An array of object_ids. */ public function ensureObjects($objects, $type) { if (!is_array($objects)) { $objects = array($objects); } $objectIds = array(); $objectName = array(); $type = current($this->_typeManager->ensureTypes($type)); // Anything already typed as an integer is assumed to be an object id. foreach ($objects as $objectIndex => $object) { if (is_int($object)) { $objectIds[$objectIndex] = $object; } else { $objectName[$object] = $objectIndex; } } // Get the ids for any objects that already exist. try { if (count($objectName)) { $rows = $this->_db->selectAll('SELECT object_id, object_name FROM ' . $this->_t('objects') . ' WHERE object_name IN (' . implode(',', array_map(array($this->_db, 'quoteString'), array_keys($objectName))) . ') AND type_id = ' . $type); foreach ($rows as $row) { $objectIndex = $objectName[$row['object_name']]; unset($objectName[$row['object_name']]); $objectIds[$objectIndex] = $row['object_id']; } } // Create any objects that didn't already exist foreach ($objectName as $object => $objectIndex) { $objectIds[$objectIndex] = $this->_db->insert('INSERT INTO ' . $this->_t('objects') . ' (object_name, type_id) VALUES (' . $this->_db->quoteString($object) . ', ' . $type . ')'); } } catch (Horde_Db_Exception $e) { throw new Content_Exception($e); } return $objectIds; }