语言和开发规则: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
Tag: 2017 source edit
mNo edit summary
Tag: 2017 source edit
Line 23: Line 23:
= 版权规范 =
= 版权规范 =
* 所有 PHP 文件必须以下面文本开头。
* 所有 PHP 文件必须以下面文本开头。
<source lang="php">
<syntaxhighlight lang="PHP">
<?php
<?php
/* Copyright (C) YYYY John Doe  <email@email.com>
/* Copyright (C) YYYY John Doe  <email@email.com>
Line 30: Line 30:
  */
  */
...
...
</source>
</syntaxhighlight>
当编辑项目的现有文件时,必须在他人下添加版权行。
当编辑项目的现有文件时,必须在他人下添加版权行。


Line 49: Line 49:
* 当必须用相同的值初始化多个变量时,必须使用单独的声明(由;分隔)
* 当必须用相同的值初始化多个变量时,必须使用单独的声明(由;分隔)


<source lang="php">
<syntaxhighlight lang="PHP">
$var1=1;$var2=1;$var3=1;
$var1=1;$var2=1;$var3=1;
</source>
</syntaxhighlight>
代替
代替
<source lang="php">
<syntaxhighlight lang="PHP">
$var1=$var2=$var3=1;
$var1=$var2=$var3=1;
</source>
</syntaxhighlight>
后一种情况下更慢。
后一种情况下更慢。


* 字符串必须由单引号或双引号分隔,字符串内的变量必须在引号之外。
* 字符串必须由单引号或双引号分隔,字符串内的变量必须在引号之外。


<source lang="php">
<syntaxhighlight lang="PHP">
print 'My text show my '.$variable.' !';
print 'My text show my '.$variable.' !';
</source>
</syntaxhighlight>


* 注释必须使用C语法(即对单行注释使用双斜线,对代码块注释使用斜线星)
* 注释必须使用C语法(即对单行注释使用双斜线,对代码块注释使用斜线星)


<source lang="php">
<syntaxhighlight lang="PHP">
/* 块注释
/* 块注释
  *
  *
Line 81: Line 81:
}
}


</source>
</syntaxhighlight>


* 如果函数成功,函数必须返回等于或大于0的值,如果错误,则严格低于0。
* 如果函数成功,函数必须返回等于或大于0的值,如果错误,则严格低于0。
Line 130: Line 130:
例如: '''创建表table llx_mytable的文件为llx_mytable.sql''':
例如: '''创建表table llx_mytable的文件为llx_mytable.sql''':


<source lang="sql">
<syntaxhighlight lang="sql">
-- ===========================================================================
-- ===========================================================================
-- Copyright (C) 2013 Author <email@author.com>
-- Copyright (C) 2013 Author <email@author.com>
Line 164: Line 164:
   import_key varchar(14)                -- Use by import process
   import_key varchar(14)                -- Use by import process
)type=innodb;
)type=innodb;
</source>
</syntaxhighlight>


例如: '''为表table llx_mytable创建关键字/索引的文件是llx_mytable.key.sql''':
例如: '''为表table llx_mytable创建关键字/索引的文件是llx_mytable.key.sql''':


<source lang="sql">
<syntaxhighlight lang="sql">
-- ===========================================================================
-- ===========================================================================
-- Copyright (C) 2013 Author <email@author.com>
-- Copyright (C) 2013 Author <email@author.com>
Line 189: Line 189:


ALTER TABLE llx_mytable ADD CONSTRAINT fk_mytable_fk_field FOREIGN KEY (fk_field) REFERENCES llx_matablepere (rowid);
ALTER TABLE llx_mytable ADD CONSTRAINT fk_mytable_fk_field FOREIGN KEY (fk_field) REFERENCES llx_matablepere (rowid);
</source>
</syntaxhighlight>


== 表和字段结构 ==
== 表和字段结构 ==
Line 259: Line 259:
* Alias usage/Fields naming
* Alias usage/Fields naming
When doing select, we can use alias to simplify writing/reading of requests:
When doing select, we can use alias to simplify writing/reading of requests:
<source lang="sql">
<syntaxhighlight lang="sql">
select chp1, chpxxx2 as chp2 from table2 as t1, table2 as t2 where t1.chpx = t2.chpy
select chp1, chpxxx2 as chp2 from table2 as t1, table2 as t2 where t1.chpx = t2.chpy
</source>
</syntaxhighlight>
However, we must not used alias for update request as they are not compatible with Mysql 3.1.
However, we must not used alias for update request as they are not compatible with Mysql 3.1.


* Using SELECT * is forbidden ! When using SELECT you must define complete list of fields to get. This avoids confusion. Example:
* Using SELECT * is forbidden ! When using SELECT you must define complete list of fields to get. This avoids confusion. Example:
<source lang="sql">
<syntaxhighlight lang="sql">
SELECT field_a, field_b, field_c FROM table_1 WHERE field_d = '$id'
SELECT field_a, field_b, field_c FROM table_1 WHERE field_d = '$id'
</source>
</syntaxhighlight>


* Into SQL requests, you must quote fields except the fields that contain amounts which must be stored as double or real type. Quotes on numbers may result in saving as a different value. For example 412.62 in an insert will be saved as value 412.61999512 into database (due to implicit conversion string to numeric) if the target field has type double(24,8). Only PHP see value 412.61999512. Other tools will see 412.62 giving a sense that there is no problem. But it's PHP that has the good vision. There is really a wrong value into database. By removing quotes on numbers, no problem occurs.
* Into SQL requests, you must quote fields except the fields that contain amounts which must be stored as double or real type. Quotes on numbers may result in saving as a different value. For example 412.62 in an insert will be saved as value 412.61999512 into database (due to implicit conversion string to numeric) if the target field has type double(24,8). Only PHP see value 412.61999512. Other tools will see 412.62 giving a sense that there is no problem. But it's PHP that has the good vision. There is really a wrong value into database. By removing quotes on numbers, no problem occurs.


Example:
Example:
<source lang="sql">
<syntaxhighlight lang="sql">
Good:    INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', 412.62)
Good:    INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', 412.62)
Bad:      INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', '412.62')
Bad:      INSERT INTO table_1 (field_txt, field_num) VALUES ('txt', '412.62')
</source>
</syntaxhighlight>


Note, problem of float numbers is same problem on all langauges and not only when inserting data into database. It occurs also with any language when you work on "real" numbers, so numbers must be, as soon as they are affected, cleaned with function price2num with second parameter defined to :
Note, problem of float numbers is same problem on all langauges and not only when inserting data into database. It occurs also with any language when you work on "real" numbers, so numbers must be, as soon as they are affected, cleaned with function price2num with second parameter defined to :
Line 283: Line 283:


For example, don't do:
For example, don't do:
<source lang="php">
<syntaxhighlight lang="PHP">
$sql="SELECT rowid FROM table where datefield = NOW()";
$sql="SELECT rowid FROM table where datefield = NOW()";
</source>
</syntaxhighlight>
but do:
but do:
<source lang="php">
<syntaxhighlight lang="PHP">
$sql="SELECT rowid FROM table where datefield = '".$this->db->idate(dol_now())."'";
$sql="SELECT rowid FROM table where datefield = '".$this->db->idate(dol_now())."'";
</source>
</syntaxhighlight>


例如,不要这样做:
例如,不要这样做:
<source lang="php">
<syntaxhighlight lang="PHP">
$sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7";
$sql="SELECT rowid FROM table where DATEDIFF(table.datefield, NOW()) > 7";
</source>
</syntaxhighlight>
but do:
but do:
<source lang="php">
<syntaxhighlight lang="PHP">
$sql="SELECT rowid FROM table where datefield < '".$this->db->idate(dol_now() - (7 * 24 * 3600))."'";
$sql="SELECT rowid FROM table where datefield < '".$this->db->idate(dol_now() - (7 * 24 * 3600))."'";
</source>
</syntaxhighlight>


An other advantage of this rule, is that request benefits of index because we are making a compare of a field with a fixed value. When using datediff, you make an operation on field before comparison, this means database can't use the index on field, resulting on very bad performance compared to solution without the datediff.
An other advantage of this rule, is that request benefits of index because we are making a compare of a field with a fixed value. When using datediff, you make an operation on field before comparison, this means database can't use the index on field, resulting on very bad performance compared to solution without the datediff.
Line 318: Line 318:


MySQL Syntax:
MySQL Syntax:
<source lang="sql">
<syntaxhighlight lang="sql">
UPDATE table_taget as target, table_source as source SET fieldtarget=source.fieldsource
UPDATE table_taget as target, table_source as source SET fieldtarget=source.fieldsource
WHERE source.rowid=target.rowid;
WHERE source.rowid=target.rowid;
</source>
</syntaxhighlight>


PgSQL Syntax:
PgSQL Syntax:
<source lang="sql">
<syntaxhighlight lang="sql">
UPDATE table_taget as target SET fieldtarget=source.fieldsource
UPDATE table_taget as target SET fieldtarget=source.fieldsource
FROM table_source as source WHERE source.rowid=target.rowid;
FROM table_source as source WHERE source.rowid=target.rowid;
</source>
</syntaxhighlight>


There is no native SQL request "update from" in dolibarr core. But in your module you should do :
There is no native SQL request "update from" in dolibarr core. But in your module you should do :
<source lang="php">
<syntaxhighlight lang="PHP">
if ($this->db->type=='pgsql') {
if ($this->db->type=='pgsql') {
$sql="UPDATE table_taget as target SET fieldtarget=source.fieldsource
$sql="UPDATE table_taget as target SET fieldtarget=source.fieldsource
Line 338: Line 338:
WHERE source.rowid=target.rowid";
WHERE source.rowid=target.rowid";
}
}
</source>
</syntaxhighlight>


= HTML 规范 =
= HTML 规范 =
Line 346: Line 346:
* Links href must be absolute and use the function dol_buildpath() to get absolute path from a relative path and img tag must be build using function img_picto().
* Links href must be absolute and use the function dol_buildpath() to get absolute path from a relative path and img tag must be build using function img_picto().
For example:
For example:
<source lang="php">
<syntaxhighlight lang="PHP">
print '<a href="'.dol_buildpath('/mydir/mypage.php').'">'.img_picto('Texte alt','namepictopng','').'</a>';
print '<a href="'.dol_buildpath('/mydir/mypage.php').'">'.img_picto('Texte alt','namepictopng','').'</a>';
</source>
</syntaxhighlight>


* HTML tables must have columns with no forced width, except for columns that contains data we know the length. For example, a column with a picto only can be forced to with="20px".
* HTML tables must have columns with no forced width, except for columns that contains data we know the length. For example, a column with a picto only can be forced to with="20px".
Line 354: Line 354:


* Javascript/ajax code and call to javascript files into php pages must be avoided. However, if you need to include javascript code, you must add a condition on "$conf->use_javascript_ajax"
* Javascript/ajax code and call to javascript files into php pages must be avoided. However, if you need to include javascript code, you must add a condition on "$conf->use_javascript_ajax"
<source lang="php">
<syntaxhighlight lang="PHP">
if ($conf->use_javascript_ajax) {
if ($conf->use_javascript_ajax) {
...  // php code generating javascript here
...  // php code generating javascript here
}
}
</source>
</syntaxhighlight>


* Popup windows must not be used, except for tooltips (and must have a condition as explained before).
* Popup windows must not be used, except for tooltips (and must have a condition as explained before).
Line 411: Line 411:
With PHP, like other languages (Java for exemple), non integer data (float, real, double) are not reliable for calculation.
With PHP, like other languages (Java for exemple), non integer data (float, real, double) are not reliable for calculation.
Try to make for example  
Try to make for example  
<source lang="php">
<syntaxhighlight lang="PHP">
print 239.2 - 229.3 - 9.9;
print 239.2 - 229.3 - 9.9;
</source>
</syntaxhighlight>
You wont get zero but a very small decimal number. If you get zero her, you should be able to find other examples that don't work.
You wont get zero but a very small decimal number. If you get zero her, you should be able to find other examples that don't work.
Problem of float is general, so a variable that is a result of a calculation using decimal numbers must ALWAYS be cleaned using the function '''price2num()''' with the econd parameter to: 'MU', 'MT' or 'MS' depending on need (see description of function).
Problem of float is general, so a variable that is a result of a calculation using decimal numbers must ALWAYS be cleaned using the function '''price2num()''' with the econd parameter to: 'MU', 'MT' or 'MS' depending on need (see description of function).
<source lang="php">
<syntaxhighlight lang="PHP">
print price2num(239.2 - 229.3 - 9.9, 'MT');
print price2num(239.2 - 229.3 - 9.9, 'MT');
</source>
</syntaxhighlight>
If data manipulated is not an amount, then using MU, MT, MS has no sense, and you must use the function '''round()'''.
If data manipulated is not an amount, then using MU, MT, MS has no sense, and you must use the function '''round()'''.


Line 427: Line 427:
== 比较版本 ==
== 比较版本 ==
If your code need to make different things depending on Dolibarr version, you can use the following tip to detect and compare versions
If your code need to make different things depending on Dolibarr version, you can use the following tip to detect and compare versions
<source lang="php">
<syntaxhighlight lang="PHP">
$version=preg_split('/[\.-]/',DOL_VERSION);
$version=preg_split('/[\.-]/',DOL_VERSION);
if (versioncompare($version,array(5,0,-4)) >= 0) { //mycode for 5.0 only; } // For dolibarr 5.0.* (the -4 means we include also alpha, beta, rc and rcX)
if (versioncompare($version,array(5,0,-4)) >= 0) { //mycode for 5.0 only; } // For dolibarr 5.0.* (the -4 means we include also alpha, beta, rc and rcX)
</source>
</syntaxhighlight>


But this solution need to include the function versioncompare. An alternative solution to test version is to do:
But this solution need to include the function versioncompare. An alternative solution to test version is to do:
<source lang="php">
<syntaxhighlight lang="PHP">
if ((float) DOL_VERSION >= 5.0) { //mycode for 5.0 only; } // For dolibarr 5.0.*
if ((float) DOL_VERSION >= 5.0) { //mycode for 5.0 only; } // For dolibarr 5.0.*
</source>
</syntaxhighlight>


== 日志 ==
== 日志 ==
Add logs to your code using function
Add logs to your code using function
<source lang="php">
<syntaxhighlight lang="PHP">
dol_syslog($yourmessage, LOG_INFO|LOG_DEBUG|LOG_WARNING|LOG_ERR);
dol_syslog($yourmessage, LOG_INFO|LOG_DEBUG|LOG_WARNING|LOG_ERR);
</source>
</syntaxhighlight>


==  工作目录 ==
==  工作目录 ==
Line 448: Line 448:


The directory can be created into your code by the following function:
The directory can be created into your code by the following function:
<source lang="php">
<syntaxhighlight lang="PHP">
$mymoduledir=DOL_DATA_ROOT.'/mymodule';
$mymoduledir=DOL_DATA_ROOT.'/mymodule';
dol_mkdir($mymoduledir);
dol_mkdir($mymoduledir);
</source>
</syntaxhighlight>


If you need a directory to store temporary data, this directory must be '''DOL_DATA_ROOT.'/mymodule/temp''''
If you need a directory to store temporary data, this directory must be '''DOL_DATA_ROOT.'/mymodule/temp''''

Navigation menu