/** * Build a list of objects which are mapped to the specified categories. * * @param string $tablename Treated table reference. * @param string $categoryFilter The category list to use for filtering. * @param boolean $returnArray Whether or not to return an array (optional) (default=false). * * @return mixed The resulting string or array. */ private static function _generateCategoryFilter($tablename, $categoryFilter, $returnArray = false) { if (!$categoryFilter) { return ''; } if (!ModUtil::dbInfoLoad('ZikulaCategoriesModule')) { return ''; } // check the meta data if (isset($categoryFilter['__META__']['module'])) { $modname = $categoryFilter['__META__']['module']; } else { $modname = ModUtil::getName(); } // check operator to use // when it's AND, the where contains subqueries if (isset($categoryFilter['__META__']['operator']) && in_array(strtolower($categoryFilter['__META__']['operator']), array('and', 'or'))) { $op = strtoupper($categoryFilter['__META__']['operator']); } else { $op = 'OR'; } unset($categoryFilter['__META__']); // get the properties IDs in the category register $propids = CategoryRegistryUtil::getRegisteredModuleCategoriesIds($modname, $tablename); // build the where clause $n = 1; // subquery counter $catmapobjtbl = 'categories_mapobj'; $where = array(); foreach ($categoryFilter as $property => $category) { $prefix = ''; if ($op == 'AND') { $prefix = "table{$n}."; } // this allows to have an array of categories IDs if (is_array($category)) { $wherecat = array(); foreach ($category as $cat) { $wherecat[] = "{$prefix}category_id='" . DataUtil::formatForStore($cat) . "'"; } $wherecat = '(' . implode(' OR ', $wherecat) . ')'; // if there's only one category ID } else { $wherecat = "{$prefix}category_id='" . DataUtil::formatForStore($category) . "'"; } // process the where depending of the operator if ($op == 'AND') { $where[] = "obj_id IN (SELECT {$prefix}obj_id FROM {$catmapobjtbl} table{$n} WHERE {$prefix}reg_id = '" . DataUtil::formatForStore($propids[$property]) . "' AND {$wherecat})"; } else { $where[] = "(reg_id='" . DataUtil::formatForStore($propids[$property]) . "' AND {$wherecat})"; } $n++; } $where = "tablename='" . DataUtil::formatForStore($tablename) . "' AND (" . implode(" {$op} ", $where) . ')'; // perform the query $objIds = DBUtil::selectFieldArray('categories_mapobj', 'obj_id', $where); // this ensures that we return an empty set if no objects are mapped to the requested categories if (!$objIds) { $objIds[] = -1; } if ($returnArray) { return $objIds; } return implode(',', $objIds); }
/** * Insert a categorization data object. * * @param array $obj The object we wish to store categorization data for. * @param string $tablename The object's tablename. * @param string $idcolumn The object's idcolumn (optional) (default='id'). * @param boolean $wasUpdateQuery True after an update and false after an insert. * * @return The result from the category data insert operation */ public static function storeObjectCategories($obj, $tablename, $idcolumn = 'id', $wasUpdateQuery = true) { if (!$obj) { throw new \Exception(__f('Invalid %1$s passed to %2$s.', array('object', __CLASS__ . '::' . __FUNCTION__))); } if (!$tablename) { throw new \Exception(__f('Invalid %1$s passed to %2$s.', array('tablename', __CLASS__ . '::' . __FUNCTION__))); } if (!$idcolumn) { throw new \Exception(__f('Invalid %1$s passed to %2$s.', array('idcolumn', __CLASS__ . '::' . __FUNCTION__))); } if (!ModUtil::dbInfoLoad('ZikulaCategoriesModule')) { return false; } if (!isset($obj['__CATEGORIES__']) || !is_array($obj['__CATEGORIES__']) || !$obj['__CATEGORIES__']) { return false; } if ($wasUpdateQuery) { self::deleteObjectCategories($obj, $tablename, $idcolumn); } // ensure that we don't store duplicate object mappings $values = array(); foreach ($obj['__CATEGORIES__'] as $k => $v) { if (isset($values[$v])) { unset($obj['__CATEGORIES__'][$k]); } else { $values[$v] = 1; } } // cache category id arrays to improve performance with DBUtil::(insert|update)ObjectArray() static $modTableCategoryIDs = array(); // Get the ids of the categories properties of the object $modname = isset($obj['__META__']['module']) ? $obj['__META__']['module'] : ModUtil::getName(); $reg_key = $modname . '_' . $tablename; if (!isset($modTableCategoryIDs[$reg_key])) { $modTableCategoryIDs[$reg_key] = CategoryRegistryUtil::getRegisteredModuleCategoriesIds($modname, $tablename); } $reg_ids = $modTableCategoryIDs[$reg_key]; $cobj = array(); $cobj['table'] = $tablename; $cobj['obj_idcolumn'] = $idcolumn; $res = true; foreach ($obj['__CATEGORIES__'] as $prop => $cat) { // if there's all the data and the Registry exists // the category is mapped if ($cat && $prop && isset($reg_ids[$prop])) { $cobj['id'] = ''; $cobj['modname'] = $modname; $cobj['obj_id'] = $obj[$idcolumn]; $cobj['category_id'] = $cat; $cobj['reg_id'] = $reg_ids[$prop]; $res = DBUtil::insertObject($cobj, 'categories_mapobj'); } } $dbtables = DBUtil::getTables(); if (isset($dbtables[$tablename])) { DBUtil::flushCache($tablename); } return (bool) $res; }
/** * Returns a list of all registries / properties for a given object type. * * @param string $args['ot'] The object type to retrieve (optional). * * @return array list of the registries (property name as key, id as value). */ public function getAllProperties(array $args = array()) { $objectType = $this->determineObjectType($args, 'getAllProperties'); $propertyIdsPerName = CategoryRegistryUtil::getRegisteredModuleCategoriesIds($this->name, ucfirst($objectType)); return $propertyIdsPerName; }