Merge pull request #20308 from masetto/master
[civicrm-core.git] / xml / templates / dao.tpl
1 <?php
2 /**
3 * @package CRM
4 * @copyright CiviCRM LLC https://civicrm.org/licensing
5 *
6 * Generated from {$table.sourceFile}
7 * {$generated}
8 * (GenCodeChecksum:{$genCodeChecksum})
9 */
10 {if isset($useHelper)}{$useHelper}{/if}
11 /**
12 * Database access object for the {$table.entity} entity.
13 */
14 class {$table.className} extends CRM_Core_DAO {ldelim}
15
16 const EXT = {$ext};
17 const TABLE_ADDED = '{$table.add}';
18 {if !empty($table.component)}const COMPONENT = '{$table.component}';{/if}
19
20 /**
21 * Static instance to hold the table name.
22 *
23 * @var string
24 */
25 public static $_tableName = '{$table.name}';
26
27 {* Only print this variable if it's different than the default in CRM_Core_DAO *}
28 {if count($table.primaryKey.field) !== 1 || $table.primaryKey.field.0 !== 'id'}
29 /**
30 * Primary key field(s).
31 *
32 * @var string[]
33 */
34 public static $_primaryKey = [{if $table.primaryKey.field}'{"', '"|implode:$table.primaryKey.field}'{/if}];
35 {/if}
36
37 {if $table.icon}
38 /**
39 * Icon associated with this entity.
40 *
41 * @var string
42 */
43 public static $_icon = '{$table.icon}';
44 {/if}
45
46 {if $table.labelField}
47 /**
48 * Field to show when displaying a record.
49 *
50 * @var string
51 */
52 public static $_labelField = '{$table.labelField}';
53 {/if}
54 /**
55 * Should CiviCRM log any modifications to this table in the civicrm_log table.
56 *
57 * @var bool
58 */
59 public static $_log = {$table.log|strtoupper};
60 {if $table.paths}
61 /**
62 * Paths for accessing this entity in the UI.
63 *
64 * @var string[]
65 */
66 protected static $_paths = {$table.paths|@print_array};
67 {/if}
68
69 {foreach from=$table.fields item=field}
70 /**
71 {if $field.comment}
72 * {$field.comment|regex_replace:"/\n[ ]*/":"\n* "}
73 *
74 {/if}
75 * @var {$field.phpType}
76 */
77 public ${$field.name};
78
79 {/foreach} {* table.fields *}
80
81 /**
82 * Class constructor.
83 */
84 public function __construct( ) {ldelim}
85 $this->__table = '{$table.name}';
86
87 parent::__construct( );
88 {rdelim}
89
90 /**
91 * Returns localized title of this entity.
92 *
93 * @param bool $plural
94 * Whether to return the plural version of the title.
95 */
96 public static function getEntityTitle($plural = FALSE) {ldelim}
97 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
98 {rdelim}
99
100 {if !empty($table.description)}
101 /**
102 * Returns user-friendly description of this entity.
103 *
104 * @return string
105 */
106 public static function getEntityDescription() {ldelim}
107 return {$tsFunctionName}('{$table.description|replace:"'":"\'"}');
108 {rdelim}
109 {/if}
110
111
112 {if !empty($table.foreignKey) || !empty($table.dynamicForeignKey)}
113 /**
114 * Returns foreign keys and entity references.
115 *
116 * @return array
117 * [CRM_Core_Reference_Interface]
118 */
119 public static function getReferenceColumns() {ldelim}
120 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
121 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
122 {if isset($table.foreignKey)}
123 {foreach from=$table.foreignKey item=foreign}
124 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
125 {/foreach}
126 {/if}
127 {if isset($table.dynamicForeignKey)}
128 {foreach from=$table.dynamicForeignKey item=foreign}
129 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
130 {/foreach}
131 {/if}
132 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
133 {rdelim}
134 return Civi::$statics[__CLASS__]['links'];
135 {rdelim}
136 {/if} {* table.foreignKey *}
137
138 /**
139 * Returns all the column names of this table
140 *
141 * @return array
142 */
143 public static function &fields( ) {ldelim}
144 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
145 Civi::$statics[__CLASS__]['fields'] = array(
146 {foreach from=$table.fields item=field}
147
148 {if $field.uniqueName}
149 '{$field.uniqueName}'
150 {else}
151 '{$field.name}'
152 {/if}
153 => array(
154 'name' => '{$field.name}',
155 'type' => {$field.crmType},
156 {if $field.title}
157 'title' => {$tsFunctionName}('{$field.title}'),
158 {/if}
159 {if $field.comment}
160 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
161 {/if}
162 {if $field.required}
163 'required' => {$field.required|strtoupper},
164 {/if} {* field.required *}
165 {if isset($field.length)}
166 'maxlength' => {$field.length},
167 {/if} {* field.length *}
168 {if isset($field.precision)}
169 'precision' => array({$field.precision}),
170 {/if}
171 {if isset($field.size)}
172 'size' => {$field.size},
173 {/if} {* field.size *}
174 {if isset($field.rows)}
175 'rows' => {$field.rows},
176 {/if} {* field.rows *}
177 {if isset($field.cols)}
178 'cols' => {$field.cols},
179 {/if} {* field.cols *}
180
181 {if $field.import}
182 'import' => {$field.import|strtoupper},
183
184 {/if} {* field.import *}
185 'where' => '{$table.name}.{$field.name}',
186 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
187 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
188 {if $field.export}
189 'export' => {$field.export|strtoupper},
190 {/if} {* field.export *}
191 {if $field.contactType}
192 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
193 {/if}
194 {if $field.rule}
195 'rule' => '{$field.rule}',
196 {/if} {* field.rule *}
197 {if !empty($field.permission)}
198 'permission' => {$field.permission|@print_array},
199 {/if}
200 {if $field.default || $field.default === '0'}
201 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
202 {/if} {* field.default *}
203 'table_name' => '{$table.name}',
204 'entity' => '{$table.entity}',
205 'bao' => '{$table.bao}',
206 'localizable' => {if $field.localizable}1{else}0{/if},
207 {if isset($field.localize_context)}'localize_context' => '{$field.localize_context}',{/if}
208
209 {if isset($field.FKClassName)}
210 'FKClassName' => '{$field.FKClassName}',
211 {/if}
212 {if !empty($field.component)}
213 'component' => '{$field.component}',
214 {/if}
215 {if $field.serialize}
216 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
217 {/if}
218 {if $field.uniqueTitle}
219 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
220 {/if}
221 {if $field.html}
222 'html' => array(
223 {foreach from=$field.html item=val key=key}
224 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
225 {/foreach}
226 ),
227 {/if}
228 {if $field.pseudoconstant}
229 'pseudoconstant' => {$field.pseudoconstant|@print_array},
230 {/if}
231 {if $field.readonly || $field.name === $table.primaryKey.name}
232 'readonly' => TRUE,
233 {/if}
234 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
235 ),
236 {/foreach} {* table.fields *}
237 );
238 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
239 {rdelim}
240 return Civi::$statics[__CLASS__]['fields'];
241 {rdelim}
242
243 /**
244 * Return a mapping from field-name to the corresponding key (as used in fields()).
245 *
246 * @return array
247 * Array(string $name => string $uniqueName).
248 */
249 public static function &fieldKeys( ) {ldelim}
250 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
251 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
252 {rdelim}
253 return Civi::$statics[__CLASS__]['fieldKeys'];
254 {rdelim}
255
256 /**
257 * Returns the names of this table
258 *
259 * @return string
260 */
261 public static function getTableName( ) {ldelim}
262 {if $table.localizable}
263 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
264 {else}
265 return self::$_tableName;
266 {/if}
267 {rdelim}
268
269 /**
270 * Returns if this table needs to be logged
271 *
272 * @return bool
273 */
274 public function getLog( ) {ldelim}
275 return self::$_log;
276 {rdelim}
277
278 /**
279 * Returns the list of fields that can be imported
280 *
281 * @param bool $prefix
282 *
283 * @return array
284 */
285 public static function &import( $prefix = FALSE ) {ldelim}
286 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
287 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
288 {if $foreign.import}'{$foreign.className}',{/if}
289 {/foreach}{/if}
290 ));
291 return $r;
292 {rdelim}
293
294 /**
295 * Returns the list of fields that can be exported
296 *
297 * @param bool $prefix
298 *
299 * @return array
300 */
301 public static function &export( $prefix = FALSE ) {ldelim}
302 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
303 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
304 {if $foreign.export}'{$foreign.className}',{/if}
305 {/foreach}{/if}
306 ));
307 return $r;
308 {rdelim}
309
310 /**
311 * Returns the list of indices
312 *
313 * @param bool $localize
314 *
315 * @return array
316 */
317 public static function indices($localize = TRUE) {ldelim}
318 $indices = {$indicesPhp};
319 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
320 {rdelim}
321
322 {rdelim}