1

Currently I am trying to use group access level code under public static function. File is belong to 3rd part component and stays under the administrator folder. 3rd part component has own custom filed management and this file is containing classes about them.

I have two different access level for frontend users. For the commercial users I have tried to add below code but it doesn't work.

$user = JFactory::getUser();
if ($user->get('Commercial') == 1)  {

Here is full code page.

<?php

// Access the file from Joomla environment
defined('_JEXEC') or die('Restricted access');

class JHtmlEditRAuction
{
    /**
     * DisplayFieldsHtml
     *
     * @param        $row
     * @param        $fieldlist
     * @param string $style
     *
     * @return null
     */
    public static function DisplayFieldsHtml(&$row, $fieldlist, $style = 'div')
    {
        if (!count($fieldlist)) return NULL;
        $page = $fieldlist[0]->page;
        $cfg  = CustomFieldsFactory::getConfig();

        $category_filter = array();
        if ($cfg['has_category'][$page]) {
            $db = JFactory::getDBO();
            $db->setQuery("SELECT `fid` FROM `#__" . APP_PREFIX . "_fields_categories` WHERE `cid` = '" . $row->cat . "'");
            $category_filter = $db->loadColumn();
        }
        $flist        = array();
        $field_object = JTable::getInstance('FieldsTable', 'JTheFactory');

        foreach ($fieldlist as $field) {

            if ($field->categoryfilter && !in_array($field->id, $category_filter))
                continue;

            $field_type = CustomFieldsFactory::getFieldType($field->ftype);

            $field_object->bind($field);
            $f        = new stdClass();
            $f->field = clone $field;
            $f->value = $row->{$field->db_name};
            $f->html  = $field_type->getFieldHTML($field_object, $row->{$field->db_name});
            $flist[]  = $f;
        }
        $func = 'DisplayFieldsHtml_' . ucfirst($style);
        $html = self::$func($flist);

        return $html;
    }

    /**
     * DisplayFieldsHtml_Div
     *
     * @param $flist
     *
     * @return null|string
     */
    public static function DisplayFieldsHtml_Div($flist)
    {
        if (!count($flist)) return NULL;
        $html = '<div>';
        foreach ($flist as $f) {
            $tooltip = '';
            if ($f->field->help) $tooltip = JHtml::_('tooltip', $f->field->help, NULL, JUri::root() . 'components/com_rbids/images/tooltip.png');

            $html .= '<div class="auction_edit_field_container ';

            // This is required as helper for 'required' handler in js validator
            if ($f->field->compulsory) {
                $html .= 'required">';
            } else {
                $html .= '">';
            }

// Here is acces level code that I tried to use
$user = JFactory::getUser();
if ($user->get('Commercial') == 1)  
{
if($f->field->css_class == 'commercialclass') 
{       
            $html .= '<div class=" span3 auction_edit_field_label rbids_custom_field">' . JText::_($f->field->name) . '</div>
                <div class=" span9 auction_edit_field_input">' . $f->html . $tooltip;
            if ($f->field->compulsory) {
                $html .= '&nbsp;' . JHtml::image(JUri::root() . 'components/com_rbids/images/requiredfield.gif', 'Required');
            }
            $html .= '</div>
                <div style="clear: both;"></div>
            </div>';

}
}
        }
        $html .= '</div>';
        return $html;
    }
}

I don't get any error on the page but also nothing comes to the page about related part.

To be honest I am not a PHP expert. I have learned everything on stackexchange and stackoverflow and keep trying to find answers here.

Hope somebody can help me about it.

Thanks in advance.

Regards

1 Answer 1

1

There's Joomla\CMS\User\User::getAuthorisedViewLevels() method which returns an array of user's access level IDs. Instead of hardcoding the access level name or ID, make it a configuration option in your component by adding a field to config.xml:

<field
    name="commercialAccessLevel"
    type="accesslevel"
    label="COM_EXAMPLE_CONFIG_COMMERCIAL_ACCESS_LEVEL_LABEL"
    filter="integer"
/>

Retrieve it in your code and do a in_array() check against the mentioned method:

$commercialAccessLevel = JComponentHelper::getParams('com_example')->get('commercialAccessLevel');
$user = JFactory::getUser();

if (in_array($commercialAccessLevel, $user->getAuthorisedViewLevels()))
{
    // User has commercial access level.
}
1
  • Thank you so much. Because of you I have created my new milestone about joomla. I had to modify your code like this. Because fileds has their own table. $user = JFactory::getUser(); if (in_array($f->field->commercialAccessLevel, $user->getAuthorisedViewLevels())) Regards
    – okisan
    Mar 8, 2019 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.