Example #1
0
 public function display()
 {
     $vars = $this->getContent();
     if (!empty($vars) && is_array($vars)) {
         extract($vars);
     }
     // Get requested layout
     if (empty($layout)) {
         $layout = 1;
         // default tree here
     }
     if (!empty($startmodule)) {
         // static behaviour
         list($module, $itemtype, $rootcid) = explode('.', $startmodule);
         if (empty($rootcid)) {
             $rootcids = null;
         } elseif (strpos($rootcid, ' ')) {
             $rootcids = explode(' ', $rootcid);
         } elseif (strpos($rootcid, '+')) {
             $rootcids = explode('+', $rootcid);
         } else {
             $rootcids = explode('-', $rootcid);
         }
     }
     // TODO: for multi-module pages, we'll need some other reference point(s)
     //       (e.g. cross-module categories defined in categories admin ?)
     // Get current module
     if (empty($module)) {
         if (xarVarIsCached('Blocks.categories', 'module')) {
             $modname = xarVarGetCached('Blocks.categories', 'module');
         }
         if (empty($modname)) {
             $modname = xarModGetName();
         }
     } else {
         $modname = $module;
     }
     $modid = xarModGetIDFromName($modname);
     if (empty($modid)) {
         return;
     }
     // Get current item type (if any)
     if (!isset($itemtype)) {
         if (xarVarIsCached('Blocks.categories', 'itemtype')) {
             $itemtype = xarVarGetCached('Blocks.categories', 'itemtype');
         } else {
             // try to get itemtype from input
             xarVarFetch('itemtype', 'isset', $itemtype, NULL, XARVAR_DONT_SET);
             if (empty($itemtype)) {
                 xarVarFetch('ptid', 'isset', $itemtype, NULL, XARVAR_DONT_SET);
             }
             // if
         }
     }
     if (empty($itemtype)) {
         $itemtype = null;
     }
     // Get current item id (if any)
     if (!isset($itemid)) {
         if (xarVarIsCached('Blocks.categories', 'itemid')) {
             $itemid = xarVarGetCached('Blocks.categories', 'itemid');
         } else {
             // try to get itemid from input
             xarVarFetch('itemid', 'isset', $itemid, NULL, XARVAR_DONT_SET);
             if (empty($itemid)) {
                 xarVarFetch('id', 'isset', $itemid, NULL, XARVAR_DONT_SET);
             }
             // if
         }
     }
     if (empty($itemid)) {
         $itemid = null;
     }
     if (isset($rootcids)) {
         $mastercids = $rootcids;
     } else {
         // Get number of categories for this module + item type
         $numcats = xarModAPIfunc('categories', 'user', 'countcatbases', array('module' => $modname, 'itemtype' => empty($itemtype) ? NULL : $itemtype));
         if (empty($numcats)) {
             // no categories to show here -> return empty output
             return;
         }
         // Get master cids for this module + item type
         $mastercids = xarModAPIfunc('categories', 'user', 'getallcatbases', array('module' => $modname, 'format' => 'cids', 'order' => 'cid', 'itemtype' => empty($itemtype) ? NULL : $itemtype));
         if (empty($mastercids)) {
             // no categories to show here -> return empty output
             return;
         }
         $mastercids = array_unique($mastercids);
         if (!empty($startmodule)) {
             $rootcids = $mastercids;
         }
     }
     // See if we need to show a count per category
     if (!isset($show_catcount)) {
         $show_catcount = 0;
     }
     // See if we need to show the children of current categories
     if (!isset($showchildren)) {
         $showchildren = 1;
     }
     // Get current category counts (optional array of cid => count)
     if (empty($show_catcount)) {
         $catcount = array();
     }
     if (empty($showempty) || !empty($show_catcount)) {
         // A 'deep count' sums the totals at each node with the totals of all descendants.
         if (xarVarIsCached('Blocks.categories', 'deepcount')) {
             $deepcount = xarVarGetCached('Blocks.categories', 'deepcount');
         } else {
             $deepcount = xarModAPIFunc('categories', 'user', 'deepcount', array('modid' => $modid, 'itemtype' => $itemtype));
             xarVarSetCached('Blocks.categories', 'deepcount', $deepcount);
         }
     }
     if (!empty($show_catcount)) {
         if (xarVarIsCached('Blocks.categories', 'catcount')) {
             $catcount = xarVarGetCached('Blocks.categories', 'catcount');
         } else {
             // Get number of items per category (for this module).
             // If show_catcount == 2 then add in all descendants too.
             if ($show_catcount == 1) {
                 // We want to display only children category counts.
                 $catcount = xarModAPIFunc('categories', 'user', 'groupcount', array('modid' => $modid, 'itemtype' => $itemtype));
             } else {
                 // We want to display the deep counts.
                 $catcount =& $deepcount;
             }
             xarVarSetCached('Blocks.categories', 'catcount', $catcount);
         }
     }
     // Specify type=... & func = ... arguments for xarModURL()
     if (empty($type)) {
         if (xarVarIsCached('Blocks.categories', 'type')) {
             $type = xarVarGetCached('Blocks.categories', 'type');
         }
         if (empty($type)) {
             $type = 'user';
         }
     }
     if (empty($func)) {
         if (xarVarIsCached('Blocks.categories', 'func')) {
             $func = xarVarGetCached('Blocks.categories', 'func');
         }
         if (empty($func)) {
             $func = 'view';
         }
     }
     // Get current categories
     if (xarVarIsCached('Blocks.categories', 'catid')) {
         $catid = xarVarGetCached('Blocks.categories', 'catid');
     }
     if (empty($catid)) {
         // try to get catid from input
         xarVarFetch('catid', 'isset', $catid, NULL, XARVAR_DONT_SET);
     }
     // turn $catid into $cids array (and set $andcids flag)
     $istree = 0;
     if (!empty($catid)) {
         // if we're viewing all items below a certain category, i.e. catid = _NN
         if (strstr($catid, '_')) {
             $catid = preg_replace('/_/', '', $catid);
             $istree = 1;
         }
         if (strpos($catid, ' ')) {
             $cids = explode(' ', $catid);
             $andcids = true;
         } elseif (strpos($catid, '+')) {
             $cids = explode('+', $catid);
             $andcids = true;
         } else {
             $cids = explode('-', $catid);
             $andcids = false;
         }
     } elseif (empty($cids)) {
         if (xarVarIsCached('Blocks.categories', 'cids')) {
             $cids = xarVarGetCached('Blocks.categories', 'cids');
         }
         if (xarVarIsCached('Blocks.categories', 'andcids')) {
             $andcids = xarVarGetCached('Blocks.categories', 'andcids');
         }
         if (empty($cids)) {
             // try to get cids from input
             xarVarFetch('cids', 'isset', $cids, NULL, XARVAR_DONT_SET);
             xarVarFetch('andcids', 'isset', $andcids, false, XARVAR_NOT_REQUIRED);
             if (empty($cids)) {
                 $cids = array();
                 if ((empty($module) || $module == $modname) && !empty($itemid)) {
                     $links = xarModAPIFunc('categories', 'user', 'getlinks', array('modid' => $modid, 'itemtype' => $itemtype, 'iids' => array($itemid)));
                     if (!empty($links) && count($links) > 0) {
                         $cids = array_keys($links);
                     }
                 }
             }
         }
     }
     if (count($cids) > 0) {
         $seencid = array();
         foreach ($cids as $cid) {
             if (empty($cid) || !is_numeric($cid)) {
                 continue;
             }
             $seencid[$cid] = 1;
         }
         $cids = array_keys($seencid);
     }
     $data = array();
     $data['cids'] = $cids;
     // pass information about current module, item type and item id (if any) to template
     $data['module'] = $modname;
     $data['itemtype'] = $itemtype;
     $data['itemid'] = $itemid;
     // pass information about current function to template
     $data['type'] = $type;
     $data['func'] = $func;
     $blockinfo['content'] = '';
     // Generate output
     switch ($layout) {
         case 3:
             // menu category
             $template = 'menu';
             break;
         case 2:
             // crumbtrails
             $template = 'trails';
             $data['cattrees'] = array();
             if (empty($cids) || sizeof($cids) <= 0) {
                 return;
             }
             // if
             $cid = current($cids);
             $data['cid'] = $cid;
             # Get category parents
             $parents = xarModAPIFunc('categories', 'user', 'getparents', array('cid' => $cid));
             if (empty($parents)) {
                 return;
             }
             // if
             $root = '';
             $parentid = 0;
             foreach ($parents as $id => $info) {
                 $publications = xarModAPIFunc('publications', 'user', 'getall', array('cid' => $info['cid'], 'ptid' => $itemtype, 'fields' => array('id', 'title')));
                 foreach ($publications as $k => $article) {
                     $publications[$article['title']] = $article['id'];
                     unset($publications[$k]);
                 }
                 // foreach
                 $label = xarVarPrepForDisplay($info['name']);
                 if (isset($publications[$label])) {
                     $link = xarModURL($modname, $type, 'display', array('ptid' => $itemtype, 'catid' => $info['cid'], 'id' => $publications[$label]));
                 } else {
                     $link = xarModURL($modname, $type, $func, array('itemtype' => $itemtype, 'catid' => $info['cid']));
                 }
                 // if
                 if (empty($root)) {
                     $link = xarModURL('', '', '');
                     $root = $label;
                 }
                 // if
                 if (!empty($catcount[$info['cid']])) {
                     $count = $catcount[$info['cid']];
                 } else {
                     $count = 0;
                 }
                 // if
                 $catparents[] = array('catlabel' => $label, 'catid' => $info['cid'], 'catlink' => $link, 'catcount' => $count);
             }
             // foreach
             $data['cattrees'][] = array('catparents' => $catparents);
             $data['crumbSeparator'] = '&#160;>&#160;';
             break;
         case 1:
             // tree
         // tree
         default:
             $template = 'tree';
             $data['cattrees'] = array();
             if (empty($cids) || sizeof($cids) <= 0) {
                 return;
             }
             // if
             $cid = current($cids);
             $cat = xarModAPIFunc('categories', 'user', 'getcatinfo', array('cid' => $cid));
             $blockinfo['title'] = xarVarPrepForDisplay($cat['name']);
             if (isset($cat['blockimage'])) {
                 $data['catimage'] = $cat['blockimage'];
             }
             // if
             # Get child categories
             $childrenCategories = xarModAPIFunc('categories', 'user', 'getchildren', array('cid' => $cid));
             # get all the pubtypes so we can digest the ids
             $pubtypes = xarModAPIFunc('publications', 'user', 'get_pubtypes', array());
             # get immediate items in current category
             $items = xarModAPIFunc('publications', 'user', 'getall', array('cids' => array($cid), 'fields' => array('id', 'pubtype_id', 'title')));
             $tmpPublications = array();
             foreach ($items as $k => $item) {
                 if (strtolower($item['title']) == strtolower($cat['name'])) {
                     unset($items[$k]);
                 } else {
                     $label = xarVarPrepForDisplay($item['title']);
                     $class = $item['id'] == $itemid ? 'xar-menu-item-current' : 'xar-menu-item';
                     $link = xarModURL($modname, $type, 'display', array('id' => $item['id'], 'itemtype' => $item['pubtype_id'], 'catid' => $cid));
                     $count = 0;
                     $items[$k] = array('label' => $label, 'id' => $item['id'], 'class' => $class, 'link' => $link);
                     $tmpPublications[$pubtypes[$item['pubtype_id']]['description']][] = $items[$k];
                 }
                 // if
             }
             // foreach
             $items = $tmpPublications;
             unset($tmpPublications);
             if (empty($itemid) && empty($andcids)) {
                 $link = '';
             }
             $catitems = array();
             if (!empty($childrenCategories) && count($childrenCategories) > 0) {
                 foreach ($childrenCategories as $child) {
                     $publications = xarModAPIFunc('publications', 'user', 'getall', array('cid' => $child['cid'], 'ptid' => $itemtype, 'fields' => array('id', 'title')));
                     foreach ($publications as $k => $article) {
                         $publications[$article['title']] = $article['id'];
                         unset($publications[$k]);
                     }
                     // foreach
                     $clabel = xarVarPrepForDisplay($child['name']);
                     if (isset($publications[$clabel])) {
                         $clink = xarModURL($modname, $type, 'display', array('ptid' => $itemtype, 'catid' => $child['cid'], 'id' => $publications[$clabel]));
                     } else {
                         $clink = xarModURL($modname, $type, $func, array('itemtype' => $itemtype, 'catid' => $child['cid']));
                     }
                     // if
                     if (!empty($catcount[$child['cid']])) {
                         $ccount = $catcount[$child['cid']];
                     } else {
                         $ccount = 0;
                     }
                     $catitems[] = array('catlabel' => $clabel, 'catid' => $child['cid'], 'catlink' => $clink, 'catcount' => $ccount, 'catchildren' => array());
                 }
                 // foreach
             }
             // if
             if (sizeof($catitems) > 0 || sizeof($items) > 0) {
                 $data['cattrees'][] = array('catitems' => $catitems, 'items' => $items);
             } else {
                 return;
             }
             // if
             break;
     }
     $data['blockid'] = $this->block_id;
     // The template base is set by this block if not already provided.
     // The base is 'nav-tree', 'nav-trails' or 'nav-prevnext', but allow
     // the admin to override this completely.
     $this->setTemplateBase('nav-' . $template);
     return $data;
 }
Example #2
0
/**
 * count number of items depending on additional module criteria
 *
 * @param $args['catid'] string of category id(s) that we're counting in, or
 * @param $args['cids'] array of cids that we are counting in (OR/AND)
 * @param $args['andcids'] true means AND-ing categories listed in cids
 *
 * @param $args['owner'] the ID of the author
 * @param $args['ptid'] publication type ID (for news, sections, reviews, ...)
 * @param $args['state'] array of requested status(es) for the publications
 * @param $args['startdate'] publications published at startdate or later
 *                           (unix timestamp format)
 * @param $args['enddate'] publications published before enddate
 *                         (unix timestamp format)
 * @return int number of items
 */
function publications_userapi_countitems($args)
{
    // Database information
    $dbconn = xarDB::getConn();
    // Get the field names and LEFT JOIN ... ON ... parts from publications
    // By passing on the $args, we can let leftjoin() create the WHERE for
    // the publications-specific columns too now
    $publicationsdef = xarModAPIFunc('publications', 'user', 'leftjoin', $args);
    // TODO: make sure this is SQL standard
    // Start building the query
    if ($dbconn->databaseType == 'sqlite') {
        $query = 'SELECT COUNT(*)
                  FROM ( SELECT DISTINCT ' . $publicationsdef['field'] . '
                         FROM ' . $publicationsdef['table'];
        // WATCH OUT, UNBALANCED
    } else {
        $query = 'SELECT COUNT(DISTINCT ' . $publicationsdef['field'] . ')';
        $query .= ' FROM ' . $publicationsdef['table'];
    }
    if (!isset($args['cids'])) {
        $args['cids'] = array();
    }
    if (!isset($args['andcids'])) {
        $args['andcids'] = false;
    }
    if (count($args['cids']) > 0 || !empty($args['catid'])) {
        // Load API
        if (!xarModAPILoad('categories', 'user')) {
            return;
        }
        // Get the LEFT JOIN ... ON ...  and WHERE (!) parts from categories
        $args['modid'] = xarModGetIDFromName('publications');
        if (isset($args['ptid']) && !isset($args['itemtype'])) {
            $args['itemtype'] = $args['ptid'];
        }
        $categoriesdef = xarModAPIFunc('categories', 'user', 'leftjoin', $args);
        $query .= ' LEFT JOIN ' . $categoriesdef['table'];
        $query .= ' ON ' . $categoriesdef['field'] . ' = ' . $publicationsdef['id'];
        $query .= $categoriesdef['more'];
        $docid = 1;
    }
    // Create the WHERE part
    $where = array();
    // we rely on leftjoin() to create the necessary publications clauses now
    if (!empty($publicationsdef['where'])) {
        $where[] = $publicationsdef['where'];
    }
    if (!empty($docid)) {
        // we rely on leftjoin() to create the necessary categories clauses
        $where[] = $categoriesdef['where'];
    }
    if (count($where) > 0) {
        $query .= ' WHERE ' . join(' AND ', $where);
    }
    // Balance parentheses
    if ($dbconn->databaseType == 'sqlite') {
        $query .= ')';
    }
    // Run the query - finally :-)
    $result =& $dbconn->Execute($query);
    if (!$result) {
        return;
    }
    if ($result->EOF) {
        return;
    }
    $num = $result->fields[0];
    $result->Close();
    return $num;
}
Example #3
0
/**
 * get a list of article authors depending on additional module criteria
 *
 * @param $args['cids'] array of cids that we are counting for (OR/AND)
 * @param $args['andcids'] true means AND-ing categories listed in cids
 *
 * @param $args['owner'] the ID of the author
 * @param $args['ptid'] publication type ID (for news, sections, reviews, ...)
 * @param $args['state'] array of requested status(es) for the publications
 * @param $args['startdate'] publications published at startdate or later
 *                           (unix timestamp format)
 * @param $args['enddate'] publications published before enddate
 *                         (unix timestamp format)
 * @return array of author id => author name
 */
function publications_userapi_getauthors($args)
{
    // Database information
    $dbconn = xarDB::getConn();
    // Get the field names and LEFT JOIN ... ON ... parts from publications
    // By passing on the $args, we can let leftjoin() create the WHERE for
    // the publications-specific columns too now
    $publicationsdef = xarModAPIFunc('publications', 'user', 'leftjoin', $args);
    // Load API
    if (!xarModAPILoad('roles', 'user')) {
        return;
    }
    // Get the field names and LEFT JOIN ... ON ... parts from users
    $usersdef = xarModAPIFunc('roles', 'user', 'leftjoin');
    // TODO: make sure this is SQL standard
    // Start building the query
    $query = 'SELECT DISTINCT ' . $publicationsdef['owner'] . ', ' . $usersdef['name'];
    $query .= ' FROM ' . $publicationsdef['table'];
    // Add the LEFT JOIN ... ON ... parts from users
    $query .= ' LEFT JOIN ' . $usersdef['table'];
    $query .= ' ON ' . $usersdef['field'] . ' = ' . $publicationsdef['owner'];
    if (!isset($args['cids'])) {
        $args['cids'] = array();
    }
    if (!isset($args['andcids'])) {
        $args['andcids'] = false;
    }
    if (count($args['cids']) > 0) {
        // Load API
        if (!xarModAPILoad('categories', 'user')) {
            return;
        }
        // Get the LEFT JOIN ... ON ...  and WHERE (!) parts from categories
        $args['modid'] = xarModGetIDFromName('publications');
        if (isset($args['ptid']) && !isset($args['itemtype'])) {
            $args['itemtype'] = $args['ptid'];
        }
        $categoriesdef = xarModAPIFunc('categories', 'user', 'leftjoin', $args);
        $query .= ' LEFT JOIN ' . $categoriesdef['table'];
        $query .= ' ON ' . $categoriesdef['field'] . ' = ' . $publicationsdef['id'];
        $query .= $categoriesdef['more'];
        $docid = 1;
    }
    // Create the WHERE part
    $where = array();
    // we rely on leftjoin() to create the necessary publications clauses now
    if (!empty($publicationsdef['where'])) {
        $where[] = $publicationsdef['where'];
    }
    if (!empty($docid)) {
        // we rely on leftjoin() to create the necessary categories clauses
        $where[] = $categoriesdef['where'];
    }
    if (count($where) > 0) {
        $query .= ' WHERE ' . join(' AND ', $where);
    }
    // Order by author name
    $query .= ' ORDER BY ' . $usersdef['name'] . ' ASC';
    // Run the query - finally :-)
    $result =& $dbconn->Execute($query);
    if (!$result) {
        return;
    }
    $authors = array();
    while (!$result->EOF) {
        list($uid, $name) = $result->fields;
        $authors[$uid] = array('id' => $uid, 'name' => $name);
        $result->MoveNext();
    }
    $result->Close();
    return $authors;
}