ListIt Module: How to set up 'Read more' function Stephen Owens www.Studio-Owens.com

Transcription

ListIt Module: How to set up 'Read more' function Stephen Owens www.Studio-Owens.com
Page 1 of 5
Stephen Owens
www.Studio-Owens.com
HowTo ListIt Summary and Detail View
ListIt Module: How to set up 'Read more' function
Source (downloaded on February 17, 2011):
http://forum.cmsmadesimple.org/viewtopic.php?f=7&t=51542
Allan1412 asked on Tuesday February 01, 2011:
The 'ListIt' module is a great way to display certain types of content.
Can anyone help with this?
I need to use ListIt with a 'Summary / Detail' function similar to CGBlog ie display the summary content
with a 'Read more' link to display the summary and full content in a separate page.
Stephen Owens Answered on Thursday February 17, 2011:
Sure here's a very simple proof of concept showing a single detail record and a summary from ListIt, without
making any modifications to the source code. I've never used the CGblog (I use NEWS on Studio-Owens.com as a
Blog) so I have no idea if this would be similar. The ListIt Module is based upon the EAV datamodel so it isn't going
to be suited for large amounts of records.
I am presenting two methods here. Method one just filters the existing data. Method two queries the DB to find
the single record data. Both use the same concept to build URLs and create summary pages.
Method One Filter The Existing Data
Create a UDT called listit_details_check
Put this code in the UDT...
CODE: SELECT ALL
if ($params['thisid'] == (int)$_GET['cntnt01itemid']) {
echo True;
} else {
echo False;
}
Next modify the ListIt template named "default"
Add a {capture} and {if} inside the {foreach}. This is what the stock "default" template will look like when
modified (new code is on lines 2,3, and 19)...
CODE: SELECT ALL
{foreach from=$items item=item}
{capture assign="idCheck"}{listit_details_check thisid=$item.item_id}{/capture}
{if $idCheck == True}
<div class="item">
Page 2 of 5
HowTo ListIt Summary and Detail View
Stephen Owens
www.Studio-Owens.com
<h2 class="item-title">{$item.title|cms_escape}</h2>
<div class="item-category">Category: {$item.category_name|cms_escape}</div>
{if !empty($item.fielddefs)}
<div class="item-properties">
{foreach from=$item.fielddefs item=fielddef}
{if $fielddef.type == 'upload_file' || $fielddef.type == 'select_file'}
{$fielddef.name|cms_escape}: <a href="{$fielddef.dir|cms_escape}/
{$fielddef.value|cms_escape}">{$fielddef.value|cms_escape}</a><br />
{else}
{$fielddef.name|cms_escape}: {$fielddef.value|cms_escape}<br />
{/if}
{/foreach}
</div>
{/if}
</div><!-- item -->
{/if}
{/foreach}
Method Two Query The Database For The Record
Create a UDT called listit_get_record
Put this code in the UDT...
CODE: SELECT ALL
$db_prefix = $params['db_prefix'];
$listit_name = $params['listit_name'];
$itemid = (int)$_GET['cntnt01itemid'];
if (is_int($itemid)) {
$gCms = cmsms(); //global $gCms;
$smarty = &$gCms->GetSmarty();
$db =& $gCms->GetDB();
$r = $db->Execute("SELECT category_id, title, active FROM ".
$db_prefix."module_".$listit_name."_item AS i
WHERE i.item_id=".$db->qstr($itemid)." LIMIT 1");
if ($r) {
$data= array();
$row = $r->FetchRow();
$data['title'] = $row['title'];
$data['active'] = $row['active'];
$r1 = $db->Execute("SELECT category_name FROM ".$db_prefix."module_".
$listit_name."_category AS c
WHERE c.category_id=".$db->qstr($row['category_id'])." LIMIT 1");
if ($r1) {
$rowC = $r1->FetchRow();
$data['category_name'] = $rowC['category_name'];
}
Page 3 of 5
HowTo ListIt Summary and Detail View
Stephen Owens
www.Studio-Owens.com
$r2 = $db->Execute("SELECT fielddef_id, value FROM ".$db_prefix."module_".
$listit_name."_fieldval AS fv
WHERE fv.item_id=".$db->qstr($itemid));
if ($r2) {
while($r2 && $rowFv = $r2->FetchRow()) {
$r3 = $db->Execute("SELECT alias FROM ".$db_prefix."module_".
$listit_name."_fielddef AS fd
WHERE fd.fielddef_id=".$db->qstr($rowFv['fielddef_id'])." LIMIT 1");
if ($r3) {
$rowFd = $r3->FetchRow();
$data[$rowFd['alias']] = $rowFv['value'];
}
}
}
$smarty->assign('itemDetail', $data);
}
}
The UDT is used like this; inserting your own database table prefix and change the listit_name if you cloned this
as a new module.
CODE: SELECT ALL
{listit_get_record db_prefix='cms_' listit_name='listit'}
The {$itemDetail} array being returned by this UDT always contains these keys:
CODE: SELECT ALL
{$itemDetail.title|cms_escape}
{$itemDetail.category_name|cms_escape}
{$itemDetail.active|cms_escape}
The {$itemDetail} array will also contain the alias and value of your custom defined Field Definitions. It can be
used like the following; replace alias with the alias you defined:
CODE: SELECT ALL
{$itemDetail.alias|cms_escape}
Next modify the ListIt template named "default" constructing your own custom template. An example would be:
CODE: SELECT ALL
{listit_get_record db_prefix='cms_' listit_name='listit'}
{if $itemDetail.active == 1}
<div class="item" style="margin-top:50px;">
<h2 class="item-title">{$itemDetail.title|cms_escape}</h2>
<div class="item-category">Category: {$itemDetail.category_name|
cms_escape}</div>
<div class="item-properties">
Something: {$itemDetail.alias|cms_escape}<br/>
Page 4 of 5
HowTo ListIt Summary and Detail View
Stephen Owens
www.Studio-Owens.com
Something Link: <a href="{$itemDetail.alias|cms_escape}">Something</a><br />
</div>
</div><!-- item -->
{/if}
URL construction for the Details Page
Then construct a URL to call your new the ListIt Module detail page:
&cntnt01itemid=1 : is the ListIt item_id that is to be displayed
&cntnt01returnid=58 : is the page template to use for displaying your single record.
CODE: SELECT ALL
http://www.yoursite.com/index.php?
mact=ListIt,cntnt01,default,0&cntnt01itemid=1&cntnt01returnid=58
Pretty URLs
If using Mod Rewrite you could clean up the URL by doing the method found in the CMSMS handbook on this page
under "For News module":
http://wiki.cmsmadesimple.org/index.php/User_Handbook/Tips_And_Tricks/Installation
Basically create a subfolder called /listit and add an .htaccess file to it with the following code (change the .htm
if you are using a different extension or no extension at all)...
CODE: SELECT ALL
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]*)(.htm)$ index.php?
mact=ListIt,cntnt01,default,0&cntnt01itemid=$1&cntnt01returnid=62 [NC,L]
If you didn't want to hard code the page to use as the template you could use a rewrite rule with something like
this...
CODE: SELECT ALL
RewriteRule ^([0-9]*)/([0-9]*)(.htm)$ index.php?
mact=ListIt,cntnt01,default,0&cntnt01itemid=$1&cntnt01returnid=$2 [NC,L]
the first INT passed would be used for the ListIt item_id and the second one passed would be used for the page
content_id.
Summary Index
For this just add some <a href=""> tags to a new summary template, maybe something like...
Page 5 of 5
HowTo ListIt Summary and Detail View
Stephen Owens
www.Studio-Owens.com
CODE: SELECT ALL
{foreach from=$items item=item}
<div class="item">
<a href="weeklyspecials/{$item.item_id}.htm">{$item.title|cms_escape}</a>
</div><!-- item -->
{/foreach}
Proof of the Above Concepts
I renamed my ListIt as WeeklySpecials, which is actually being used to feed out an RSS file for automatic postings
to Facebook and Twitter.
Single Item Detail - UGLY URL Proof:
http://www.cafemarkcumberland.com/index.php?
mact=WeeklySpecials,cntnt01,default,0&cntnt01itemid=1&cntnt01returnid=62
Single Item Detail - Pretty URL Proof:
http://www.cafemarkcumberland.com/weeklyspecials/1.htm
All Items Summary with Links Proof:
http://www.cafemarkcumberland.com/weeklyspecials.htm
Last Edit: added in Method Two and tried to make the post a little clearer.