Creating modules for PHPShop.CMS Free - Stumbling (part 1)
Communication with users of the PHPShop.CMS Free system and getting them on the same "rake" prompted me to write useful tips.
Useful tips for using template functions.
The set () method assigns a template variable for use in templates as var @, for example:
and inserting its look @ my_var @ into the template tpl file will allow you to display the phrase “We love Habr” on the site. Often in modules like formgenerator or comment, interception and change of variables is used. If we use the model $ this-> set ('my_var', $ var) in the hook function, then we completely rewrite the value of the variable, in our example we rewrite the contents of the page. Everything would be fine, but this rule works if we have only one module enabled that intercepts the contents of the page, and if we have more of them, then each new module will overwrite the output of the other module. In such cases, the third argument of the $ this-> set method, which takes the value true, comes to the rescue. Using this argument will allow us not to rewrite the variable, but to supplement it; they will allow us to display the result of the work of the modules in turn - the output of the form and comments.
Decision:
If the module uses the template for this module described in the module config.ini, then a record of the form:
will display an empty result. To use the global template function ParseTemplateReturn () in modules, you must specify the second argument true, which puts the function in the mode of reading template files from the module folder, and not from the general phpshop / templates /.
Decision:
The template engine allows you to use php functions. Insert Format:
To avoid errors, the php compiler uses double quotes "instead of single ', for example:
Useful tips on using database access functions.
Quick selection of module settings
There is a very good and short way to get module settings, which are stored in the modulename_systems database:
In this record, we need to specify the name of the database from which we want to get data, for example
and list with comma the names of the fields that we need
To access this data, use:
For work with MySQL PHPShopOrm is used . An example of a typical sample is:
This all simplifies and standardizes queries, but if you need to perform complex selections with combining tables? In such cases, the $ PHPShopOrm-> query () method is used, for example:
A frequent “side” gets in the way of the developer when, when fetching $ PHPShopOrm-> select () and provided that in DB 1 the record displays a one-dimensional array, and if more, then a multidimensional one. To circumvent such uncertainty, the argument array ('limit' => 1) is used, in which the array will always be one-dimensional and work with it through a record of the form:
in cases of an explicit predisposition of the result to multiple fields, the recording format is used with an explicit limit indication array ('limit' => 1000):
Source wiki.phpshopcms.ru
UPD: Part 2
Template Engine
Useful tips for using template functions.
Tricks with set ()
The set () method assigns a template variable for use in templates as var @, for example:
$var = "Мы любим Хабр";
$this->set('my_var',$var);
and inserting its look @ my_var @ into the template tpl file will allow you to display the phrase “We love Habr” on the site. Often in modules like formgenerator or comment, interception and change of variables is used. If we use the model $ this-> set ('my_var', $ var) in the hook function, then we completely rewrite the value of the variable, in our example we rewrite the contents of the page. Everything would be fine, but this rule works if we have only one module enabled that intercepts the contents of the page, and if we have more of them, then each new module will overwrite the output of the other module. In such cases, the third argument of the $ this-> set method, which takes the value true, comes to the rescue. Using this argument will allow us not to rewrite the variable, but to supplement it; they will allow us to display the result of the work of the modules in turn - the output of the form and comments.
Decision:
$this->set('my_var',$var,true);
ParseTemplateReturn () in modules
If the module uses the template for this module described in the module config.ini, then a record of the form:
$comment=ParseTemplateReturn($GLOBALS['SysValue']['templates']['comment']['comment_content']);
will display an empty result. To use the global template function ParseTemplateReturn () in modules, you must specify the second argument true, which puts the function in the mode of reading template files from the module folder, and not from the general phpshop / templates /.
Decision:
ParseTemplateReturn($GLOBALS['SysValue']['templates']['comment']['comment_content'],true);
Using php in templates
The template engine allows you to use php functions. Insert Format:
@php
...
php@
To avoid errors, the php compiler uses double quotes "instead of single ', for example:
@php
echo "Мы любим Хабр!";
php@
Work with a DB
Useful tips on using database access functions.
Quick selection of module settings
There is a very good and short way to get module settings, which are stored in the modulename_systems database:
// Настройки модуля
PHPShopObj::loadClass("array");
class PHPShopCommentArray extends PHPShopArray {
function PHPShopCommentArray() {
$this->objType=3;
$this->objBase=$GLOBALS['SysValue']['base']['comment']['comment_system'];
parent::PHPShopArray("enabled","flag","other");
}
}
In this record, we need to specify the name of the database from which we want to get data, for example
$this->objBase=$GLOBALS['SysValue']['base']['comment']['comment_system'];
and list with comma the names of the fields that we need
parent::PHPShopArray("enabled","flag","other");
To access this data, use:
$PHPShopCommentArray = new PHPShopCommentArray();
$enabled = $PHPShopCommentArray->getParam('enabled');
Complex queries in ORM
For work with MySQL PHPShopOrm is used . An example of a typical sample is:
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000));
if(is_array($data))
foreach($data as $row){ .... }
This all simplifies and standardizes queries, but if you need to perform complex selections with combining tables? In such cases, the $ PHPShopOrm-> query () method is used, for example:
$result = $PHPShopOrm->query('SELECT a.*, b.login FROM '.$GLOBALS['SysValue']['base']['comment']['comment_log'].' AS a
JOIN '.$GLOBALS['SysValue']['base']['users']['users_base'].' AS b ON a.user_id = b.id
WHERE a.page="'.$page.'" order by a.id desc');
while($row = mysql_fetch_array($result)) { ..... }
Hash with multidimensionality select () result
A frequent “side” gets in the way of the developer when, when fetching $ PHPShopOrm-> select () and provided that in DB 1 the record displays a one-dimensional array, and if more, then a multidimensional one. To circumvent such uncertainty, the argument array ('limit' => 1) is used, in which the array will always be one-dimensional and work with it through a record of the form:
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000));
if(is_array($data))
{ .... }
in cases of an explicit predisposition of the result to multiple fields, the recording format is used with an explicit limit indication array ('limit' => 1000):
$data=$PHPShopOrm->select(array('id','name'),array('id'=>'=10'),array('order'=>'id DESC'),array('limit'=>1000));
if(is_array($data))
foreach($data as $row){ .... }
Source wiki.phpshopcms.ru
UPD: Part 2