Questions regarding advanced modules and the dashboard - list_items, permssions...

edited April 2017 in Modules
Hey guys, I've created an advanced module because I need to display some information on the dashboard and a simple module obviously wasn't going to cut it. So far so good, but I've run into some issues and I can't wrap my head around them. Here goes:

1) The advanced module works with two models - breakdowns_model and regions_model. They have a 1:M relationship (one region, multiple breakdowns) and I've joined them in the breakdowns_model's list_items method, like so:

function list_items($limit = NULL, $offset = NULL, $col = 'id', $order = 'asc', $just_count = FALSE) { $this->db->join('regions', 'regions.id = breakdowns.region', 'left'); $this->db->select('breakdowns.id, regions.name as region, SUBSTRING(content, 1, 50) AS content, start_date, status', FALSE); $data = parent::list_items($limit, $offset, $col, $order, $just_count); if(empty($just_count)) { foreach ($data as $key => $val) { $data[$key]['content'] = htmlentities($val['content'], ENT_QUOTES, 'UTF-8'); } } return $data; }
(it's basically like the one in the simple module tutorial, haven't done anything fancy with it yet)

This works perfectly and it gives me the list I want - the region's name is displayed in the list instead of its id in the breakdowns table.

Now I need to display a couple of these breakdowns in the dashboard and this is where my problem arises. In the Dashboard controller I retrieve the data with find_all:

.... $where = "`start_date` <= '$check_date' AND status = 'new'"; $vars['due_breakdowns'] = $this->breakdowns_model->find_all($where, 'start_date asc'); $this->load->view('_admin/dashboard', $vars);

However, since I haven't joined the two DB tables I can't get the region's name, only the id. That's why I used _common_query in the breakdowns_model:

function _common_query(){ parent::_common_query(); $this->db->join('regions', 'regions.id = breakdowns.region', 'left'); $this->db->select('breakdowns.id, regions.name as region, start_date, status', FALSE); }

It works and I get the region's name in the dashboard, but when I try to edit an existing breakdown item, it no longer brings up the breakdown's data, the fields stay empty, as if there was nothing in the database for that record. What am I missing here? I feel like I shouldn't have to join the tables twice and this is probably what's causing the issue, but how do I resolve it?

PS. I know it's a confusing question, but I'm not a native speaker, so apologies - I'll try to clarify if needed.

2) The next question is regarding permissions for the dashboard specifically. The breakdowns items I'm displaying currently appear only for the admin user, can I make them visible to other users as well? I noticed this is the same for the Blog module - Recent Blog Posts appear for the admin, but not for users, even after I've given them permissions.

Is this intended or have I overlooked something?

Thanks to whoever takes the time to read this!

Comments

  • edited 4:54AM
    1) The list_items does require it's own join and select logic separate from the find_all/_common_query because it can sometimes be quite different then what you may normally want with a find_all. To help get around this issue (since I run into it a lot too), there is a _common_joins method in the latest version of FUEL 1.4 that will do the joins in both the list_items method and in the find_all so you only need to declare the following in one spot.
    $this->db->join('regions', 'regions.id = breakdowns.region', 'left');
    To get around your issue with the data not showing up, changing the common query select to one of the following:
    $this->db->select('breakdowns.*, regions.name as region, start_date, status', FALSE); // OR $this->db->select('regions.name as region, start_date, status', FALSE);

    2) For the permissions for the dashboard and the blog, there should be a permission of just "blog" setup that the user is associated with. It looks like the current install SQL doesn't have that permission.
  • edited April 2017
    Thank you for the prompt reply!

    I'm still running FUEL 1.3.2 and have been meaning to upgrade for a while, but it's just gone up in my priority list. I'm going to be using more queries like these, so the _common_joins method will certainly come in useful. In the mean time however, the fix you suggested works like a charm.

    I also got the permissions working. As you said, the SQL file didn''t have it, which I hadn't noticed. Added it in manually and did the same for my own breakdowns module, now everything is working.

    Thanks again, I swear this forum (and FUEL itself!) is a life-saver :)
Sign In or Register to comment.