4d7174d7ed4020f11bf3e424cb4e0dc93c0db7d9
[civicrm-core.git] / xml / templates / dao.tpl
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2017
31 *
32 * Generated from {$table.sourceFile}
33 * {$generated}
34 * (GenCodeChecksum:{$genCodeChecksum})
35 */
36
37 require_once 'CRM/Core/DAO.php';
38 require_once 'CRM/Utils/Type.php';
39
40 /**
41 * {$table.className} constructor.
42 */
43 class {$table.className} extends CRM_Core_DAO {ldelim}
44
45 /**
46 * Static instance to hold the table name.
47 *
48 * @var string
49 */
50 static $_tableName = '{$table.name}';
51
52 /**
53 * Should CiviCRM log any modifications to this table in the civicrm_log table.
54 *
55 * @var boolean
56 */
57 static $_log = {$table.log};
58
59 {foreach from=$table.fields item=field}
60 /**
61 {if $field.comment}
62 * {$field.comment}
63 {/if}
64 *
65 * @var {$field.phpType}
66 */
67 public ${$field.name};
68
69 {/foreach} {* table.fields *}
70
71 /**
72 * Class constructor.
73 */
74 function __construct( ) {ldelim}
75 $this->__table = '{$table.name}';
76
77 parent::__construct( );
78 {rdelim}
79
80 {if $table.foreignKey || $table.dynamicForeignKey}
81 /**
82 * Returns foreign keys and entity references.
83 *
84 * @return array
85 * [CRM_Core_Reference_Interface]
86 */
87 static function getReferenceColumns() {ldelim}
88 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
89 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
90 {foreach from=$table.foreignKey item=foreign}
91 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
92 {/foreach}
93
94 {foreach from=$table.dynamicForeignKey item=foreign}
95 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
96 {/foreach}
97 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
98 {rdelim}
99 return Civi::$statics[__CLASS__]['links'];
100 {rdelim}
101 {/if} {* table.foreignKey *}
102
103 /**
104 * Returns all the column names of this table
105 *
106 * @return array
107 */
108 static function &fields( ) {ldelim}
109 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
110 Civi::$statics[__CLASS__]['fields'] = array (
111 {foreach from=$table.fields item=field}
112
113 {if $field.uniqueName}
114 '{$field.uniqueName}'
115 {else}
116 '{$field.name}'
117 {/if}
118 => array(
119 'name' => '{$field.name}',
120 'type' => {$field.crmType},
121 {if $field.title}
122 'title' => ts('{$field.title}'),
123 {/if}
124 {if $field.comment}
125 'description' => '{$field.comment|replace:"'":"\'"}',
126 {/if}
127 {if $field.required}
128 'required' => {$field.required},
129 {/if} {* field.required *}
130 {if $field.length}
131 'maxlength' => {$field.length},
132 {/if} {* field.length *}
133 {if $field.precision}
134 'precision' => array({$field.precision}),
135 {/if}
136 {if $field.size}
137 'size' => {$field.size},
138 {/if} {* field.size *}
139 {if $field.rows}
140 'rows' => {$field.rows},
141 {/if} {* field.rows *}
142 {if $field.cols}
143 'cols' => {$field.cols},
144 {/if} {* field.cols *}
145
146 {if $field.import}
147 'import' => {$field.import},
148 'where' => '{$table.name}.{$field.name}',
149 'headerPattern' => '{$field.headerPattern}',
150 'dataPattern' => '{$field.dataPattern}',
151 {/if} {* field.import *}
152 {if $field.export}
153 'export' => {$field.export},
154 {if ! $field.import}
155 'where' => '{$table.name}.{$field.name}',
156 'headerPattern' => '{$field.headerPattern}',
157 'dataPattern' => '{$field.dataPattern}',
158 {/if}
159 {/if} {* field.export *}
160 {if $field.rule}
161 'rule' => '{$field.rule}',
162 {/if} {* field.rule *}
163 {if $field.default}
164 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
165 {/if} {* field.default *}
166 'table_name' => '{$table.name}',
167 'entity' => '{$table.entity}',
168 'bao' => '{$table.bao}',
169 'localizable' => {if ($field.localizable)}{1}{else}{0}{/if},
170
171 {if $field.FKClassName}
172 'FKClassName' => '{$field.FKClassName}',
173 {/if}
174 {if $field.serialize}
175 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
176 {/if}
177 {if $field.html}
178 {assign var=htmlOptions value=$field.html}
179 'html' => array(
180 {*{$htmlOptions|@print_array}*}
181 {foreach from=$htmlOptions key=optionKey item=optionValue}
182 '{$optionKey}' => '{$optionValue}',
183 {/foreach}
184 ),
185 {/if} {* field.html *}
186 {if $field.pseudoconstant}
187 {assign var=pseudoOptions value=$field.pseudoconstant}
188 'pseudoconstant' => array(
189 {*{$pseudoOptions|@print_array}*}
190 {foreach from=$pseudoOptions key=optionKey item=optionValue}
191 '{$optionKey}' => '{$optionValue}',
192 {/foreach}
193 )
194 {/if} {* field.pseudoconstant *} ),
195 {/foreach} {* table.fields *}
196 );
197 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
198 {rdelim}
199 return Civi::$statics[__CLASS__]['fields'];
200 {rdelim}
201
202 /**
203 * Return a mapping from field-name to the corresponding key (as used in fields()).
204 *
205 * @return array
206 * Array(string $name => string $uniqueName).
207 */
208 static function &fieldKeys( ) {ldelim}
209 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
210 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
211 {rdelim}
212 return Civi::$statics[__CLASS__]['fieldKeys'];
213 {rdelim}
214
215 /**
216 * Returns the names of this table
217 *
218 * @return string
219 */
220 static function getTableName( ) {ldelim}
221 {if $table.localizable}
222 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
223 {else}
224 return self::$_tableName;
225 {/if}
226 {rdelim}
227
228 /**
229 * Returns if this table needs to be logged
230 *
231 * @return boolean
232 */
233 function getLog( ) {ldelim}
234 return self::$_log;
235 {rdelim}
236
237 /**
238 * Returns the list of fields that can be imported
239 *
240 * @param bool $prefix
241 *
242 * @return array
243 */
244 static function &import( $prefix = false ) {ldelim}
245 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
246 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
247 {if $foreign.import}'{$foreign.className}',{/if}
248 {/foreach}{/if}
249 ));
250 return $r;
251 {rdelim}
252
253 /**
254 * Returns the list of fields that can be exported
255 *
256 * @param bool $prefix
257 *
258 * @return array
259 */
260 static function &export( $prefix = false ) {ldelim}
261 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
262 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
263 {if $foreign.export}'{$foreign.className}',{/if}
264 {/foreach}{/if}
265 ));
266 return $r;
267 {rdelim}
268
269 /**
270 * Returns the list of indices
271 */
272 public static function indices($localize = TRUE) {ldelim}
273 $indices = {$indicesPhp};
274 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
275 {rdelim}
276 {rdelim}