Merge pull request #21323 from civicrm/5.41
[civicrm-core.git] / xml / templates / dao.tpl
CommitLineData
6a488035 1<?php
6a488035 2/**
6a488035 3 * @package CRM
ca5cec67 4 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 5 *
d5864876
ARW
6 * Generated from {$table.sourceFile}
7 * {$generated}
a8950859 8 * (GenCodeChecksum:{$genCodeChecksum})
6a488035 9 */
513ae7e3 10{if isset($useHelper)}{$useHelper}{/if}
f41f0342 11/**
c3fc2621 12 * Database access object for the {$table.entity} entity.
f41f0342 13 */
6a488035
TO
14class {$table.className} extends CRM_Core_DAO {ldelim}
15
4b4cf875
CW
16 const EXT = {$ext};
17 const TABLE_ADDED = '{$table.add}';
d31fb4e3 18 {if !empty($table.component)}const COMPONENT = '{$table.component}';{/if}
4b4cf875 19
6a488035 20 /**
f41f0342 21 * Static instance to hold the table name.
6a488035
TO
22 *
23 * @var string
6a488035 24 */
fa45b5b9 25 public static $_tableName = '{$table.name}';
6a488035 26
eda5da74
CW
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
449c4e6b
CW
37 {if $table.icon}
38 /**
39 * Icon associated with this entity.
40 *
41 * @var string
42 */
43 public static $_icon = '{$table.icon}';
44 {/if}
8ab43c93
CW
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}
6a488035 54 /**
f41f0342 55 * Should CiviCRM log any modifications to this table in the civicrm_log table.
6a488035 56 *
c3fc2621 57 * @var bool
6a488035 58 */
fa45b5b9 59 public static $_log = {$table.log|strtoupper};
a7bd99ff
CW
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}
6a488035
TO
68
69{foreach from=$table.fields item=field}
70 /**
71{if $field.comment}
fb607354 72 * {$field.comment|regex_replace:"/\n[ ]*/":"\n* "}
6a488035 73 *
c3fc2621 74{/if}
6a488035
TO
75 * @var {$field.phpType}
76 */
77 public ${$field.name};
78
79{/foreach} {* table.fields *}
80
81 /**
f41f0342 82 * Class constructor.
6a488035 83 */
c3fc2621 84 public function __construct( ) {ldelim}
6a488035
TO
85 $this->__table = '{$table.name}';
86
87 parent::__construct( );
88 {rdelim}
89
449c4e6b
CW
90 /**
91 * Returns localized title of this entity.
7b66c3b5
AH
92 *
93 * @param bool $plural
94 * Whether to return the plural version of the title.
449c4e6b 95 */
7b66c3b5
AH
96 public static function getEntityTitle($plural = FALSE) {ldelim}
97 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
449c4e6b
CW
98 {rdelim}
99
2562d09a
CW
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}
449c4e6b
CW
110
111
1832ba99 112{if !empty($table.foreignKey) || !empty($table.dynamicForeignKey)}
6a488035 113 /**
f41f0342 114 * Returns foreign keys and entity references.
6a488035 115 *
b6e60008
CW
116 * @return array
117 * [CRM_Core_Reference_Interface]
6a488035 118 */
c3fc2621 119 public static function getReferenceColumns() {ldelim}
740dd877
TO
120 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
121 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
1832ba99 122{if isset($table.foreignKey)}
6a488035 123{foreach from=$table.foreignKey item=foreign}
740dd877 124 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
71e5aa5c 125{/foreach}
1832ba99
SL
126{/if}
127{if isset($table.dynamicForeignKey)}
71e5aa5c 128{foreach from=$table.dynamicForeignKey item=foreign}
740dd877 129 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
6a488035 130{/foreach}
1832ba99 131{/if}
740dd877 132 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
71e5aa5c 133 {rdelim}
740dd877 134 return Civi::$statics[__CLASS__]['links'];
6a488035
TO
135 {rdelim}
136{/if} {* table.foreignKey *}
137
138 /**
b6e60008 139 * Returns all the column names of this table
6a488035 140 *
6a488035
TO
141 * @return array
142 */
c3fc2621 143 public static function &fields( ) {ldelim}
740dd877 144 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
c3fc2621 145 Civi::$statics[__CLASS__]['fields'] = array(
6a488035
TO
146{foreach from=$table.fields item=field}
147
148{if $field.uniqueName}
3cef3c00 149 '{$field.uniqueName}'
6a488035
TO
150{else}
151 '{$field.name}'
152{/if}
153 => array(
3cef3c00 154 'name' => '{$field.name}',
6a488035
TO
155 'type' => {$field.crmType},
156{if $field.title}
8af97d90 157 'title' => {$tsFunctionName}('{$field.title}'),
6a488035 158{/if}
5c7169eb 159{if $field.comment}
8af97d90 160 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
5c7169eb 161{/if}
6a488035 162{if $field.required}
c3fc2621 163 'required' => {$field.required|strtoupper},
6a488035 164{/if} {* field.required *}
1832ba99 165{if isset($field.length)}
6a488035
TO
166 'maxlength' => {$field.length},
167{/if} {* field.length *}
1832ba99 168{if isset($field.precision)}
ac5f2ccd
TO
169 'precision' => array({$field.precision}),
170{/if}
1832ba99 171{if isset($field.size)}
6a488035
TO
172 'size' => {$field.size},
173{/if} {* field.size *}
1832ba99 174{if isset($field.rows)}
6a488035
TO
175 'rows' => {$field.rows},
176{/if} {* field.rows *}
1832ba99 177{if isset($field.cols)}
6a488035
TO
178 'cols' => {$field.cols},
179{/if} {* field.cols *}
180
181{if $field.import}
c3fc2621 182 'import' => {$field.import|strtoupper},
bf144e74 183
6a488035 184{/if} {* field.import *}
bf144e74 185 'where' => '{$table.name}.{$field.name}',
186 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
187 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
6a488035 188{if $field.export}
c3fc2621 189 'export' => {$field.export|strtoupper},
6a488035 190{/if} {* field.export *}
f5c0f096
CW
191{if $field.contactType}
192 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
193{/if}
6a488035
TO
194{if $field.rule}
195 'rule' => '{$field.rule}',
196{/if} {* field.rule *}
1713a0ec
CW
197{if !empty($field.permission)}
198 'permission' => {$field.permission|@print_array},
042043f1 199{/if}
45a83e42 200{if $field.default || $field.default === '0'}
9e1ed301 201 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
6a488035 202{/if} {* field.default *}
3cef3c00 203 'table_name' => '{$table.name}',
204 'entity' => '{$table.entity}',
205 'bao' => '{$table.bao}',
fbdb9a4d 206 'localizable' => {if $field.localizable}1{else}0{/if},
1832ba99 207 {if isset($field.localize_context)}'localize_context' => '{$field.localize_context}',{/if}
6a488035 208
1832ba99 209{if isset($field.FKClassName)}
6a488035 210 'FKClassName' => '{$field.FKClassName}',
2a5c9b4d 211{/if}
88c31c05
CW
212{if !empty($field.component)}
213 'component' => '{$field.component}',
214{/if}
2a5c9b4d
CW
215{if $field.serialize}
216 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
217{/if}
74db51d3 218{if $field.uniqueTitle}
219 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
220{/if}
5e545f38 221{if $field.html}
4361de53
CW
222 'html' => array(
223 {foreach from=$field.html item=val key=key}
8af97d90 224 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
4361de53
CW
225 {/foreach}
226 ),
227{/if}
6a488035 228{if $field.pseudoconstant}
800d9240 229 'pseudoconstant' => {$field.pseudoconstant|@print_array},
34745448
CW
230{/if}
231{if $field.readonly || $field.name === $table.primaryKey.name}
232 'readonly' => TRUE,
a9d0587b
CW
233{/if}
234 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
235),
6a488035
TO
236{/foreach} {* table.fields *}
237 );
740dd877 238 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
6a488035 239 {rdelim}
740dd877 240 return Civi::$statics[__CLASS__]['fields'];
6a488035
TO
241 {rdelim}
242
66abbb6d 243 /**
787b31c7 244 * Return a mapping from field-name to the corresponding key (as used in fields()).
66abbb6d 245 *
66abbb6d 246 * @return array
787b31c7 247 * Array(string $name => string $uniqueName).
66abbb6d 248 */
c3fc2621 249 public static function &fieldKeys( ) {ldelim}
787b31c7
TO
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'];
66abbb6d
AS
254 {rdelim}
255
6a488035 256 /**
b6e60008 257 * Returns the names of this table
6a488035 258 *
6a488035
TO
259 * @return string
260 */
c3fc2621 261 public static function getTableName( ) {ldelim}
6a488035
TO
262 {if $table.localizable}
263 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
264 {else}
265 return self::$_tableName;
266 {/if}
267 {rdelim}
268
269 /**
b6e60008 270 * Returns if this table needs to be logged
6a488035 271 *
c3fc2621 272 * @return bool
6a488035 273 */
c3fc2621 274 public function getLog( ) {ldelim}
6a488035
TO
275 return self::$_log;
276 {rdelim}
277
278 /**
b6e60008
CW
279 * Returns the list of fields that can be imported
280 *
281 * @param bool $prefix
6a488035 282 *
b6e60008 283 * @return array
6a488035 284 */
c3fc2621 285 public static function &import( $prefix = FALSE ) {ldelim}
84a0493c 286 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
1832ba99 287 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
84a0493c
TO
288 {if $foreign.import}'{$foreign.className}',{/if}
289 {/foreach}{/if}
290 ));
291 return $r;
6a488035
TO
292 {rdelim}
293
294 /**
b6e60008
CW
295 * Returns the list of fields that can be exported
296 *
297 * @param bool $prefix
298 *
299 * @return array
300 */
c3fc2621 301 public static function &export( $prefix = FALSE ) {ldelim}
84a0493c 302 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
1832ba99 303 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
84a0493c
TO
304 {if $foreign.export}'{$foreign.className}',{/if}
305 {/foreach}{/if}
306 ));
307 return $r;
6a488035
TO
308 {rdelim}
309
6b86d84f
AS
310 /**
311 * Returns the list of indices
c3fc2621
CW
312 *
313 * @param bool $localize
314 *
315 * @return array
6b86d84f
AS
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}
c3fc2621 321
6a488035 322{rdelim}