Page 1 of 1

Module Writing Tutorial: limit not working

Posted: Wed Oct 26, 2016 4:53 pm
by paulbaker
After the Geekmoot presentation I am working my way through the excellent "Introduction to Writing Modules for CMS Made Simple" document which can be downloaded here:
http://docs.cmsmadesimple.org/uploads/M ... torial.pdf

I have the basic module working, I can create/edit/delete holidays and they show OK in front end, both summary and detail templates.

But I am stuck on page 33 "Adding A Detail Page Parameter and a Page limit".

I have added the code in this section to my module. The detailpage parameter works OK. But the limit parameter is being ignored.

Have I messed something up or is there an error in that part of the document?

My module call is:

Code: Select all

{cms_module module=Holidays limit=4 detailpage=home}
I currently have 5 items of data and they are all being shown. I was expecting only 4.

Here are the files that need additions in this section:

Holidays.module.php:

Code: Select all

<?php

class Holidays extends CMSModule
{
 const MANAGE_PERM = 'manage_holidays';
 public function GetVersion() { return '0.1'; }
 public function GetFriendlyName() { return $this->Lang('friendlyname'); }
 public function GetAdminDescription() { return $this->Lang('admindescription'); }
 public function IsPluginModule() { return TRUE; }
 public function HasAdmin() { return TRUE; }
 public function VisibleToAdminUser() { return $this->CheckPermission(self::MANAGE_PERM); }
 public function GetAuthor() { return 'Paul'; }
 public function GetAuthorEmail() { return 'asdf@asdf.com'; }
 public function UninstallPreMessage() { return $this->Lang('ask_uninstall'); }

    public function InitializeFrontend() {
        $this->SetParameterType('hid',CLEAN_INT);
        $this->SetParameterType('pagelimit',CLEAN_INT);
        $this->SetParameterType('detailpage',CLEAN_STRING);
    }
    public function InitializeAdmin() {
        $this->CreateParameter('hid',null,$this->Lang('param_hid'));
        $this->CreateParameter('pagelimit',1000,$this->Lang('param_pagelimit'));
        $this->CreateParameter('detailpage',null,$this->Lang('param_detailpage'));
    }

}
?>
action.default.php:

Code: Select all

<?php
if( !defined('CMS_VERSION') ) exit;
$limit = (isset($params['limit'])) ? (int) $params['limit'] : 1000;
$limit = max(1,$limit);
$detailpage = $returnid;
if( isset($params['detailpage']) ) {
    $hm = CmsApp::get_instance()->GetHierarchyManager();
    $node = $hm->sureGetNodeByAlias($params['detailpage']);
    if( is_object($node) ) $detailpage = $node->get_tag('id');
}
$query = new HolidayQuery(array('published'=>1,'limit'=>$limit));
$holidays = $query->GetMatches();
$tpl = $smarty->CreateTemplate($this->GetTemplateResource('default.tpl'),null,null,$smarty);
$tpl->assign('holidays',$holidays);
$tpl->assign('detailpage',$detailpage);
$tpl->display();
?>
class.HolidayQuery.php:

Code: Select all

<?php
class HolidayQuery extends CmsDbQueryBase
{
public function execute()
{
    if( !is_null($this->_rs) ) return;
    $sql = 'SELECT SQL_CALC_FOUND_ROWS H.* FROM '.CMS_DB_PREFIX.'mod_holidays H';
    if( isset($this->_args['published']) ) {
        // store only draft or published items
        $tmp = $this->_args['published'];
        if( $tmp === 0 ) {
            $sql .= ' WHERE published = 0';
        } else if( $tmp === 1 ) {
            $sql .= ' WHERE published = 1';
        }
    }
    $sql .= ' ORDER BY the_date DESC';
    $db = \cms_utils::get_db();
    $this->_rs = $db->SelectLimit($sql,$this->_limit,$this->_offset);
    IF( $db->ErrorMsg() ) throw new \CmsSQLErrorException($db->sql.' -- '.$db->ErrorMsg());
    $this->_totalmatchingrows = $db->GetOne('SELECT FOUND_ROWS()');
}
    
    
    
    public function &GetObject()
    {
        $obj = new HolidayItem;
        $obj->fill_from_array($this->fields);
        return $obj;
    }
    
    public function __construct($args = '')
    {
        parent::__construct($args);
        if( isset($this->_args['limit']) ) $this->_limit = (int) $this->_args['limit'];
    }
}
?>
default.tpl:

Code: Select all

<div class="holidayWrapper">
  {foreach $holidays as $holiday}
    <div class="holiday">
      <div class="row">
        <div class="col-sm-6">
   <a href="{cms_action_url action=detail hid=$holiday->id returnid=$detailpage}">{$holiday->name}</a>
</div>
<div class="col-sm-6 text-right">{$holiday->the_date|date_format:'%x'}</div>
      </div>
    </div>
  {foreachelse}
     <div class="alert alert-danger">{$mod->Lang('sorry_noholidays')}</div>
  {/foreach}
</div>
Thanks O0

----------------------------------------------
Cms Version: 2.1.5
Installed Modules:
AdminSearch: 1.0.2
CMSContentManager: 1.1.1
DesignManager: 1.1.1
FileManager: 1.5.2
Holidays: 0.1
MicroTiny: 2.0.3
ModuleManager: 2.0.4
Navigator: 1.0.3
News: 2.50.5
Search: 1.50.2

Config Information:
php_memory_limit:
max_upload_size: 32000000
url_rewriting: none
page_extension:
query_var: page
auto_alias_content: true
locale:
set_names: true
timezone: Europe/London
permissive_smarty: false

Php Information:
phpversion: 5.6.24
md5_function: On (True)
json_function: On (True)
gd_version: 2
tempnam_function: On (True)
magic_quotes_runtime: Off (False)
E_ALL: 22519
E_STRICT: 0
E_DEPRECATED: 0
test_file_timedifference: No time difference found
test_db_timedifference: No time difference found
create_dir_and_file: 1
memory_limit: 128M
max_execution_time: 120
register_globals: Off (False)
output_buffering: 4096
disable_functions:
open_basedir:
test_remote_url: Success
file_uploads: On (True)
post_max_size: 32M
upload_max_filesize: 32M
session_save_path: /tmp (1777)
session_use_cookies: On (True)
xml_function: On (True)
xmlreader_class: On (True)
check_ini_set: On (True)
curl: On

Performance Information:
allow_browser_cache: Off (False)
browser_cache_expiry: 60
php_opcache: Off (False)
smarty_cache: Off (False)
smarty_compilecheck: Off (False)
smarty_cache_udt: Off (False)
auto_clear_cache_age: On (True)
Server Information:
Server Software: Apache
Server Api: cgi-fcgi
Server Os: Linux 2.6.18-471.3.1.el5.lve0.8.72 On x86_64
Server Db Type: MySQL (mysqli)
Server Db Version: 5.6.30
Server Db Grants: Found a "GRANT ALL" statement that appears to be suitable

Permission Information:
tmp: /home/cthuk/public_html/modules/tmp (0755)
tmp_cache: /home/cthuk/public_html/modules/tmp/cache (0755)
templates_c: /home/cthuk/public_html/modules/tmp/templates_c (0755)
modules: /home/cthuk/public_html/modules/modules (0755)
uploads: /home/cthuk/public_html/modules/uploads (0755)
File Creation Mask (umask): /home/cthuk/public_html/modules/tmp/cache (0755)
config_file: 0444
----------------------------------------------

Re: Module Writing Tutorial: limit not working

Posted: Wed Oct 26, 2016 5:44 pm
by Jos
There are a few little typo's on page 33:

STEPS:
1.
$this->SetParameterType('pagelimit',CLEAN_INT);
should be: $this->SetParameterType('limit',CLEAN_INT);

2.
$this->CreateParameter('pagelimit',1000,$this->Lang('param_pagelimit'));
should be: $this->CreateParameter('limit',1000,$this->Lang('param_limit'));


Also on page 39 (when you come to it)

STEPS:
1.
$search = \cms_utils::get_search_module();
if( is_object($module) ) {
should be: if( is_object($search) ) {

Hope that helps

Re: Module Writing Tutorial: limit not working

Posted: Wed Oct 26, 2016 7:42 pm
by calguy1000
Thanks for catching the typos. I will fix them soon.

Re: Module Writing Tutorial: limit not working

Posted: Thu Oct 27, 2016 12:22 pm
by paulbaker
Now working perfectly. And yes the correction seems obvious now I see it. Thanks Jos! :D