Esempio n. 1
0
function publications_user_view($args)
{
    // Get parameters
    if (!xarVarFetch('ptid', 'id', $ptid, xarModVars::get('publications', 'defaultpubtype'), XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('startnum', 'int:0', $startnum, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('cids', 'array', $cids, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('andcids', 'str', $andcids, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('catid', 'str', $catid, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('itemtype', 'id', $itemtype, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    // TODO: put the query string through a proper parser, so searches on multiple words can be done.
    if (!xarVarFetch('q', 'pre:trim:passthru:str:1:200', $q, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    // can't use list enum here, because we don't know which sorts might be used
    // True - but we can provide some form of validation and normalisation.
    // The original 'regexp:/^[\w,]*$/' lets through *any* non-space character.
    // This validation will accept a list of comma-separated words, and will lower-case, trim
    // and strip out non-alphanumeric characters from each word.
    if (!xarVarFetch('sort', 'strlist:,:pre:trim:lower:alnum', $sort, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('numcols', 'int:0', $numcols, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('owner', 'id', $owner, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('pubdate', 'str:1', $pubdate, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    // This may not be set via user input, only e.g. via template tags, API calls, blocks etc.
    //    if(!xarVarFetch('startdate','int:0', $startdate, NULL, XARVAR_NOT_REQUIRED)) {return;}
    //    if(!xarVarFetch('enddate',  'int:0', $enddate,   NULL, XARVAR_NOT_REQUIRED)) {return;}
    //    if(!xarVarFetch('where',    'str',   $where,     NULL, XARVAR_NOT_REQUIRED)) {return;}
    // Added to impliment an Alpha Pager
    if (!xarVarFetch('letter', 'pre:lower:passthru:str:1:20', $letter, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    // Override if needed from argument array (e.g. ptid, numitems etc.)
    extract($args);
    $pubtypes = xarModAPIFunc('publications', 'user', 'get_pubtypes');
    // We need a valid pubtype number here
    if (!is_numeric($ptid) || !isset($pubtypes[$ptid])) {
        return xarResponse::NotFound();
    }
    // Constants used throughout.
    //
    // publications module ID
    $c_modid = xarMod::getID('publications');
    // state: front page or approved
    $c_posted = array(PUBLICATIONS_STATE_FRONTPAGE, PUBLICATIONS_STATE_APPROVED);
    // Default parameters
    if (!isset($startnum)) {
        $startnum = 1;
    }
    // Check if we want the default 'front page'
    if (!isset($catid) && !isset($cids) && empty($ptid) && !isset($owner)) {
        $ishome = true;
        // default publication type
        $ptid = xarModVars::get('publications', 'defaultpubtype');
        // frontpage state
        $state = array(PUBLICATIONS_STATE_FRONTPAGE);
    } else {
        $ishome = false;
        // frontpage or approved state
        $state = $c_posted;
    }
    // Get the publication type for this display
    $data['pubtypeobject'] = DataObjectMaster::getObject(array('name' => 'publications_types'));
    $data['pubtypeobject']->getItem(array('itemid' => $ptid));
    // Get the settings of this publication type
    $data['settings'] = xarMod::apiFunc('publications', 'user', 'getsettings', array('ptid' => $ptid));
    // Get the template for this publication type
    if ($ishome) {
        $data['template'] = 'frontpage';
    } else {
        $data['template'] = $data['pubtypeobject']->properties['template']->getValue();
    }
    $isdefault = 0;
    // check default view for this type of publications
    if (empty($catid) && empty($cids) && empty($owner) && empty($sort)) {
        if (substr($data['settings']['defaultview'], 0, 1) == 'c') {
            $catid = substr($data['settings']['defaultview'], 1);
        }
    }
    // Do not transform titles if we are not transforming output at all.
    if (empty($data['settings']['do_transform'])) {
        $data['settings']['dotitletransform'] = 0;
    }
    // Page template for frontpage or depending on publication type (optional)
    // Note : this cannot be overridden in templates
    if (!empty($data['settings']['page_template'])) {
        xarTplSetPageTemplateName($data['settings']['page_template']);
    }
    if (empty($data['settings']['defaultsort'])) {
        $defaultsort = 'date';
    } else {
        $defaultsort = $data['settings']['defaultsort'];
    }
    if (empty($sort)) {
        $sort = $defaultsort;
    }
    // TODO: show this *after* category list when we start from categories :)
    // Navigation links
    $data['publabel'] = xarML('Publication');
    $data['publinks'] = xarModAPIFunc('publications', 'user', 'getpublinks', array('ptid' => $ishome ? '' : $ptid, 'state' => $c_posted, 'count' => $data['settings']['show_pubcount']));
    $data['pager'] = '';
    // Add Sort to data passed to template so that we can automatically turn on alpha pager, if needed
    $data['sort'] = $sort;
    // Add current display letter, so that we can highlight the current filter in the alpha pager
    $data['letter'] = $letter;
    // Get the users requested number of stories per page.
    // If user doesn't care, use the site default
    if (xarUserIsLoggedIn()) {
        // TODO: figure how to let users specify their settings
        // COMMENT: if the settings were split into separate module variables,
        // then they could all be individually over-ridden by each user.
        //$numitems = xarModUserGetVar('items_per_page');
    }
    if (empty($numitems)) {
        if (!empty($settings['items_per_page'])) {
            $numitems = $settings['items_per_page'];
        } else {
            $numitems = 20;
        }
    }
    // turn $catid into $cids array and set $andcids flag
    if (!empty($catid)) {
        if (strpos($catid, ' ')) {
            $cids = explode(' ', $catid);
            $andcids = true;
        } elseif (strpos($catid, '+')) {
            $cids = explode('+', $catid);
            $andcids = true;
        } elseif (strpos($catid, '-')) {
            $cids = explode('-', $catid);
            $andcids = false;
        } else {
            $cids = array($catid);
            if (strstr($catid, '_')) {
                $andcids = false;
                // don't combine with current category
            } else {
                $andcids = true;
            }
        }
    } else {
        if (empty($cids)) {
            $cids = array();
        }
        if (!isset($andcids)) {
            $andcids = true;
        }
    }
    // rebuild $catid in standard format again
    $catid = null;
    if (count($cids) > 0) {
        $seencid = array();
        foreach ($cids as $cid) {
            // make sure cids are numeric
            if (!empty($cid) && preg_match('/^_?[0-9]+$/', $cid)) {
                $seencid[$cid] = 1;
            }
        }
        $cids = array_keys($seencid);
        sort($cids, SORT_NUMERIC);
        if ($andcids) {
            $catid = join('+', $cids);
        } else {
            $catid = join('-', $cids);
        }
    }
    // every field you always wanted to know about but were afraid to ask for :)
    $extra = array();
    //    $extra[] = 'author';
    // Note: we always include cids for security checks now (= performance impact if show_categories was 0)
    $extra[] = 'cids';
    if ($data['settings']['show_hitcount']) {
        $extra[] = 'counter';
    }
    if ($data['settings']['show_ratings']) {
        $extra[] = 'rating';
    }
    $now = time();
    if (empty($startdate) || !is_numeric($startdate) || $startdate > $now) {
        $startdate = null;
    }
    if (empty($enddate) || !is_numeric($enddate) || $enddate > $now) {
        $enddate = $now;
    }
    if (empty($pubdate) || !preg_match('/^\\d{4}(-\\d+(-\\d+|)|)$/', $pubdate)) {
        $pubdate = null;
    }
    if (empty($where)) {
        $where = null;
    }
    // Modify the where clause if an Alpha filter has been specified.
    if (!empty($letter)) {
        // We will allow up to three initial letters, anything more than that is assumed to be 'Other'.
        // Need to also be very wary of SQL injection, since we are not using bind variables here.
        // TODO: take into account international characters.
        if (preg_match('/^[a-z]{1,3}$/i', $letter)) {
            $extrawhere = "title LIKE '{$letter}%'";
        } else {
            // Loop through the alphabet for the 'not in' part.
            $letterwhere = array();
            for ($i = ord('a'); $i <= ord('z'); $i++) {
                $letterwhere[] = "title NOT LIKE '" . chr($i) . "%'";
            }
            $extrawhere = implode(' and ', $letterwhere);
        }
        if ($where == null) {
            $where = $extrawhere;
        } else {
            $where .= $extrawhere;
        }
    }
    // Get publications
    $publications = xarModAPIFunc('publications', 'user', 'getall', array('startnum' => $startnum, 'cids' => $cids, 'andcids' => $andcids, 'ptid' => isset($ptid) ? $ptid : null, 'owner' => $owner, 'state' => $state, 'sort' => $sort, 'extra' => $extra, 'where' => $where, 'search' => $q, 'numitems' => $numitems, 'pubdate' => $pubdate, 'startdate' => $startdate, 'enddate' => $enddate));
    if (!is_array($publications)) {
        throw new Exception('Failed to retrieve publications');
    }
    // TODO : support different 'index' templates for different types of publications
    //        (e.g. News, Sections, ...), depending on what "view" the user
    //        selected (per category, per publication type, a combination, ...) ?
    if (!empty($owner)) {
        $data['author'] = xarUserGetVar('name', $owner);
        if (empty($data['author'])) {
            xarErrorHandled();
            $data['author'] = xarML('Unknown');
        }
    }
    if (!empty($pubdate)) {
        $data['pubdate'] = $pubdate;
    }
    // Save some variables to (temporary) cache for use in blocks etc.
    xarVarSetCached('Blocks.publications', 'ptid', $ptid);
    xarVarSetCached('Blocks.publications', 'cids', $cids);
    xarVarSetCached('Blocks.publications', 'owner', $owner);
    if (isset($data['author'])) {
        xarVarSetCached('Blocks.publications', 'author', $data['author']);
    }
    if (isset($data['pubdate'])) {
        xarVarSetCached('Blocks.publications', 'pubdate', $data['pubdate']);
    }
    // TODO: add this to publications configuration ?
    if ($ishome) {
        $data['ptid'] = null;
        if (xarSecurityCheck('SubmitPublications', 0)) {
            $data['submitlink'] = xarModURL('publications', 'admin', 'new');
        }
    } else {
        $data['ptid'] = $ptid;
        if (!empty($ptid)) {
            $curptid = $ptid;
        } else {
            $curptid = 'All';
        }
        if (count($cids) > 0) {
            foreach ($cids as $cid) {
                if (xarSecurityCheck('SubmitPublications', 0, 'Publication', "{$curptid}:{$cid}:All:All")) {
                    $data['submitlink'] = xarModURL('publications', 'admin', 'new', array('ptid' => $ptid, 'catid' => $catid));
                    break;
                }
            }
        } elseif (xarSecurityCheck('SubmitPublications', 0, 'Publication', "{$curptid}:All:All:All")) {
            $data['submitlink'] = xarModURL('publications', 'admin', 'new', array('ptid' => $ptid));
        }
    }
    $data['cids'] = $cids;
    $data['catid'] = $catid;
    xarVarSetCached('Blocks.categories', 'module', 'publications');
    xarVarSetCached('Blocks.categories', 'itemtype', $ptid);
    xarVarSetCached('Blocks.categories', 'cids', $cids);
    if (!empty($ptid) && !empty($pubtypes[$ptid]['description'])) {
        xarVarSetCached('Blocks.categories', 'title', $pubtypes[$ptid]['description']);
        // Note : this gets overriden by the categories navigation if necessary
        xarTplSetPageTitle(xarVarPrepForDisplay($pubtypes[$ptid]['description']));
    }
    // optional category count
    if ($data['settings']['show_catcount']) {
        if (!empty($ptid)) {
            $pubcatcount = xarModAPIFunc('publications', 'user', 'getpubcatcount', array('state' => $c_posted, 'ptid' => $ptid));
            if (isset($pubcatcount[$ptid])) {
                xarVarSetCached('Blocks.categories', 'catcount', $pubcatcount[$ptid]);
            }
            unset($pubcatcount);
        } else {
            $pubcatcount = xarModAPIFunc('publications', 'user', 'getpubcatcount', array('state' => $c_posted, 'reverse' => 1));
            if (isset($pubcatcount) && count($pubcatcount) > 0) {
                $catcount = array();
                foreach ($pubcatcount as $cat => $count) {
                    $catcount[$cat] = $count['total'];
                }
                xarVarSetCached('Blocks.categories', 'catcount', $catcount);
            }
            unset($pubcatcount);
        }
    } else {
        // xarVarSetCached('Blocks.categories','catcount',array());
    }
    // retrieve the number of comments for each article
    if (xarModIsAvailable('coments')) {
        if ($data['settings']['show_comments']) {
            $idlist = array();
            foreach ($publications as $article) {
                $idlist[] = $article['id'];
            }
            $numcomments = xarModAPIFunc('comments', 'user', 'get_countlist', array('modid' => $c_modid, 'objectids' => $idlist));
        }
    }
    // retrieve the keywords for each article
    if (xarModIsAvailable('coments')) {
        if ($data['settings']['show_keywords']) {
            $idlist = array();
            foreach ($publications as $article) {
                $idlist[] = $article['id'];
            }
            $keywords = xarModAPIFunc('keywords', 'user', 'getmultiplewords', array('modid' => $c_modid, 'objectids' => $idlist, 'itemtype' => $ptid));
        }
    }
    /* ------------------------------------------------------------
        // retrieve the categories for each article
        $catinfo = array();
        if ($show_categories) {
            $cidlist = array();
            foreach ($publications as $article) {
                if (!empty($article['cids']) && count($article['cids']) > 0) {
                     foreach ($article['cids'] as $cid) {
                         $cidlist[$cid] = 1;
                     }
                }
            }
            if (count($cidlist) > 0) {
                $catinfo = xarModAPIFunc('categories','user','getcatinfo', array('cids' => array_keys($cidlist)));
                // get root categories for this publication type
                // get base categories for all if needed
                $catroots = xarModAPIFunc('publications', 'user', 'getrootcats',
                    array('ptid' => $ptid, 'all' => true)
                );
            }
            foreach ($catinfo as $cid => $info) {
                $catinfo[$cid]['name'] = xarVarPrepForDisplay($info['name']);
                $catinfo[$cid]['link'] = xarModURL('publications', 'user', 'view',
                    array('ptid' => $ptid, 'catid' => (($catid && $andcids) ? $catid . '+' . $cid : $cid) )
                );
    
                // only needed when sorting by root category id
                $catinfo[$cid]['root'] = 0; // means not found under a root category
                // only needed when sorting by root category order
                $catinfo[$cid]['order'] = 0; // means not found under a root category
                $rootidx = 1;
                foreach ($catroots as $rootcat) {
                    // see if we're a child category of this rootcat (cfr. Celko model)
                    if ($info['left'] >= $rootcat['catleft'] && $info['left'] < $rootcat['catright']) {
                        // only needed when sorting by root category id
                        $catinfo[$cid]['root'] = $rootcat['catid'];
                        // only needed when sorting by root category order
                        $catinfo[$cid]['order'] = $rootidx;
                        break;
                    }
                    $rootidx++;
                }
            }
            // needed for sort function below
            $GLOBALS['artviewcatinfo'] = $catinfo;
        }
    
        $number = 0;
        foreach ($publications as $article)
        {
            // TODO: don't include ptid and catid if we don't use short URLs
            // link to article
            $article['link'] = xarModURL('publications', 'user', 'display',
                // don't include pubtype id if we're navigating by category
                array(
                    'ptid' => empty($ptid) ? null : $article['pubtype_id'],
                    'catid' => $catid,
                    'id' => $article['id']
                )
            );
    
            // N words/bytes more in article
            if (!empty($article['body'])) {
                // note : this is only an approximate number
                $wordcount = count(preg_split("/\s+/", strip_tags($article['body']), -1, PREG_SPLIT_NO_EMPTY));
                $article['words'] = $wordcount;
    
                // byte-count is less CPU-intensive -> make configurable ?
                $article['bytes'] = strlen($article['body']);
            } else {
                $article['words'] = 0;
                $article['bytes'] = 0;
            }
    
            // current publication type
            $curptid = $article['pubtype_id'];
    
            // TODO: make configurable?
            $article['redirect'] = xarModURL('publications', 'user', 'redirect',
                array('ptid' => $curptid, 'id' => $article['id'])
            );
    
    
            // multi-column display (default from left to right, then from top to bottom)
            $article['number'] = $number;
            if (!empty($settings['number_of_columns'])) {
                $col = $number % $settings['number_of_columns'];
            } else {
                $col = 0;
            }
    
            // RSS Processing
            $current_theme = xarVarGetCached('Themes.name', 'CurrentTheme');
            if (($current_theme == 'rss') or ($current_theme == 'atom')){
                $article['rsstitle'] = htmlspecialchars($article['title']);
                //$article['rssdate'] = strtotime($article['date']);
                $article['rsssummary'] = preg_replace('<br />', "\n", $article['summary']);
                $article['rsssummary'] = xarVarPrepForDisplay(strip_tags($article['rsssummary']));
                $article['rsscomment'] = xarModURL('comments', 'user', 'display', array('modid' => $c_modid, 'objectid' => $article['id']));
                // $article['rsscname'] = htmlspecialchars($item['cname']);
                // <category>#$rsscname#</category>
            }
    
            // TODO: clean up depending on field format
            if ($do_transform) {
                $article['itemtype'] = $article['pubtype_id'];
                // TODO: what about transforming DD fields?
                if ($title_transform) {
                    $article['transform'] = array('title', 'summary', 'body', 'notes');
                } else {
                    $article['transform'] = array('summary', 'body', 'notes');
                }
                $article = xarModCallHooks('item', 'transform', $article['id'], $article, 'publications');
            }
    
            $data['titles'][$article['id']] = $article['title'];
    
            // fill in the summary template for this article
            $summary_template = $pubtypes[$article['pubtype_id']]['name'];
            $number++;echo $number;
        }
    ------------------------------------------------------------ */
    unset($publications);
    // TODO: verify for other URLs as well
    if ($ishome) {
        if (!empty($numcols) && $numcols > 1) {
            // if we're currently showing more than 1 column
            $data['showcols'] = 1;
        } else {
            $defaultcols = $data['settings']['number_of_columns'];
            if ($defaultcols > 1) {
                // if the default number of columns is more than 1
                $data['showcols'] = $defaultcols;
            }
        }
    }
    // Specific layout within a template (optional)
    if (isset($layout)) {
        $data['layout'] = $layout;
    }
    // Get the publications we want to view
    $data['object'] = DataObjectMaster::getObject(array('name' => $data['pubtypeobject']->properties['name']->value));
    $data['objectname'] = $data['pubtypeobject']->properties['name']->value;
    $data['ptid'] = $ptid;
    //    $object = DataObjectMaster::getObjectList(array('name' => $data['pubtypeobject']->properties['name']->value));
    //    $data['items'] = $object->getItems();
    $data['object'] = DataObjectMaster::getObjectList(array('name' => $data['pubtypeobject']->properties['name']->value));
    // Get the items here
    //    $items = $data['object']->getItems();
    /* We're doing this in the template now
        // Only show top level documents, not translations
        sys::import('xaraya.structures.query');
        $q = new Query();
        $q->eq('parent_id',0);
        $q->eq('pubtype_id',$ptid);
        $q->eq('state',3);
    
        // Suppress deleted items
        // Remove this once listing property works with dataobject access
        $q->ne('state',0);
        $data['conditions'] = $q;
    */
    // Set the page template if needed
    if (!empty($data['settings']['page_template'])) {
        $pagename = $data['settings']['page_template'];
        $position = strpos($pagename, '.');
        if ($position === false) {
            $pagetemplate = $pagename;
        } else {
            $pagetemplate = substr($pagename, 0, $position);
        }
        xarTpl::setPageTemplateName($pagetemplate);
    }
    // Throw all the relevant settings we are using into the cache
    //    $data['settings']['pubtypeobject'] = $data['pubtypeobject']->properties['configuration']->getValue();
    //    xarCore::setCached('publications', 'context' . $ptid, $data['settings']);
    return xarTplModule('publications', 'user', 'view', $data, $data['template']);
}
Esempio n. 2
0
function publications_admin_display($args)
{
    // Get parameters from user
    // this is used to determine whether we come from a pubtype-based view or a
    // categories-based navigation
    // Note we support both id and itemid
    if (!xarVarFetch('name', 'str', $name, '', XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('ptid', 'id', $ptid, NULL, XARVAR_DONT_SET)) {
        return;
    }
    if (!xarVarFetch('itemid', 'id', $itemid, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('id', 'id', $id, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('page', 'int:1', $page, NULL, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('translate', 'int:1', $translate, 1, XARVAR_NOT_REQUIRED)) {
        return;
    }
    if (!xarVarFetch('layout', 'str:1', $layout, 'detail', XARVAR_NOT_REQUIRED)) {
        return;
    }
    // Override xarVarFetch
    extract($args);
    //The itemid var takes precedence if it exiata
    if (isset($itemid)) {
        $id = $itemid;
    }
    # --------------------------------------------------------
    #
    # If no ID supplied, try getting the id of the default page.
    #
    if (empty($id)) {
        $id = xarModVars::get('publications', 'defaultpage');
    }
    # --------------------------------------------------------
    #
    # Get the ID of the translation if required
    #
    // First save the "untranslated" id
    xarVarSetCached('Blocks.publications', 'current_base_id', $id);
    if ($translate) {
        $id = xarMod::apiFunc('publications', 'user', 'gettranslationid', array('id' => $id));
    }
    # --------------------------------------------------------
    #
    # If still no ID, check if we are trying to display a pubtype
    #
    if (empty($name) && empty($ptid) && empty($id)) {
        // Nothing to be done
        $id = xarModVars::get('publications', 'notfoundpage');
    } elseif (empty($id)) {
        // We're missing an id but can get a pubtype: jump to the pubtype view
        xarController::redirect(xarModURL('publications', 'user', 'view'));
    }
    # --------------------------------------------------------
    #
    # If still no ID, we have come to the end of the line
    #
    if (empty($id)) {
        return xarResponse::NotFound();
    }
    # --------------------------------------------------------
    #
    # We have an ID, now first get the page
    #
    // Here we get the publication type first, and then from that the page
    // Perhaps more efficient to get the page directly?
    $ptid = xarMod::apiFunc('publications', 'user', 'getitempubtype', array('itemid' => $id));
    // An empty publication type means the page does not exist
    if (empty($ptid)) {
        return xarResponse::NotFound();
    }
    /*    if (empty($name) && empty($ptid)) return xarResponse::NotFound();
    
        if(empty($ptid)) {
            $publication_type = DataObjectMaster::getObjectList(array('name' => 'publications_types'));
            $where = 'name = ' . $name;
            $items = $publication_type->getItems(array('where' => $where));
            $item = current($items);
            $ptid = $item['id'];
        }
    */
    $pubtypeobject = DataObjectMaster::getObject(array('name' => 'publications_types'));
    $pubtypeobject->getItem(array('itemid' => $ptid));
    $data['object'] = DataObjectMaster::getObject(array('name' => $pubtypeobject->properties['name']->value));
    //    $id = xarMod::apiFunc('publications','user','gettranslationid',array('id' => $id));
    $itemid = $data['object']->getItem(array('itemid' => $id));
    # --------------------------------------------------------
    #
    # Are we allowed to see this page?
    #
    $accessconstraints = unserialize($data['object']->properties['access']->value);
    $access = DataPropertyMaster::getProperty(array('name' => 'access'));
    $allow = $access->check($accessconstraints['display']);
    $nopublish = time() < $data['object']->properties['start_date']->value || time() > $data['object']->properties['end_date']->value && !$data['object']->properties['no_end']->value;
    // If no access, then bail showing a forbidden or an empty page
    if (!$allow || $nopublish) {
        if ($accessconstraints['display']['failure']) {
            return xarResponse::Forbidden();
        } else {
            return xarTplModule('publications', 'user', 'empty');
        }
    }
    # --------------------------------------------------------
    #
    # If this is a redirect page, then send it on its way now
    #
    $redirect_type = $data['object']->properties['redirect_flag']->value;
    if ($redirect_type == 1) {
        // This is a simple redirect to another page
        try {
            $url = $data['object']->properties['redirect_url']->value;
            // Check if this is a Xaraya function
            $pos = strpos($url, 'xar');
            if ($pos === 0) {
                eval('$url = ' . $url . ';');
            }
            xarController::redirect($url, 301);
        } catch (Exception $e) {
            return xarResponse::NotFound();
        }
    } elseif ($redirect_type == 2) {
        // This displays a page of a different module
        // If this is from a link of a redirect child page, use the child param as new URL
        if (!xarVarFetch('child', 'str', $child, NULL, XARVAR_NOT_REQUIRED)) {
            return;
        }
        if (!empty($child)) {
            // Turn entities into amps
            $url = urldecode($child);
        } else {
            $url = $data['object']->properties['proxy_url']->value;
        }
        // Bail if the URL is bad
        try {
            // Check if this is a Xaraya function
            $pos = strpos($url, 'xar');
            if ($pos === 0) {
                eval('$url = ' . $url . ';');
            }
            $params = parse_url($url);
            $params['query'] = preg_replace('/&amp;/', '&', $params['query']);
        } catch (Exception $e) {
            return xarResponse::NotFound();
        }
        // If this is an external link, show it without further processing
        if (!empty($params['host']) && $params['host'] != xarServer::getHost() && $params['host'] . ":" . $params['port'] != xarServer::getHost()) {
            xarController::redirect($url, 301);
        } else {
            parse_str($params['query'], $info);
            $other_params = $info;
            unset($other_params['module']);
            unset($other_params['type']);
            unset($other_params['func']);
            unset($other_params['child']);
            try {
                $page = xarMod::guiFunc($info['module'], 'user', $info['func'], $other_params);
            } catch (Exception $e) {
                return xarResponse::NotFound();
            }
            // Debug
            // echo xarModURL($info['module'],'user',$info['func'],$other_params);
            # --------------------------------------------------------
            #
            # For proxy pages: the transform of the subordinate function's template
            #
            // Find the URLs in submits
            $pattern = '/(action)="([^"\\r\\n]*)"/';
            preg_match_all($pattern, $page, $matches);
            $pattern = array();
            $replace = array();
            foreach ($matches[2] as $match) {
                $pattern[] = '%</form%';
                $replace[] = '<input type="hidden" name="return_url" id="return_url" value="' . urlencode(xarServer::getCurrentURL()) . '"/><input type="hidden" name="child" value="' . urlencode($match) . '"/></form';
            }
            $page = preg_replace($pattern, $replace, $page);
            $pattern = '/(action)="([^"\\r\\n]*)"/';
            $page = preg_replace_callback($pattern, create_function('$matches', 'return $matches[1]."=\\"".xarServer::getCurrentURL()."\\"";'), $page);
            // Find the URLs in links
            $pattern = '/(href)="([^"\\r\\n]*)"/';
            $page = preg_replace_callback($pattern, create_function('$matches', 'return $matches[1]."=\\"".xarServer::getCurrentURL(array("child" => urlencode($matches[2])))."\\"";'), $page);
            return $page;
        }
    }
    # --------------------------------------------------------
    #
    # If this is a bloccklayout page, then process it
    #
    if ($data['object']->properties['pagetype']->value == 2) {
        // Get a copy of the compiler
        sys::import('xaraya.templating.compiler');
        $blCompiler = XarayaCompiler::instance();
        // Get the data fields
        $fields = array();
        $sourcefields = array('title', 'description', 'summary', 'body1', 'body2', 'body3', 'body4', 'body5', 'notes');
        $prefix = strlen('publications.') - 1;
        foreach ($data['object']->properties as $prop) {
            if (in_array(substr($prop->source, $prefix), $sourcefields)) {
                $fields[] = $prop->name;
            }
        }
        // Run each template field through the compiler
        foreach ($fields as $field) {
            try {
                $tplString = '<xar:template xmlns:xar="http://xaraya.com/2004/blocklayout">';
                $tplString .= xarMod::apiFunc('publications', 'user', 'prepareforbl', array('string' => $data['object']->properties[$field]->value));
                $tplString .= '</xar:template>';
                $tplString = $blCompiler->compilestring($tplString);
                // We don't allow passing $data to the template for now
                $tpldata = array();
                $tplString = xarTplString($tplString, $tpldata);
            } catch (Exception $e) {
                var_dump($tplString);
            }
            $data['object']->properties[$field]->value = $tplString;
        }
    }
    # --------------------------------------------------------
    #
    # Get the complete tree for this section of pages. We need this for blocks etc.
    #
    $tree = xarMod::apiFunc('publications', 'user', 'getpagestree', array('tree_contains_pid' => $id, 'key' => 'id', 'status' => 'ACTIVE,FRONTPAGE,PLACEHOLDER'));
    // If this page is of type PLACEHOLDER, then look in its descendents
    if ($data['object']->properties['state']->value == 5) {
        // Scan for a descendent that is ACTIVE or FRONTPAGE
        if (!empty($tree['pages'][$id]['child_keys'])) {
            foreach ($tree['pages'][$id]['child_keys'] as $scan_key) {
                // If the page is displayable, then treat it as the new page.
                if ($tree['pages'][$scan_key]['status'] == 3 || $tree['pages'][$scan_key]['status'] == 4) {
                    $id = $tree['pages'][$scan_key]['id'];
                    $id = xarMod::apiFunc('publications', 'user', 'gettranslationid', array('id' => $id));
                    $itemid = $data['object']->getItem(array('itemid' => $id));
                    break;
                }
            }
        }
    }
    # --------------------------------------------------------
    #
    # Additional data
    #
    // Pass the layout to the template
    $data['layout'] = $layout;
    // Get the settings for this publication type;
    $data['settings'] = xarModAPIFunc('publications', 'user', 'getsettings', array('ptid' => $ptid));
    // The name of this object
    $data['objectname'] = $data['object']->name;
    # --------------------------------------------------------
    #
    # Set the theme if needed
    #
    if (!empty($data['object']->properties['theme']->value)) {
        xarTplSetThemeName($data['object']->properties['theme']->value);
    }
    # --------------------------------------------------------
    #
    # Set the page template from the pubtype if needed
    #
    if (!empty($data['settings']['page_template'])) {
        $pagename = $data['settings']['page_template'];
        $position = strpos($pagename, '.');
        if ($position === false) {
            $pagetemplate = $pagename;
        } else {
            $pagetemplate = substr($pagename, 0, $position);
        }
        xarTpl::setPageTemplateName($pagetemplate);
    }
    // It can be overridden by the page itself
    if (!empty($data['object']->properties['page_template']->value)) {
        $pagename = $data['object']->properties['page_template']->value;
        $position = strpos($pagename, '.');
        if ($position === false) {
            $pagetemplate = $pagename;
        } else {
            $pagetemplate = substr($pagename, 0, $position);
        }
        xarTpl::setPageTemplateName($pagetemplate);
    }
    # --------------------------------------------------------
    #
    # Cache data for blocks
    #
    // Now we can cache all this data away for the blocks.
    // The blocks should have access to most of the same data as the page.
    xarVarSetCached('Blocks.publications', 'pagedata', $tree);
    // The 'serialize' hack ensures we have a proper copy of the
    // paga data, which is a self-referencing array. If we don't
    // do this, then any changes we make will affect the stored version.
    $data = unserialize(serialize($data));
    // Save some values. These are used by blocks in 'automatic' mode.
    xarVarSetCached('Blocks.publications', 'current_id', $id);
    xarVarSetCached('Blocks.publications', 'ptid', $ptid);
    xarVarSetCached('Blocks.publications', 'author', $data['object']->properties['author']->value);
    # --------------------------------------------------------
    #
    # Make the properties available to the template
    #
    $data['properties'] =& $data['object']->properties;
    return $data;
    /*
        // TEST - highlight search terms
        if(!xarVarFetch('q',     'str',  $q,     NULL, XARVAR_NOT_REQUIRED)) {return;}
    */
    // Override if needed from argument array (e.g. preview)
    extract($args);
    // Defaults
    if (!isset($page)) {
        $page = 1;
    }
    // via arguments only
    if (!isset($preview)) {
        $preview = 0;
    }
    /*
        if ($preview) {
            if (!isset($publication)) {
                return xarML('Invalid publication');
            }
            $id = $publication->properties['id']->value;
        } elseif (!isset($id) || !is_numeric($id) || $id < 1) {
            return xarML('Invalid publication ID');
        }
    */
    /*    // Get publication
        if (!$preview) {
            $publication = xarModAPIFunc('publications',
                                    'user',
                                    'get',
                                    array('id' => $id,
                                          'withcids' => true));
        }
    
        if (!is_array($publication)) {
            $msg = xarML('Failed to retrieve publication in #(3)_#(1)_#(2).php', 'userapi', 'get', 'publications');
            throw new DataNotFoundException(null, $msg);
        }
        // Get publication types
        $pubtypes = xarModAPIFunc('publications','user','get_pubtypes');
    
        // Check that the publication type is valid, otherwise use the publication's pubtype
        if (!empty($ptid) && !isset($pubtypes[$ptid])) {
            $ptid = $publication['pubtype_id'];
        }
    
    */
    // keep original ptid (if any)
    //    $ptid = $publication['pubtype_id'];
    //    $pubtype_id = $publication->properties['itemtype']->value;
    //    $owner = $publication->properties['author']->value;
    /*    if (!isset($publication['cids'])) {
            $publication['cids'] = array();
        }
        $cids = $publication['cids'];
    */
    // Get the publication settings for this publication type
    if (empty($ptid)) {
        $settings = unserialize(xarModVars::get('publications', 'settings'));
    } else {
        $settings = unserialize(xarModVars::get('publications', 'settings.' . $ptid));
    }
    // show the number of publications for each publication type
    if (!isset($show_pubcount)) {
        if (!isset($settings['show_pubcount']) || !empty($settings['show_pubcount'])) {
            $show_pubcount = 1;
            // default yes
        } else {
            $show_pubcount = 0;
        }
    }
    // show the number of publications for each category
    if (!isset($show_catcount)) {
        if (empty($settings['show_catcount'])) {
            $show_catcount = 0;
            // default no
        } else {
            $show_catcount = 1;
        }
    }
    // Initialize the data array
    $data = $publication->getFieldValues();
    $data['ptid'] = $ptid;
    // navigation pubtype
    $data['pubtype_id'] = $pubtype_id;
    // publication pubtype
    // TODO: improve the case where we have several icons :)
    $data['topic_icons'] = '';
    $data['topic_images'] = array();
    $data['topic_urls'] = array();
    $data['topic_names'] = array();
    /*
        if (count($cids) > 0) {
            if (!xarModAPILoad('categories', 'user')) return;
            $catlist = xarModAPIFunc('categories',
                                    'user',
                                    'getcatinfo',
                                    array('cids' => $cids));
            foreach ($catlist as $cat) {
                $link = xarModURL('publications','user','view',
                                 array(//'state' => array(PUBLICATIONS_STATE_FRONTPAGE,PUBLICATIONS_STATE_APPROVED).
                                       'ptid' => $ptid,
                                       'catid' => $cat['cid']));
                $name = xarVarPrepForDisplay($cat['name']);
    
                $data['topic_urls'][] = $link;
                $data['topic_names'][] = $name;
    
                if (!empty($cat['image'])) {
                    $image = xarTplGetImage($cat['image'],'categories');
                    $data['topic_icons'] .= '<a href="'. $link .'">'.
                                            '<img src="'. $image .
                                            '" alt="'. $name .'" />'.
                                            '</a>';
                    $data['topic_images'][] = $image;
    
                    break;
                }
            }
        }
    */
    // multi-page output for 'body' field (mostly for sections at the moment)
    $themeName = xarVarGetCached('Themes.name', 'CurrentTheme');
    if ($themeName != 'print') {
        if (strstr($publication->properties['body']->value, '<!--pagebreak-->')) {
            if ($preview) {
                $publication['body'] = preg_replace('/<!--pagebreak-->/', '<hr/><div style="text-align: center;">' . xarML('Page Break') . '</div><hr/>', $publication->properties['body']->value);
                $data['previous'] = '';
                $data['next'] = '';
            } else {
                $pages = explode('<!--pagebreak-->', $publication->properties['body']->value);
                // For documents with many pages, the pages can be
                // arranged in blocks.
                $pageBlockSize = 10;
                // Get pager information: one item per page.
                $pagerinfo = xarTplPagerInfo(empty($page) ? 1 : $page, count($pages), 1, $pageBlockSize);
                // Retrieve current page and total pages from the pager info.
                // These will have been normalised to ensure they are in range.
                $page = $pagerinfo['currentpage'];
                $numpages = $pagerinfo['totalpages'];
                // Discard everything but the current page.
                $publication['body'] = $pages[$page - 1];
                unset($pages);
                if ($page > 1) {
                    // Don't count page hits after the first page.
                    xarVarSetCached('Hooks.hitcount', 'nocount', 1);
                }
                // Pass in the pager info so a complete custom pager
                // can be created in the template if required.
                $data['pagerinfo'] = $pagerinfo;
                // Get the rendered pager.
                // The pager template (last parameter) could be an
                // option for the publication type.
                $urlmask = xarModURL('publications', 'user', 'display', array('ptid' => $ptid, 'id' => $id, 'page' => '%%'));
                $data['pager'] = xarTplGetPager($page, $numpages, $urlmask, 1, $pageBlockSize, 'multipage');
                // Next two assignments for legacy templates.
                // TODO: deprecate them?
                $data['next'] = xarTplGetPager($page, $numpages, $urlmask, 1, $pageBlockSize, 'multipagenext');
                $data['previous'] = xarTplGetPager($page, $numpages, $urlmask, 1, $pageBlockSize, 'multipageprev');
            }
        } else {
            $data['previous'] = '';
            $data['next'] = '';
        }
    } else {
        $publication['body'] = preg_replace('/<!--pagebreak-->/', '', $publication['body']);
    }
    // TEST
    if (isset($prevnextart)) {
        $settings['prevnextart'] = $prevnextart;
    }
    if (!empty($settings['prevnextart']) && $preview == 0) {
        if (!array_key_exists('defaultsort', $settings)) {
            $settings['defaultsort'] = 'id';
        }
        $prevart = xarModAPIFunc('publications', 'user', 'getprevious', array('id' => $id, 'ptid' => $ptid, 'sort' => $settings['defaultsort'], 'state' => array(PUBLICATIONS_STATE_FRONTPAGE, PUBLICATIONS_STATE_APPROVED), 'enddate' => time()));
        if (!empty($prevart['id'])) {
            //Make all previous publication info available to template
            $data['prevartinfo'] = $prevart;
            $data['prevart'] = xarModURL('publications', 'user', 'display', array('ptid' => $prevart['pubtype_id'], 'id' => $prevart['id']));
        } else {
            $data['prevart'] = '';
        }
        $nextart = xarModAPIFunc('publications', 'user', 'getnext', array('id' => $id, 'ptid' => $ptid, 'sort' => $settings['defaultsort'], 'state' => array(PUBLICATIONS_STATE_FRONTPAGE, PUBLICATIONS_STATE_APPROVED), 'enddate' => time()));
        if (!empty($nextart['id'])) {
            //Make all next art info available to template
            $data['nextartinfo'] = $nextart;
            $data['nextart'] = xarModURL('publications', 'user', 'display', array('ptid' => $nextart['pubtype_id'], 'id' => $nextart['id']));
        } else {
            $data['nextart'] = '';
        }
    } else {
        $data['prevart'] = '';
        $data['nextart'] = '';
    }
    // Display publication
    unset($publication);
    // temp. fix to include dynamic data fields without changing templates
    if (xarModIsHooked('dynamicdata', 'publications', $pubtype_id)) {
        list($properties) = xarModAPIFunc('dynamicdata', 'user', 'getitemfordisplay', array('module' => 'publications', 'itemtype' => $pubtype_id, 'itemid' => $id, 'preview' => $preview));
        if (!empty($properties) && count($properties) > 0) {
            foreach (array_keys($properties) as $field) {
                $data[$field] = $properties[$field]->getValue();
                // POOR mans flagging for transform hooks
                try {
                    $configuration = $properties[$field]->configuration;
                    if (substr($configuration, 0, 10) == 'transform:') {
                        $data['transform'][] = $field;
                    }
                } catch (Exception $e) {
                }
                // TODO: clean up this temporary fix
                $data[$field . '_output'] = $properties[$field]->showOutput();
            }
        }
    }
    // Let any transformation hooks know that we want to transform some text.
    // You'll need to specify the item id, and an array containing all the
    // pieces of text that you want to transform (e.g. for autolinks, wiki,
    // smilies, bbcode, ...).
    $data['itemtype'] = $pubtype_id;
    // TODO: what about transforming DDfields ?
    // <mrb> see above for a hack, needs to be a lot better.
    // Summary is always included, is that handled somewhere else? (publication config says i can ex/include it)
    // <mikespub> publications config allows you to call transforms for the publications summaries in the view function
    if (!isset($title_transform)) {
        if (empty($settings['title_transform'])) {
            $data['transform'][] = 'summary';
            $data['transform'][] = 'body';
            $data['transform'][] = 'notes';
        } else {
            $data['transform'][] = 'title';
            $data['transform'][] = 'summary';
            $data['transform'][] = 'body';
            $data['transform'][] = 'notes';
        }
    }
    $data = xarModCallHooks('item', 'transform', $id, $data, 'publications');
    return xarTplModule('publications', 'user', 'display', $data);
    if (!empty($data['title'])) {
        // CHECKME: <rabbit> Strip tags out of the title - the <title> tag shouldn't have any other tags in it.
        $title = strip_tags($data['title']);
        xarTplSetPageTitle(xarVarPrepForDisplay($title), xarVarPrepForDisplay($pubtypes[$data['itemtype']]['description']));
        // Save some variables to (temporary) cache for use in blocks etc.
        xarVarSetCached('Comments.title', 'title', $data['title']);
    }
    /*
        if (!empty($q)) {
        // TODO: split $q into search terms + add style (cfr. handlesearch in search module)
            foreach ($data['transform'] as $field) {
                $data[$field] = preg_replace("/$q/","<span class=\"xar-search-match\">$q</span>",$data[$field]);
            }
        }
    */
    // Navigation links
    $data['publabel'] = xarML('Publication');
    $data['publinks'] = array();
    //xarModAPIFunc('publications','user','getpublinks',
    //    array('state' => array(PUBLICATIONS_STATE_FRONTPAGE,PUBLICATIONS_STATE_APPROVED),
    //          'count' => $show_pubcount));
    if (isset($show_map)) {
        $settings['show_map'] = $show_map;
    }
    if (!empty($settings['show_map'])) {
        $data['maplabel'] = xarML('View Publication Map');
        $data['maplink'] = xarModURL('publications', 'user', 'viewmap', array('ptid' => $ptid));
    }
    if (isset($show_archives)) {
        $settings['show_archives'] = $show_archives;
    }
    if (!empty($settings['show_archives'])) {
        $data['archivelabel'] = xarML('View Archives');
        $data['archivelink'] = xarModURL('publications', 'user', 'archive', array('ptid' => $ptid));
    }
    if (isset($show_publinks)) {
        $settings['show_publinks'] = $show_publinks;
    }
    if (!empty($settings['show_publinks'])) {
        $data['show_publinks'] = 1;
    } else {
        $data['show_publinks'] = 0;
    }
    $data['show_catcount'] = $show_catcount;
    // Tell the hitcount hook not to display the hitcount, but to save it
    // in the variable cache.
    if (xarModIsHooked('hitcount', 'publications', $pubtype_id)) {
        xarVarSetCached('Hooks.hitcount', 'save', 1);
        $data['dohitcount'] = 1;
    } else {
        $data['dohitcount'] = 0;
    }
    // Tell the ratings hook to save the rating in the variable cache.
    if (xarModIsHooked('ratings', 'publications', $pubtype_id)) {
        xarVarSetCached('Hooks.ratings', 'save', 1);
        $data['doratings'] = 1;
    } else {
        $data['doratings'] = 0;
    }
    // Retrieve the current hitcount from the variable cache
    if ($data['dohitcount'] && xarVarIsCached('Hooks.hitcount', 'value')) {
        $data['counter'] = xarVarGetCached('Hooks.hitcount', 'value');
    } else {
        $data['counter'] = '';
    }
    // Retrieve the current rating from the variable cache
    if ($data['doratings'] && xarVarIsCached('Hooks.ratings', 'value')) {
        $data['rating'] = intval(xarVarGetCached('Hooks.ratings', 'value'));
    } else {
        $data['rating'] = '';
    }
    // Save some variables to (temporary) cache for use in blocks etc.
    xarVarSetCached('Blocks.publications', 'title', $data['title']);
    // Generating keywords from the API now instead of setting the entire
    // body into the cache.
    $keywords = xarModAPIFunc('publications', 'user', 'generatekeywords', array('incomingkey' => $data['body']));
    xarVarSetCached('Blocks.publications', 'body', $keywords);
    xarVarSetCached('Blocks.publications', 'summary', $data['summary']);
    xarVarSetCached('Blocks.publications', 'id', $id);
    xarVarSetCached('Blocks.publications', 'ptid', $ptid);
    xarVarSetCached('Blocks.publications', 'cids', $cids);
    xarVarSetCached('Blocks.publications', 'owner', $owner);
    if (isset($data['author'])) {
        xarVarSetCached('Blocks.publications', 'author', $data['author']);
    }
    // TODO: add this to publications configuration ?
    //if ($shownavigation) {
    $data['id'] = $id;
    $data['cids'] = $cids;
    xarVarSetCached('Blocks.categories', 'module', 'publications');
    xarVarSetCached('Blocks.categories', 'itemtype', $ptid);
    xarVarSetCached('Blocks.categories', 'itemid', $id);
    xarVarSetCached('Blocks.categories', 'cids', $cids);
    if (!empty($ptid) && !empty($pubtypes[$ptid]['description'])) {
        xarVarSetCached('Blocks.categories', 'title', $pubtypes[$ptid]['description']);
    }
    // optional category count
    if ($show_catcount && !empty($ptid)) {
        $pubcatcount = xarModAPIFunc('publications', 'user', 'getpubcatcount', array('state' => array(PUBLICATIONS_STATE_FRONTPAGE, PUBLICATIONS_STATE_APPROVED), 'ptid' => $ptid));
        if (!empty($pubcatcount[$ptid])) {
            xarVarSetCached('Blocks.categories', 'catcount', $pubcatcount[$ptid]);
        }
    } else {
        //    xarVarSetCached('Blocks.categories','catcount',array());
    }
    //}
    // Module template depending on publication type
    $template = $pubtypes[$pubtype_id]['name'];
    // Page template depending on publication type (optional)
    // Note : this cannot be overridden in templates
    if (empty($preview) && !empty($settings['page_template'])) {
        xarTplSetPageTemplateName($settings['page_template']);
    }
    // Specific layout within a template (optional)
    if (isset($layout)) {
        $data['layout'] = $layout;
    }
    $pubtypeobject = DataObjectMaster::getObject(array('name' => 'publications_types'));
    $pubtypeobject->getItem(array('itemid' => $ptid));
    $data['object'] = DataObjectMaster::getObject(array('name' => $pubtypeobject->properties['name']->value));
    $id = xarMod::apiFunc('publications', 'user', 'getranslationid', array('id' => $id));
    $data['object']->getItem(array('itemid' => $id));
    return xarTplModule('publications', 'user', 'display', $data, $template);
}