1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<?php
/**
* Copyright 2008-2014 Horde LLC (http://www.horde.org/)
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Horde_Content
*/
/**
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Rubinsky <mrubinsk@horde.org>
* @license http://www.horde.org/licenses/bsd BSD
* @category Horde
* @package Horde_Content
*/
class Content_Objects_Manager
{
/**
* Database adapter
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Tables
*
* @TODO: this should probably be populated by the responsible manager...
* @var array
*/
protected $_tables = array(
'objects' => 'rampage_objects',
);
/**
* Type manager
*
* @var Content_Types_Manager
*/
protected $_typeManager;
/**
* Constructor
*
* @param Horde_Db_Adapter $db The db adapter
* @param Content_Types_Manager $typeManager A content type manager
*
* @return Content_Objects_Manager
*/
public function __construct(Horde_Db_Adapter $db, Content_Types_Manager $typeManager)
{
$this->_db = $db;
$this->_typeManager = $typeManager;
}
/**
* Check for object existence without causing the objects to be created.
* Helps save queries for things like tags when we already know the object
* doesn't yet exist in rampage tables.
*
* @param mixed string|array $objects Either an object identifier or an
* array of them.
* @param mixed $type A type identifier. Either a string
* type name or the integer type_id.
*
* @return mixed Either a hash of object_id => object_names or false if
* the object(s) do not exist.
* @throws InvalidArgumentException, Content_Exception
*/
public function exists($objects, $type)
{
$type = current($this->_typeManager->ensureTypes($type));
if (!is_array($objects)) {
$objects = array($objects);
}
if (!count($objects)) {
return array();
}
// Ensure we take the object as a string indentifier.
foreach ($objects as &$object) {
$object = strval($object);
}
$params = $objects;
$params[] = $type;
try {
$ids = $this->_db->selectAssoc(
'SELECT object_id, object_name FROM ' . $this->_t('objects')
. ' WHERE object_name IN ('
. str_repeat('?,', count($objects) - 1) . '?)'
. ' AND type_id = ?', $params);
if ($ids) {
return $ids;
}
} catch (Horde_Db_Exception $e) {
throw new Content_Exception($e);
}
return false;
}
/**
* 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;
}
/**
* Shortcut for getting a table name.
*
* @param string $tableType
*
* @return string Configured table name.
*/
protected function _t($tableType)
{
return $this->_db->quoteTableName($this->_tables[$tableType]);
}
}
|