Fork me on GitHub

Categories ['__META__']['operator'] disappeared  Bottom

  • With reference to this thread http://community.zik…ewtopic-topic-57144:

    Browsing through the master branch on GitHub (https://github.com/z…lib/util/DBUtil.php) it seems that the ['__META__']['operator'] disappeared. It was used in _generateCategoryFilter(). This operator made it possible to AND the filtering of multiple categories. It was committed by Mateo here: (27130).

    Is there a new approach available to achieve the same thing in 1.3?

    The operator property is still available 1.2.5, but there is a bug there.

    The $n++ happens to early. I've modified it as follows:

    Code

    /**
         * Build a list of objects which are mapped to the specified categories
         *
         * @param categoryFilter   The category list to use for filtering
         * @param returnArray      Whether or not to return an array (optional) (default=false)
         *
         * @return The resulting string or array
         */

        function _generateCategoryFilter($tablename, $categoryFilter, $returnArray = false)
        {
            if (!$categoryFilter) {
                return '';
            }

            if (!pnModDBInfoLoad('Categories')) {
                return '';
            }

            // check the meta data
            if (isset($categoryFilter['__META__']['module'])) {
                $modname = $categoryFilter['__META__']['module'];
            } else {
                $modname = pnModGetName();
            }

            // 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
            Loader::loadClass('CategoryRegistryUtil');
            $propids = CategoryRegistryUtil::getRegisteredModuleCategoriesIds($modname, $tablename);

            // build the where clause
            $n = 1; // subquery counter
            $catmapobjtbl = DBUtil::getLimitedTablename('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}cmo_category_id='" . DataUtil::formatForStore($cat) . "'";
                    }
                    $wherecat = '(' . implode(' OR ', $wherecat) . ')';

                // if there's only one category ID
                } else {
                    $wherecat = "{$prefix}cmo_category_id='" . DataUtil::formatForStore($category) . "'";
                }

                // process the where depending of the operator
                if ($op == 'AND') {
                    $where[] = "cmo_obj_id IN (SELECT {$prefix}cmo_obj_id FROM $catmapobjtbl table$n WHERE {$prefix}cmo_reg_id = '".DataUtil::formatForStore($propids[$property])."' AND $wherecat)";
                } else {
                    $where[] = "(cmo_reg_id='" . DataUtil::formatForStore($propids[$property]) . "' AND $wherecat)";
                }

                $n++;
            }
            $where = "cmo_table='" . 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);
        }
  • there was a recent discussion of this item in the News project - see these notes in the repo discussion.

    from what I can tell, the operator property was never committed to the Core 1.3 branch.
  • Thanks Graig,

    This feature was actually part of the core since October 2009 (committed by Mateo) and requested by me. See this thread: http://community.zik…c-57144-start-0.htm.

    Don't understand why this was taken away for 1.3. In my opinion it's a very useful feature, taking it away is making functionality less in stead of more icon_confused . Guess I'll need to add a ticket to get it back.
  • If this get's back into the Core News should supply functionlaity to call it also. Craig removed the lines in the commit in the repo discussion.

    --
    campertoday.nl, Module development, Dutch Zikula Community
  • Good news: this is back in the core. If I understand correct a Doctrine alternative to the DBUtil implementation is also forthcoming. http://code.zikula.org/core/ticket/114

This list is based on users active over the last 60 minutes.