[SOLVED] CTLModuleMaker with Drop Down List of CMS Page Links?

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

[SOLVED] CTLModuleMaker with Drop Down List of CMS Page Links?

Post by Russ »

I'm looking to have a drop down list in a module (built using CTLModuleMaker)
for selecting CMS pages and saving their links - a bit like the one in TinyMCE.

Essentially I need a form output with something like:

Code: Select all

<form>
<select>
  <option selected="yes" value="nolink">No Link</option>
  <optgroup label="Home">
	<option value="/">Home</option>
    <option value="/home/homesub1">Home Sub Page 1</option>
     <option value="/home/homesub2">Home Sub Page 2</option>
  </optgroup>
  <optgroup label="News">
	<option value="/news">News</option>
  </optgroup>
  <optgroup label="Help">
    <option value="/help">Help</option>
    <option value="/help/sub">Help Sub Page</option>
  </optgroup>
</select>
</form>
Assuming a set of pages, with 'Home', 'News' and 'Help' as the top level menus e.g.

Home
Home Sub Page 1
Home Sub Page 2

News
Help
Help Sub Page

Obviously this actually would need to be dynamic to pick up the actual pages for the CMS and create the form and then save the elements to the database.

(For this purpose, I guess I could ignore External Links, Section Headers and Seperators as theydon't have pages you cna link to.)

I've tried a few things playing with menu templaltes and the Sitemap plugin code, but with no real good results. Does ayone have any useful pointers?
Last edited by Russ on Mon Nov 23, 2009 4:33 pm, edited 1 time in total.
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by plger »

Two solutions coming to me:
1) You could do it with the "Attachments" module
2) You could create a field of "predefined list" type in your ctlmm module and in the function get_predefinedoptions you could change the static options to some code that would retrieve the pages/dropdown options (requires php knowledge, but nothing very hard, and you could copy most code from the Attachments module or any other that retrieves a pages combo)
plger
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by Russ »

1. Thanks very much plger, I did not know about "Attachmnets" very nice, not as nice as CTLModuleMaker, but very nice ;-)

2. Thanks you again, with your pointer I can see where it might go given the function get_predefinedoptions :)
However, the module stores the value of the 'index' of the drop down list box item and not the 'value' (text) of the item. Given that I'd probably need to store a real reference to the item text (hierarchy_path?) for the drop down, I could not see a way to do that? It is obviously dynamic?
As it stand with the predefined list you get:
None
Home
News
Events

when I would guess I would want something like this: (value as hierarchy_path? and text or label as maybe menu_text or even hierarchy_path again...)
None
Home
News
events

Does that make sense?
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by plger »

My second solution implicitly required a bit of php programming: you replace the static array in the predefined options with some code that retrieves the page options.
An example :

Code: Select all

$db = $this->GetDb();
$dbresult = "SELECT hierarchy_path, menu_text FROM ".cms_db_prefix()."content";
$options = array();
while($dbresult && $row = $dbresult->FetchRow())  $options[$row['menu_text']] = $row['hierarchy_path'];
return $options; 
Here I use hierarchy path, but I could also use "id" and then later retrieve informations relative to that content id.


A more elegant (and api consistent) way would be to change the input generated in your action.editLEVEL.php to the content manager one. Let's say you have :

Code: Select all

$this->smarty->assign("fieldname_input", $this->CreateInputDropdown($id,"fieldname",$fieldnameoptions,-1,isset($item)?$item->fieldname:0));
You could change that to:

Code: Select all

$cntoperations = $gCms->getContentOperations();
$theinput = $cntoperations->CreateHierarchyDropdown("",isset($item)?$item->fieldname:-1,$id."fieldname");
$this->smarty->assign("fieldname_input", $theinput);
This would make a more beautiful combo (with depth), and save the content id. Later on you can just retrieve the page url with the following (assuming the content id is stored in $content_id):

Code: Select all

$manager = $gCms->GetHierarchyManager();
$node = $manager->sureGetNodeById($content_id);
$content = $node->GetContent();
echo '<a href="'.$content->getURL().'">'.$content->Name().'</a>';
plger
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by Russ »

Many thanks again plger, I realised about the PHP code, but it was the 'CreateHierarchyDropdown' I was missing ;-) I could not get the order and grouping right. Easy when you know how! Good Karma coming your way...

In the end, however, I did not do that (CreateHierarchyDropdown), because:
1. It was easier for me to have the 'hierarchy_path' in the actual database
2. I could not see a way to filter out inactive, non-content or invisible pages using the CreateHierarchyDropdown method or change to using hierarchy rather than content _id???
Any ideas - just for learning ;-)

I actually used:

Code: Select all


case "image_image_link_path":
$db = $this->GetDb();
$query = "SELECT content_id, hierarchy_path, menu_text FROM cms_content WHERE type=\"content\" AND active=1 and show_in_menu = 1 ORDER by hierarchy ASC";
$dbresult = $db->Execute($query);
$options = array($this->Lang("image_image_link_path_no_link")=>$this->Lang("image_image_link_path_no_link"));
while($dbresult && $row = $dbresult->FetchRow()){$options[$row['hierarchy_path']] = $row['menu_text'];}
return $options;

Whilst the resulting drop down menu isn't so pretty, it is in order and it only shows pages you can actually link to!

Now I need to know if there is a way to read the language file variables from the user created template, or how to do that?
I tried assigning with smarty, which works fine for the admin templates, but not the user created ones?
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by plger »

Russ wrote: Now I need to know if there is a way to read the language file variables from the user created template, or how to do that?
What do you mean it doesn't work? It always shows the same language?
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by Russ »

Sorry plger, what I meant was how to use a language variable (from a language file like en.US.php or other language file) e.g.

Code: Select all

$lang["MyVariable"] = "MyText";
I think what I'm asking is what is the smarty declaration and where to put it to use the 'MyVariable' in a user defined template?
I thought it would be something like:

Code: Select all

$this->smarty->assign("MyVariableText", $this->Lang("MyVariable"));
In a user created template, e.g.

Code: Select all

<h3>{$item->name}</h3>
{$MyVariableText}
But I tried this in the admin.defaultadmin.php file but it did not work for me.

Is that clear, I hope so, thank you again for your help.
Last edited by Russ on Sat Nov 21, 2009 7:39 pm, edited 1 time in total.
User avatar
plger
Forum Members
Forum Members
Posts: 196
Joined: Wed Oct 15, 2008 10:38 am

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by plger »

Well it's supposed to work.
Make sure you place it before the $this->ProcessTemplate (juste place it at the beginning).
If you are working on the frontend, make sure to pass the language to the module tag:

Code: Select all

{cms_module module="mymodule" lang=$page_lang}
Or something similar.
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: CTLModuleMaker with Drop Down List of CMS Page Links?

Post by Russ »

Thanks again plger, it was my mistake! It seems that if you want to use variables in a user defined template (one you create to display information from the module), you actually need to declare and assign the smarty in the action.default file not the action.defaultadmin file. But I think I have the hang of it now and have created the module I needed, I just need to tidy it up a bit with some specific help etc.

So many thanks, a brilliant module!
Post Reply

Return to “Developers Discussion”