モジュール 開発: Difference between revisions

m
Adding interlang links (links to translated versions of this page in other languages) triggered by origin English page "Module_development" update.
m (Adding interlang links (links to translated versions of this page in other languages) triggered by origin English page "Module_development" update.)
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<!-- BEGIN origin interlang links -->
<!-- BEGIN interlang links -->
<!-- You can edit this section but do NOT remove these comments
<!-- Do NOT edit this section
     Links below will be automatically replicated on translated pages by PolyglotBot -->
     Links below are automatically managed by PolyglotBot
    You can edit links on the English source page : Module_development -->
[[en:Module_development]]
[[fr:Développement_module]]
[[fr:Développement_module]]
[[es:Desarrollo_de_un_módulo]]
[[es:Desarrollo_de_un_módulo]]
Line 11: Line 13:
{{TemplateDocDevEn}}
{{TemplateDocDevEn}}


Dolibarr向け新規 モジュール/アドオン を作成するには, 複数のステップがある. このチュートリアルで説明するのは、Dolibarrの特質拡張用モジュールを作るステップのそれぞれである, 以下の1つとか、複数の組み合わせとかのように :
Dolibarr向け新規 モジュール/アドオン を作成するには, 複数のステップがある. このチュートリアルで記述するのは、Dolibarrの特質拡張用モジュールを作るステップのそれぞれである, 以下の1つとか、複数の組み合わせとかのように :


*データベースに新規テーブルを追加.
*データベースに新規テーブルを追加.
Line 46: Line 48:


=自前モジュールを作る=
=自前モジュールを作る=
==自前モジュールに関するモジュール説明(Module descriptor)を作る (必須)==
==自前モジュールに関するモジュール記述(Module descriptor)を作る (必須)==
'''必須となる時期''': アドオンを開発すると早急に必要、たとえゴールが何であれ (例外はスキンの追加).
'''必須となる時期''': アドオンを開発すると早急に必要、たとえゴールが何であれ (例外はスキンの追加).


'''Dolibarr 9.0 から, モジュールビルダ (ModuleBuilder) モジュールを使用すべきだ、モジュール説明を生成するための標準モデルとして使える'''
'''Dolibarr 9.0 から, モジュールビルダ (ModuleBuilder) モジュールを使用すべきだ、モジュール記述を生成するための標準モデルとして使える'''


===自前のモジュール説明 (module descriptor) を作成する===
===自前のモジュール記述 (module descriptor) を作成する===
最初のステップは、モジュールに対するファイル説明 (file descriptor) を作成すること.  
最初のステップは、モジュールに対するファイル記述 (file descriptor) を作成すること. (訳者注: 以下の内容は'''モジュールビルダを使うことで自動設定'''される)


*ディレクトリ作成: '''/htdocs/custom/''mymodule''/core/modules''' (外部モジュール向け) または '''/htdocs/''mymodule''/core/modules''' ここはDolibarr の公式コアモジュール (official core module) とする場合. 次にファイル modMyModule.class.php をディレクトリ '''htdocs/modulebuilder/template''' からコピーして上記のディレクトリに作成. ファイルの名称を modMyModule.class.php から 自前モジュールの目的に合致した名称に変更. ファイル名は、先頭に 'mod' があり、最後が .class.php で終わる (例えば、ファイル名を '新名' にするなら, 'mod新名.class.php' となる).
*ディレクトリ作成: '''/htdocs/custom/''mymodule''/core/modules''' (外部モジュール向け) または '''/htdocs/''mymodule''/core/modules''' ここはDolibarr の公式コアモジュール (official core module) とする場合. 次にファイル modMyModule.class.php をディレクトリ '''htdocs/modulebuilder/template''' からコピーして上記のディレクトリに作成. ファイルの名称を modMyModule.class.php から 自前モジュールの目的に合致した名称に変更. ファイル名は、先頭に 'mod' があり、最後が .class.php で終わる (例えば、ファイル名を '新名' にするなら, 'mod新名.class.php' となる).
Line 64: Line 66:
自前モジュールのファイルディスクリプタの準備ができた。有効化するには次ステップを参照.
自前モジュールのファイルディスクリプタの準備ができた。有効化するには次ステップを参照.


===自前説明をテストする===
===自前記述をテストする===


Dolibarrを起動し、'''設定->モジュール (Setup->module'''?) ページを表示.
Dolibarrを起動し、'''設定->モジュール (Setup->module'''?) ページを表示.
モジュール説明でプロパティバージョン (property version) を設定すると、自前の新規モジュールを載せた新規行とそれを有効化するかどうかの選択が表示されるはず. プロパティバージョンを "develop" または "experimental" に設定した場合、自前モジュールを見るには、まず '''設定->その他 (Setup-Others)''' に行き、 MAIN_FEATURES_LEVEL を 1 に設定してから "experimental" モジュールを見るか、2にして から "development" モジュールを見るかする必要がある.
モジュール記述でプロパティバージョン (property version) を設定すると、自前の新規モジュールを載せた新規行とそれを有効化するかどうかの選択が表示されるはず. プロパティバージョンを "develop" または "experimental" に設定した場合、自前モジュールを見るには、まず '''設定->その他 (Setup-Others)''' に行き、 MAIN_FEATURES_LEVEL を 1 に設定してから "experimental" モジュールを見るか、2にして から "development" モジュールを見るかする必要がある.


==新規モジュールファイルに関するパスツリー (必須)==
==新規モジュールファイルに関するパスツリー (必須)==
Line 73: Line 75:
注:2行目だけが必須.
注:2行目だけが必須.


{{TemplateModuleTreeSimple}}
{{TemplateModuleTreeSimpleJA}}


==自前 SQL テーブル と PHP DAO クラス (任意)==
==自前 SQL テーブル と PHP DAO クラス (任意)==
'''When''': If your module needs to manage data of its own
'''使用場面''': 自前モジュールがそれ自身のデータを管理する必要がある場合


===Create your .sql file===
===自前の .sql ファイルを作成===
If your module is designed to manage data of its own, which are not available in the standard version of Dolibarr, it is necessary to define SQL tables to store the data.
自前モジュールが、Dolibarr標準版では利用できない独自データを管理する設計である場合、そのデータを格納するためのSQLテーブルを定義する必要がある.


Scripts for creating tables and loading data by your own module are saved into a subdirectory sql ('''"mymodule/sql"''') of your module. (With ModuleBuilder, the directory and all sql files will be created automatically when you will create a new Object from tab Object).
自前モジュールでテーブルを作成したり、データを読込むためのスクリプトは、自前モジュールのsqlサブディレクトリ ("'''mymodule/sql'''") に保存される。(ModuleBuilder では、オブジェクト タブから新しいオブジェクトを作成するとき、このディレクトリとすべての sql ファイルが自動作成される).


Then check into your descriptor file, into function '''init''' that the line
次に、記述ファイルでの、'''init''' 関数にて、次の行


<syntaxhighlight lang="PHP">$this->_load_tables('/mymodule/sql/');</syntaxhighlight>
<syntaxhighlight lang="PHP">$this->_load_tables('/mymodule/sql/');</syntaxhighlight>


is not commented.
が、コメントでないことを確認する.


''Rules to follow:''
''従うべきルール:''


*Add the files about creating tables commands on the principle of a file '''llx_matable.sql''' per table, with a possibly file '''llx_matable.key.sql''' (see existing files in '''install/mysql/tables''' for examples).
*テーブルごとに '''llx_matable.sql''' ファイルを1つ、'''llx_matable.key.sql''' ファイルを可能なら1つ、作成するという原則に基づき、テーブル作成コマンドのファイルを追加する(例として、 '''install/mysql/tables''' にあるファイルを参照).
*The recommended type and name for SQL fields are defined into page [[Language_and_development_rules#Table_and_fields_structures]].
*SQLフィールドの推奨される型と名前は、このページに定義されてる [[Language_and_development_rules#Table_and_fields_structures]].
*To manage data, you must create a file called '''data.sql''' inside directory '''/mymodule/sql/''' that contains SQL command to add/edit/delete data.
*データ管理のため、 '''/mymodule/sql/''' ディレクトリ内に  '''data.sql''' というファイルを作成し、データの追加/編集/削除を行う SQL コマンドを記述すること.
*Don't use the double quote for strings (eg 'chaine' not "chaine") because the double quote have a specific meaning in PostGreSQL
*文字列にダブルクォートを使わない(例: "chaine" でなく 'chaine') ダブルクォートは PostGreSQL では特定の意味を持つ


Example of content of a file data.sql
例としてファイル data.sql の中身は
<syntaxhighlight lang="sql">
<syntaxhighlight lang="sql">
  delete from llx_const where name='MYMODULE_IT_WORKS' and entity='__ENTITY__';
  delete from llx_const where name='MYMODULE_IT_WORKS' and entity='__ENTITY__';
Line 102: Line 104:
</syntaxhighlight>
</syntaxhighlight>


Files must be operational for the database mysql. Rem: The files of other databases are not maintained. They will be read and converted on the fly by the driver of the other database.
ファイルは、mysql データベースで動作可能であること. 備考: 他データベースのファイルはメンテナンスされない. 他データベースのドライバによって、その場で読込まれ変換さる.


===Test your .sql files===
===自前の  .sql ファイルをテストする===


Once the files ready, you can return to the page to enable Dolibarr modules and disable the module, drop those tables in the database (if they already exists) and reactivate the module. The tables should be recreated by the activation of the module. If this is not the case, check your scripts by passing them by hand, or check the Dolibarr logs.
ファイルの準備ができたら、Dolibarrモジュールを有効にするページに戻り、モジュールを無効にして、データベース内のそれらのテーブルを削除し(すでに存在している場合)、モジュールを再有効化することができる。モジュールの有効化により、テーブルが再作成されるはず。もしそうでない場合、スクリプトを手で渡して確認するか、Dolibarrのログを確認すること.


===Generate the PHP DAO class===
===PHP DAO クラスを生成===
The DAO PHP files should have been generated by ModuleBuilder after creating an object.
DAO PHPファイルは、オブジェクトを作成した後にモジュールビルダによって生成されているはず.


You can however create them manually by copying existing object however using ModuleBuilder is highly recommended. You should find examples into '''htdocs/modulebuilder/templates/class/myobject.class.php'''
だとしても、既存オブジェクトをコピーして手動作成することもできる、しかし、モジュールビルダの使用を強く勧める。実例がここにあるはず '''htdocs/modulebuilder/templates/class/myobject.class.php'''


In this class, there are already operational CRUD (Create/Read/Update/Delete) methods to perform an insert, a fetch (select), an update, a delete of a table row. Edit the file to use the correct module name, tables and place this file in the class subdirectory of the module. The Module Builder allows you to do this with just a few mouse clicks.
このクラスでは、テーブル行の挿入、取得(選択)、更新、削除を実行するためのCRUD(作成/読込/更新/削除)メソッドがすでに実行可能となっている。正しいモジュール名、テーブルを使用するようにファイルを編集し、このファイルをモジュールのクラスサブディレクトリに配置する。モジュールビルダを使用すると、マウスを数回クリックするだけで、この作業を実行できる.


==タブ管理 (任意)==
==タブ管理 (任意)==


===Add or remove your own tabs on object sheets===
===オブジェクトシートに自前のタブを追加・削除できる===
'''When''': To add a tab on an object (invoices, orders, proposals, member ...)
'''使用場面''': オブジェクト(請求書、注文書、提案書、メンバー...)にタブを追加する場合


To do this go into the file descriptor of module previously created and edit the $this->tabs array:
そのためには、先に作成したモジュールのファイル記述に行って、$this->tabs 配列を編集する:


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 129: Line 131:
</syntaxhighlight>
</syntaxhighlight>


The table should contain a list of strings, each string representing a new tab.
テーブルには文字列のリストが含まれ、各文字列は新しいタブを表す.文字列の形式は ": " で区切られた5つの部分からなる.
The format of the string consisting of 5 parts separated by ":"


*Part 1: The element type (object type) where the tab should appear is a value from the following:
*Part 1: タブを表示するエレメントタイプ(オブジェクトタイプ)で、以下の値:
{{TemplateModuleTabs}}
{{TemplateModuleTabsJA}}


*Part 2: A code to identify tab to add (start with +) or to remove (start with -)
*Part 2: タブのコードで、タブの追加(+で始まる)または削除(-で始まる)を識別する.
*Part 3: The title of the tab. This can be a hard read or better code translation in a file lang.
*Part 3: タブのタイトル.タイトルが読みずらいなら、コードにして言語ファイルで翻訳するとよい.
*Part 4: The name of the file "*.lang" which contains correspondence between the code translation and language to display. If this name is followed with @mymodule, Dolibarr will search translation file "*.lang" within the module, so htdocs/mymodule/langs/code_CODE/mylangfile.lang, otherwise Dolibarr will look for file htdocs/langs/code_CODE/mylangfile.lang
*Part 4: ファイル名 "*.lang" は、コード翻訳と表示する言語の対応を含む.この名前の後に@mymoduleが続く場合、 Dolibarrはモジュール内の翻訳ファイル "*.lang "を検索するので、htdocs/mymodule/langs/code_CODE/mylangfile.langとなり、そうでない場合はhtdocs/langs/code_CODE/mylangfile.langとなる.
*Part 5: A condition to test if tab must be visible. Put 1 to be always visible.
*Part 5: タブを常時表示すべきかのテスト条件.常時表示の場合は1を指定.
*Part 6: The URL of the page to display when you click on the tab. The __ID__ string will be replaced automatically by the Id of the element concerned.
*Part 6: タブをクリックしたときに表示されるページのURL。__ID__の文字列は自動的に当該要素のIdに置き換えられる.


To feed the contents of the tab with data from the database, see the next chapter.
タブの内容へデータベースからのデータを注入するには、次章を参照.


In order to get the names  'tabname' of existing tabs, you have to check the names used in the function 'product_prepare_head' of the file  'core/lib/module.lib.php' corresponding to section '$head[$h][2]'.
既存タブの名前 'tabname' を取得するには、ファイル 'core/lib/module.lib.php' の関数 'product_prepare_head' で使用されている名前で、セクション '$head[$h][2]' に対応するものをチェックする必要がある.


===Show tabs navigation into your own pages===
===自前ページにタブナビゲーションを表示===
'''When''': To show all standard tabs of an object (product, tiers, etc.) on your page.
'''使用場面''': 自前ページにオブジェクト(製品、階層、など)の標準タブを表示したい


You must follow these steps :
以下の手順に従う :


'''1. Include files you need into your file'''
'''1. Include files you need into your file'''
Line 218: Line 219:


==PHP 画面の作成や更新 (任意)==
==PHP 画面の作成や更新 (任意)==
'''When''': If the purpose of your module is to add features that require new or modified screens.
使用場面: モジュールの目的が、画面の新規作成または変更を必要とする機能の追加である場合.


===Create a new PHP screen===
===新規 PHP 画面の作成===
You must then create your PHP pages that will show/edit data from tables using the skeleton templates provided as an example in the directory '''modulebuilder/myobject_page.php''' (For the development of a script from the command line, see [[Script development]]).
次に、 '''modulebuilder/myobject_page.php''' ディレクトリに例として提示されているスケルトンテンプレートを使用して、テーブルのデータを表示/編集するPHPページを作成する必要がある (コマンドラインからのスクリプト開発については、 [[Script development]] を参照).


To create a new user screen, create a subdirectory of '''htdocs''' (if not already done) for your module (in '''htdocs/mymodule''' for example) to store the pages you will create.
新しいユーザー画面を作成するには、'''htdocs''' のサブディレクトリを作成し(まだ作成されていない場合)、作成するページを保存するために、モジュール(例えば、'''htdocs/mymodule''' )のサブディレクトリを作成する.


Copy, into that directory, the file '''myobject_page.phpp''' that will serve as a starting point for your page file.
そのディレクトリに、ページファイルの出発点となるファイル '''myobject_page.php''' をコピーする。.
Edit the file to have a correct relative path to the main.inc.php file.  
main.inc.phpファイルへの相対パスが正しくなるようにファイルを編集する.  
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
// Load Dolibarr environment
// Load Dolibarr environment
Line 243: Line 244:
if (! $res) die("Include of main fails");
if (! $res) die("Include of main fails");
</syntaxhighlight>
</syntaxhighlight>
As you can see, there are several tries to load the main.inc.php (or master.inc.php). The goal is to succeed in the most cases as possible. The minimum is 2 lines: one to try to load the master/main.inc.php into the dolibarr root directory and another one to try to load the file to support the case when the module is deployed into the "custom" directory. But you can have more to deal more situation. The provided example should be able to load the file main/master.inc.php in nearly all situation/setups.
ご覧のように、main.inc.php(または master.inc.php)のロードは何回か試行される。目標は、大抵のケースで成功すること。最小は2行: master/main.inc.phpをdolibarrルートディレクトリにロードしようとする行と、モジュールが "custom "ディレクトリにデプロイされるケースをサポートするためにファイルをロードしようとする行。しかし、より多くの状況に対応するため、より多くを持つことができる。提供された例は、ほぼすべての状況/セットアップで main/master.inc.php ファイルをロードできるはず。


Note that you may add more "../" depending on the depth of the file relative to your module directory tree.
注意: モジュール・ディレクトリ・ツリーに対するファイルの深さによっては、さらに"../"を追加する。


It's in the main.inc.php file that is loaded technical environment variables and permissions. The following variables are objects positioned in this file:
main.inc.phpファイルでは、技術上の環境変数とパーミッションを読み込む。以下の変数はこのファイルに配置されているオブジェクト:


*$user    Object that contains the characteristics of the user + his permissions.
*$user    ユーザーの特徴とパーミッションを保持するオブジェクト。
*$conf    Object that contains Dolibarr configuration.
*$conf    Dolibarrの設定を保持するオブジェクト
*$db      Object that contains an opened connection handler to the database.
*$db      データベースへのオープンされた接続ハンドラを保持するオブジェクト。
*$langs  Object that contains the user's language.
*$langs  ユーザー言語を保持するオブジェクト。


Then enter your code to display the page.
次にページを表示するために自前コードを入力する.


*To include a class or library dedicated to the module is done using a Dolibarr function (and not using directly include_once):
*モジュール専用クラスやライブラリをインクルードするには、(include_onceを直接使わず)Dolibarr関数を使う:


''Example :''
''Example :''
Line 263: Line 264:
</syntaxhighlight>
</syntaxhighlight>


*To include classes provided with Dolibarr, use the following syntax:
*Dolibarrで提供されているクラスをインクルードするには、以下の構文を使う:


''Example :''
''Example :''
Line 286: Line 287:
See chapter [[Hooks_system#Implement_the_Hook]] to know how to use Dolibarr existing hooks to add/replace code at Dolibarr hooks place.
See chapter [[Hooks_system#Implement_the_Hook]] to know how to use Dolibarr existing hooks to add/replace code at Dolibarr hooks place.


===Database access===
===データベース アクセス===
If you need to edit some data in the database inside your own table, use the PHP class generated before.
自前テーブルの中でデータベースのデータを編集する必要がある場合、前に生成されたPHPクラスを使うこと。


If you to make access to tables with no dedicated PHP class available, this is always possible (for example if you want to get a list of records with a particular join or filter). In this case, this is a code samples to follow:
専用の PHP クラスがないテーブルにアクセスしたい場合 (たとえば、特定の結合やフィルタを使用したレコードの一覧を取得したい場合など) 、これはいつでも可能。この場合に、倣うべきコード例は:


To make an insert, update or delete:
操作が、 insert, update または delete:


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
Line 300: Line 301:
</syntaxhighlight>
</syntaxhighlight>


To read:
操作が read:


<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
2,052

edits