[SOLVED] Building a sortable list of links via ECB

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

[SOLVED] Building a sortable list of links via ECB

Post by Guido »

I've been mudding around for an hour or four now, without getting a solution so I hope someone here can help me. I want to use the sortablelist option that comes with the Extended Content Blocks module to create a sortable list of links a user can make using the LinkMgr module.

Unfortunately, the 'values' option doesn't work. At first I created a template in the LinkMgr that output a simple comma separated list. I passed this list on to a smarty value using 'assign' and used this variable in the ECB content call. This didn't work. I contacted the ECB developer, who confirmed that I needed to write a UDT to create a smarty array in the form of 'label' => 'value', 'label2' => 'value2'.

Since I'm not much of a programmer, I went about this as follows:

I created a LinkMgr template that output a string like the array format described above. I then created a GCB that called the LinkMgr and that specific template.

I then made the following UDT, based on a lot of googling:

Code: Select all

$foo = 'links'; 
$linksgcb = $smarty->fetch('globalcontent:'.$foo);
$arr  = array($linksgcb);
return $arr;
What this does, however is take the string I produced in my LinkMgr template and use the complete literal thing as the first label, for the first value of the array. So it's disregarding the desired output I made from this LinkMgr template and takes it literal as a string.

Anyone have any ideas on how to fix the UDT so it actually produces a correct UDT?

I tried this:

Code: Select all

$foo = 'links'; 
$linksgcb = $smarty->fetch('globalcontent:'.$foo);
$arr  = array('label1'=>'value1','label2'=>'value2');
return $arr;
To test it, and that actually worked. But offcourse what I want is the actual output from the LinkMgr module.
Last edited by Guido on Tue Oct 21, 2014 11:08 am, edited 1 time in total.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Building a sortable list of links via ECB

Post by velden »

The output of a template usually is a string.
So you will need to convert the string to an associative array in the UDT. Though that seems to be a inefficient way of doing things (build a string and then convert it to an array).

I don't know whether the LinkMgr module has an api that can be used directly from within the UDT, or maybe you can get the links from database.
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Building a sortable list of links via ECB

Post by Guido »

Yeah I know, not exactly the nicest way to do it. And your remark about it being a string would explain the string being used as the label for the first value.

In the meantime I tried:

Code: Select all

$test = cms_utils::get_module('LinkMgr');
print_r($test);
Which returned an array of generic values from the module, like labels and standard text, no info about the test links I made. So I think no, there is no API for this in the LinkMgr, like FEU or the Gallery module have.

So now I'd like to set up a DB Query in the UDT. Can anyone help me with some CMSMS specific guidelines for making a DB query?
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Building a sortable list of links via ECB

Post by velden »

Maybe the code in modules/LinkMgr/action.default.php is a good starting point.
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Building a sortable list of links via ECB

Post by Guido »

Thanks. I now have this:

Code: Select all

$db = cmsms()->GetDb();

if (isset($params["category"]) && $params["category"] != '') 
{
	$category = strtoupper(cms_html_entity_decode(trim($params['category'])));

	$query1 = 'SELECT * FROM ' . cms_db_prefix() . 'module_linkmgr_categories WHERE upper(link_category) = "'.$category.'" ORDER BY cat_id ASC';

	$dbresult1 =& $db->Execute($query1);

	if($dbresult1 && $row1 = $dbresult1->FetchRow()) 
	{
		$link_cat = $row1['cat_id'];
	} else { 
		die('Error: '.mysql_error());
	}

	$query = 'SELECT l.*, lc.link_category FROM ' . cms_db_prefix() . 'module_linkmgr_links l LEFT JOIN '.cms_db_prefix().'module_linkmgr_categories lc ON l.link_cat = lc.cat_id WHERE link_cat='.$link_cat.' AND link_status = "published" ORDER BY link_name ASC';
} else {
	$query = 'SELECT l.*, lc.link_category FROM ' . cms_db_prefix() . 'module_linkmgr_links l LEFT JOIN '.cms_db_prefix().'module_linkmgr_categories lc ON l.link_cat = lc.cat_id WHERE link_status = "published" ORDER BY link_category,link_name ASC';
}

$dbresult =& $db->Execute($query);
$items = Array();
while($row = $dbresult->FetchRow())
{                    
	$row['link_name'] = $row['link_url'];
}
return $items;
put together myself, but when I use the print_r modifier on it, it only outputs 'Array1'.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Building a sortable list of links via ECB

Post by velden »

Code: Select all

$items['link_name'] = $items['link_url'];
[/s]
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Building a sortable list of links via ECB

Post by Guido »

Ah, stupid of me... Still doesnt work, but shouldn't I use it like

Code: Select all

$items[$row['link_name']] = $items[$row['link_url']];
?
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Building a sortable list of links via ECB

Post by velden »

Guido wrote:Ah, stupid of me... Still doesnt work, but shouldn't I use it like

Code: Select all

$items[$row['link_name']] = $items[$row['link_url']];
?
Yeah, you're probably right. Stupid of me this time.
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Building a sortable list of links via ECB

Post by Guido »

OK, I'm slowly getting somewhere. I now have:

Code: Select all

$db = cmsms()->GetDb();

$query = 'SELECT l.*, lc.link_category FROM ' . cms_db_prefix() . 'module_linkmgr_links l LEFT JOIN '.cms_db_prefix().'module_linkmgr_categories lc ON l.link_cat = lc.cat_id ORDER BY link_category,link_name ASC';

$dbresult =& $db->Execute($query);

$items = Array();
$rowclass = 'row1';

while ($dbresult && $row = $dbresult->FetchRow()){
	$i = count($items);
	$items[$i]['link_name'] = $row['link_url'];
}

return $items;
Shows only empty lines in the ECB sortable list, but you can select them. When I do, and assign the product of the sortable list to a var, and then print_r that var I get the link names!
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Building a sortable list of links via ECB

Post by velden »

Code: Select all

$i = count($items);
   $items[$i]['link_name'] = $row['link_url'];
This builds an mult-dimensional array. You don't want that.

I think this should work:

Code: Select all

$items[$row['link_name']] = $row['link_url'];
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Building a sortable list of links via ECB

Post by Guido »

Almost got it! I now have:

Code: Select all

$db = cmsms()->GetDb();

$query = 'SELECT l.*, lc.link_category FROM ' . cms_db_prefix() . 'module_linkmgr_links l LEFT JOIN '.cms_db_prefix().'module_linkmgr_categories lc ON l.link_cat = lc.cat_id ORDER BY link_category,link_name ASC';

$dbresult =& $db->Execute($query);

$items = Array();
$rowclass = 'row1';

while ($dbresult && $row = $dbresult->FetchRow()){
	$i = count($items);
	$items[$i][$row['link_url']] = $row['link_name'];
}

return $items;
Which works. The only thing is: the $i count also shows up in the sortable list options. Not a problem in functionality, because it works. It outputs a list of the link names, I can select them and it will output a csv list of the URL's belonging to those names. Without the [$i] it won't increment so I don't get all the links, just the last one. Anyone have a suggestion on how to remove the [$i] from visibility while preserving it's function?

You just beat me to the punch ;-) Gonna try that right now.
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: [SOLVED] Building a sortable list of links via ECB

Post by Guido »

Velden, thanks. Your suggestion did the trick. Now just out of curiosity, I'm going to research Multi-dimension arrays.
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: [SOLVED] Building a sortable list of links via ECB

Post by Guido »

Okay, for anyone who wants to do the same thing, here's the complete story:

You need the ECB and the LinkMgr module. Using the ECB module you create a content block for the backend by placing this in your page template:

Code: Select all

{content_module module="ECB" tab="Links" field="sortablelist" block="testsortablelist" label="Choose the links to display" allowduplicates='false' label_left="Links that will be displayed" label_right="Available links" udt='links' assign='links'}
You see the values come from a UDT. You have to create it by pasting this code:

Code: Select all

$db = cmsms()->GetDb();

$query = 'SELECT l.*, lc.link_category FROM ' . cms_db_prefix() . 'module_linkmgr_links l LEFT JOIN '.cms_db_prefix().'module_linkmgr_categories lc ON l.link_cat = lc.cat_id ORDER BY link_category,link_name ASC';

$dbresult =& $db->Execute($query);

$items = Array();
$rowclass = 'row1';

while ($dbresult && $row = $dbresult->FetchRow()){
	$items[$row['link_url']] = $row['link_name'];
}

return $items;
Save it as 'links'. This code retrieves all the available links from the Link Manager from the database.

In the first block of code (that goes in your page template) you see the result will be assigned to a variable names 'links'.

That variable will be passed into an array, by placing this code into your page template, below the first code block:

Code: Select all

{assign var='linkarray' value=','|explode:$links}
Now you have an array available that contains the URL's to the links you selected for this page.

Now edit the default template for the Link Manager, so it looks like this:

Code: Select all

<div class="linkmgr">
  <ul>
  {foreach from=$items item='item'}
  {foreach from=$linkarray item='link'}
{if $link == $item.link_url}
    <li><a href="{$item.link_url}" target="_blank">{$item.link_name}</a><br />{$item.link_desc|default:''}</li>
{/if}
  {/foreach}
  {/foreach}
  </ul>
</div>
I added a loop through the previously mentioned 'linkarray' we created in the page template, and created a variable named 'link' for each instance in the array. Now via the {if} statement, we check if the $link variable matched the $item.link_url variable that comes from the module itself. So here basically you filter all links to show only the ones you added to the $linkarray and presto: your output will be only the links you selected. Of course you have to call the {LinkMgr} somewhere in you page or page template to actually display these.

EDIT: to-do, create an escape so that the LinkMgr won't be called when no links are selected. You could create an {if} statement around the {LinkMgr} call that checks if the {$linkarray} is empty
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: [SOLVED] Building a sortable list of links via ECB

Post by Guido »

Okay, some known problems so far:
  1. It won't work with URL's that have a comma in them. I'd never seen them, until of course I added the links needed in this project, they have commas.
  2. For now, I noticed that the order of the links is not the order of the list you create in the backend sortable list. It will adapt to the order of the links in the Link Manager.
What you need to do to sort out the second problem is simply re-order the foreach loops in the Link Manager template, so like this:

Code: Select all

<div class="linkmgr">
  <ul class="rel-lijst lijst wit">
  {foreach from=$linkarray item='link'}
  {foreach from=$items item='item'}
{if $link == $item.link_url}
    <li><a href="{$item.link_url}" target="_blank">{$item.link_name}</a><br />{$item.link_desc|default:''}</li>
{/if}
  {/foreach}
  {/foreach}
  </ul>
</div>
About the first problem, I have asked the developer of ECB if it's possible to use a different delimiter (like '|') in the module.
Post Reply

Return to “Modules/Add-Ons”