示例#1
0
 /**
  * Method to load the Category
  *
  * @access public
  * @return array
  */
 function getCategory($pk = null, $raiseErrors = true, $checkAccess = true)
 {
     //initialize some vars
     $app = JFactory::getApplication();
     $user = JFactory::getUser();
     $cparams = $this->_params;
     if ($pk) {
         $this->_id = $pk;
     }
     // Set a specific id
     $cat_usable = !$this->_layout || $this->_layout != 'mcats';
     if ($this->_id && $cat_usable) {
         // ************************************************************************************************************
         // Retrieve category data, but ONLY if current layout can use it, ('mcats' does not since it uses multiple ids)
         // ************************************************************************************************************
         $query = 'SELECT c.*,' . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END as slug' . ' FROM #__categories AS c' . ' WHERE c.id = ' . $this->_id . ' AND c.published = 1 ' . (FLEXI_J16GE ? ' AND c.extension=' . $this->_db->Quote(FLEXI_CAT_EXTENSION) : '');
         $this->_db->setQuery($query);
         $_category = $this->_db->loadObject();
         if ($this->_db->getErrorNum()) {
             JFactory::getApplication()->enqueueMessage(__FUNCTION__ . '(): SQL QUERY ERROR:<br/>' . nl2br($this->_db->getErrorMsg()), 'error');
         }
     } else {
         $_category = false;
     }
     // *******************************************************************************
     // Check category was found / is published, and throw an error. Note that an empty
     // layout means single category view, so raise an error if category id is missing
     // *******************************************************************************
     if (($this->_id || $this->_layout == '') && $cat_usable && !$_category) {
         $err_mssg = $err_type = false;
         if (!$_category) {
             $err_mssg = JText::sprintf('FLEXI_CONTENT_CATEGORY_NOT_FOUND_OR_NOT_PUBLISHED', $this->_id);
             $err_type = 404;
         }
         // Throw error -OR- return if errors suppresed
         if ($err_mssg) {
             if (!$raiseErrors) {
                 return false;
             }
             if (FLEXI_J16GE) {
                 throw new Exception($err_mssg, $err_type);
             } else {
                 JError::raiseError($err_type, $err_mssg);
             }
         }
     }
     // *********************************************************************
     // Some layouts optionally limit to a specific category, for these
     // create an empty category data object (if one was not created already)
     // *********************************************************************
     if ($this->_layout) {
         if ($this->_layout != 'mcats' && !empty($_category)) {
             $this->_category = $_category;
         } else {
             $this->_category = new stdClass();
             $this->_category->published = 1;
             $this->_category->id = $this->_id;
             // can be zero for layouts: author/myitems/favs/tags, etc
             $this->_category->title = '';
             $this->_category->description = '';
             $this->_category->slug = '';
             $this->_category->ids = $this->_ids;
             // mcats layout but it can be empty, to allow all categories
         }
     } else {
         $this->_category = $_category;
     }
     // *****************************************************
     // Check for proper layout configuration and throw error
     // *****************************************************
     if ($this->_layout) {
         $err_mssg = $err_type = false;
         if (!in_array($this->_layout, array('favs', 'tags', 'mcats', 'myitems', 'author'))) {
             $err_mssg = JText::sprintf('FLEXI_CONTENT_LIST_LAYOUT_IS_NOT_SUPPORTED', $this->_layout);
             $err_type = 404;
         } else {
             if ($this->_layout == 'author' && !$this->_authorid) {
                 $err_mssg = JText::_('FLEXI_CANNOT_LIST_CONTENT_AUTHORID_NOT_SET');
                 $err_type = 404;
             } else {
                 if ($this->_layout == 'tags' && !$this->_tagid) {
                     $err_mssg = JText::_('FLEXI_CANNOT_LIST_CONTENT_TAGID_NOT_SET');
                     $err_type = 404;
                 } else {
                     if ($this->_layout == 'myitems' && !$this->_authorid) {
                         $err_mssg = JText::_('FLEXI_LOGIN_TO_DISPLAY_YOUR_CONTENT');
                         $err_type = 403;
                         $login_redirect = true;
                     } else {
                         if ($this->_layout == 'favs' && !$this->_authorid) {
                             $err_mssg = JText::_('FLEXI_LOGIN_TO_DISPLAY_YOUR_CONTENT');
                             $err_type = 403;
                             $login_redirect = true;
                         }
                     }
                 }
             }
         }
         // Raise a notice and redirect
         if ($err_mssg) {
             if (!$raiseErrors) {
                 return false;
             }
             if (!empty($login_redirect)) {
                 // redirect unlogged user to login
                 $uri = JFactory::getURI();
                 $return = $uri->toString();
                 $com_users = FLEXI_J16GE ? 'com_users' : 'com_user';
                 $url = $cparams->get('login_page', 'index.php?option=' . $com_users . '&view=login');
                 $return = strtr(base64_encode($return), '+/=', '-_,');
                 $url .= '&return=' . $return;
                 // '&return='.base64_encode($return);
                 $url .= '&isfcurl=1';
                 JError::raiseWarning($err_type, $err_mssg);
                 $app->redirect($url);
             } else {
                 if (FLEXI_J16GE) {
                     throw new Exception($err_mssg, $err_type);
                 } else {
                     JError::raiseError($err_type, $err_mssg);
                 }
             }
         }
     }
     // *******************************************************
     // Set category parameters, these have already been loaded
     // *******************************************************
     $this->_category->parameters = $this->_params;
     // ******************************************************************
     // Check whether category access level allows access and throw errors
     // but skip checking Access if so requested via function parameter
     // ******************************************************************
     if (!$checkAccess) {
         return $this->_category;
     }
     $canread = true;
     if ($this->_id) {
         if (FLEXI_J16GE) {
             $aid_arr = JAccess::getAuthorisedViewLevels($user->id);
             $canread = in_array($this->_category->access, $aid_arr);
         } else {
             $aid = (int) $user->get('aid');
             $canread = FLEXI_ACCESS ? FAccess::checkAllItemReadAccess('com_content', 'read', 'users', $user->gmid, 'category', $this->_category->id) : $this->_category->access <= $aid;
         }
     }
     if (!$canread && $this->_id != 0) {
         if ($user->guest) {
             // Redirect to login
             $uri = JFactory::getURI();
             $return = $uri->toString();
             $com_users = FLEXI_J16GE ? 'com_users' : 'com_user';
             $url = $cparams->get('login_page', 'index.php?option=' . $com_users . '&view=login');
             $return = strtr(base64_encode($return), '+/=', '-_,');
             $url .= '&return=' . $return;
             // '&return='.base64_encode($return);
             $url .= '&isfcurl=1';
             JError::raiseWarning(403, JText::sprintf("FLEXI_LOGIN_TO_ACCESS", $url));
             $app->redirect($url);
         } else {
             if ($cparams->get('unauthorized_page', '')) {
                 $app->redirect($cparams->get('unauthorized_page'));
             } else {
                 JError::raiseWarning(403, JText::_("FLEXI_ALERTNOTAUTH_VIEW"));
                 $app->redirect('index.php');
             }
         }
     }
     return $this->_category;
 }