1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Checkbox options based on user ACL

Discussion in 'Community' started by SoilentRed, Oct 21, 2019.

  1. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    Esteemed Fabrikators,

    I once again seek to drink from the Fabrik fountain of knowledge.

    I want create a checkbox option based on a user's access level. In my #_acymailing_list table there is a column called access_manage. This column contains data that looks like this:

    none
    ,8,27,
    ,28,
    none
    ,46,

    I see that using the {$my->groups} placeholder returns all the joomla group ids a member belongs to.

    2,8,14,15,16,17,21,22,23,27,32

    Can I create a checkbox element that will only show options that contain a match? So, for example, UserA is in groups 5,8,28,55. #_acymailing_list columns have
    Code (Text):

    access_manage       name
    ,8,27,              group1
    ,28,                group2
    none                group3
    ,46,                group4
    ,8,55,              group5
     
    Therefore, UserA's checkbox options would be:

    group1, group2, group5

    Is it possible?!?
     
  2. troester

    troester Well-Known Member Staff Member

    Level: Community
    You can use php code in "Eval populate" in the checkbox element's "Advanced" options.
     
    SoilentRed likes this.
  3. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    Thank you. I'll try that as well. I am currently down the database join path trying to see what I can come up with.
     
  4. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    So, here's what I've been tinkering with on the database join element. I've also checked the advanced tab of the checkbox element. At the risk of revealing myself to be a complete computer illiterate, I have to say, I'd have a very similar approach to checkbox element.

    In my peanut brain, this should come down to a logical operator. I could be wrong. I'm probably wrong. As stated, I'm trying compare comma-separated list, and find commonalities. Am I way off base? I thought the LIKE operator should help. I'm getting this error:

    Code (Text):

    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '8,14,15,16,17,21,22,23,24,25,26,27,28,29,35,36,37,38,39,40,46,47,48,49,50,51,52,' at line 6
     
    Obviously my account is in a lot of groups, so I'd expect a lot of options would appear available (if I did it right...)

    Anywho, here are my current settings.

    Code (Text):

    Render As           Checkbox
    Connection          Site Database
    Table               blah_acymailing_list
    Value               listid
    Lable               name
     
    Joins where and/or order by statement (SQL)

    Code (Text):

    WHERE {thistable}.`listid`
        IN ( SELECT blah_acymailing_subscriber.listid
        FROM blah_acymailing_list
            WHERE blah_acymailing_list.access_manage LIKE {$my->groups} )
     
    Your patience and guidance is revered and valued, and I am not worthy.
     
  5. mahmoodee

    mahmoodee Member

    Level: Community
    Please try to put the placeholder between quotations
    LIKE '{$my->groups}'
     
    SoilentRed likes this.
  6. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    Thank you. That should have been obvious to me.

    So the error is gone. I also realized that my select was pulling from the wrong table. My new statement looks like this:

    Code (Text):

    WHERE {thistable}.`listid`
        IN ( SELECT blah_acymailing_list.listid
        FROM blah_acymailing_list
            WHERE blah_acymailing_list.access_manage LIKE '{$my->groups}' )
     
    Now the problem is that it is not returning any matches, therefore, no options to check off. Am I going about this the wrong way?
     
  7. troester

    troester Well-Known Member Staff Member

    Level: Community
    You are comparing strings, e.g. (from your example in post1)
    WHERE ',8,55' LIKE '5,8,28,55'
    which obviously doesn't match.

    You must split the $my->groups values and compare the single ones.
     
    SoilentRed likes this.
  8. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    I'm sorry, that's over my head. Can you point me in the right direction? How do you split the values? And then I assume you run the values through loop to compare?
     
  9. mahmoodee

    mahmoodee Member

    Level: Community
    I think it is better to use checkbox element Instead of database join because you can use php code to create options in checkbox element , so you can split the values and run the values through loop to compare
     
    SoilentRed likes this.
  10. SoilentRed

    SoilentRed Caaan do!

    Level: Community
    Welp, you, @mahmoodee and @troester were right. I Shoulda did that from the beginning. Guess I had to fail my way first. lol. Anyways, here's what I came up with:

    Code (Text):

    $db = JFactory:: getDbo();
    $db->setQuery('SELECT listid, name, access_manage FROM #__acymailing_list');
    $rows = $db->loadObjectList();
    foreach ($rows as $row) {
        $accessManage = $row->access_manage;
        $accessManageParsed = explode(",", $accessManage);
        $myGroups = explode(",", "{$my->groups}");
        $matches = array_intersect($accessManageParsed, $myGroups);
            foreach ($matches as $m) {
        $options[] = jHTML::_('select.option', $row->listid, $row->name); }
    }
    return $options;
     
    This seems to work.

    Now my goal is to load phone numbers based on that selection. Wondering if I should ajaxify that. Will let you know.

    Toodles!
     

Share This Page