Afform - Predefined container styles
[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}
fc846b72
BT
75 * @var {$field.phpType}{if $field.phpNullable}|null
76{/if}
77
78 * (SQL type: {$field.sqlType})
79 * Note that values will be retrieved from the database as a string.
6a488035
TO
80 */
81 public ${$field.name};
82
83{/foreach} {* table.fields *}
84
85 /**
f41f0342 86 * Class constructor.
6a488035 87 */
c3fc2621 88 public function __construct( ) {ldelim}
6a488035
TO
89 $this->__table = '{$table.name}';
90
91 parent::__construct( );
92 {rdelim}
93
449c4e6b
CW
94 /**
95 * Returns localized title of this entity.
7b66c3b5
AH
96 *
97 * @param bool $plural
98 * Whether to return the plural version of the title.
449c4e6b 99 */
7b66c3b5
AH
100 public static function getEntityTitle($plural = FALSE) {ldelim}
101 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
449c4e6b
CW
102 {rdelim}
103
2562d09a
CW
104{if !empty($table.description)}
105 /**
106 * Returns user-friendly description of this entity.
107 *
108 * @return string
109 */
110 public static function getEntityDescription() {ldelim}
111 return {$tsFunctionName}('{$table.description|replace:"'":"\'"}');
112 {rdelim}
113{/if}
449c4e6b
CW
114
115
1832ba99 116{if !empty($table.foreignKey) || !empty($table.dynamicForeignKey)}
6a488035 117 /**
f41f0342 118 * Returns foreign keys and entity references.
6a488035 119 *
b6e60008
CW
120 * @return array
121 * [CRM_Core_Reference_Interface]
6a488035 122 */
c3fc2621 123 public static function getReferenceColumns() {ldelim}
740dd877
TO
124 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
125 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
1832ba99 126{if isset($table.foreignKey)}
6a488035 127{foreach from=$table.foreignKey item=foreign}
740dd877 128 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
71e5aa5c 129{/foreach}
1832ba99
SL
130{/if}
131{if isset($table.dynamicForeignKey)}
71e5aa5c 132{foreach from=$table.dynamicForeignKey item=foreign}
740dd877 133 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
6a488035 134{/foreach}
1832ba99 135{/if}
740dd877 136 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
71e5aa5c 137 {rdelim}
740dd877 138 return Civi::$statics[__CLASS__]['links'];
6a488035
TO
139 {rdelim}
140{/if} {* table.foreignKey *}
141
142 /**
b6e60008 143 * Returns all the column names of this table
6a488035 144 *
6a488035
TO
145 * @return array
146 */
c3fc2621 147 public static function &fields( ) {ldelim}
740dd877 148 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
c3fc2621 149 Civi::$statics[__CLASS__]['fields'] = array(
6a488035
TO
150{foreach from=$table.fields item=field}
151
152{if $field.uniqueName}
3cef3c00 153 '{$field.uniqueName}'
6a488035
TO
154{else}
155 '{$field.name}'
156{/if}
157 => array(
3cef3c00 158 'name' => '{$field.name}',
6a488035
TO
159 'type' => {$field.crmType},
160{if $field.title}
8af97d90 161 'title' => {$tsFunctionName}('{$field.title}'),
6a488035 162{/if}
5c7169eb 163{if $field.comment}
8af97d90 164 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
5c7169eb 165{/if}
6a488035 166{if $field.required}
c3fc2621 167 'required' => {$field.required|strtoupper},
6a488035 168{/if} {* field.required *}
1832ba99 169{if isset($field.length)}
6a488035
TO
170 'maxlength' => {$field.length},
171{/if} {* field.length *}
1832ba99 172{if isset($field.precision)}
ac5f2ccd
TO
173 'precision' => array({$field.precision}),
174{/if}
1832ba99 175{if isset($field.size)}
6a488035
TO
176 'size' => {$field.size},
177{/if} {* field.size *}
1832ba99 178{if isset($field.rows)}
6a488035
TO
179 'rows' => {$field.rows},
180{/if} {* field.rows *}
1832ba99 181{if isset($field.cols)}
6a488035
TO
182 'cols' => {$field.cols},
183{/if} {* field.cols *}
184
185{if $field.import}
c3fc2621 186 'import' => {$field.import|strtoupper},
bf144e74 187
6a488035 188{/if} {* field.import *}
bf144e74 189 'where' => '{$table.name}.{$field.name}',
190 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
191 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
6a488035 192{if $field.export}
c3fc2621 193 'export' => {$field.export|strtoupper},
6a488035 194{/if} {* field.export *}
f5c0f096
CW
195{if $field.contactType}
196 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
197{/if}
6a488035
TO
198{if $field.rule}
199 'rule' => '{$field.rule}',
200{/if} {* field.rule *}
1713a0ec
CW
201{if !empty($field.permission)}
202 'permission' => {$field.permission|@print_array},
042043f1 203{/if}
45a83e42 204{if $field.default || $field.default === '0'}
5fb0de1f 205 {capture assign=unquotedDefault}{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}{/capture}
206 'default' => {if ($unquotedDefault==='NULL')}NULL{else}'{$unquotedDefault}'{/if},
6a488035 207{/if} {* field.default *}
3cef3c00 208 'table_name' => '{$table.name}',
209 'entity' => '{$table.entity}',
210 'bao' => '{$table.bao}',
fbdb9a4d 211 'localizable' => {if $field.localizable}1{else}0{/if},
1832ba99 212 {if isset($field.localize_context)}'localize_context' => '{$field.localize_context}',{/if}
6a488035 213
1832ba99 214{if isset($field.FKClassName)}
6a488035 215 'FKClassName' => '{$field.FKClassName}',
2a5c9b4d 216{/if}
88c31c05
CW
217{if !empty($field.component)}
218 'component' => '{$field.component}',
219{/if}
2a5c9b4d
CW
220{if $field.serialize}
221 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
222{/if}
74db51d3 223{if $field.uniqueTitle}
224 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
225{/if}
5e545f38 226{if $field.html}
4361de53
CW
227 'html' => array(
228 {foreach from=$field.html item=val key=key}
8af97d90 229 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
4361de53
CW
230 {/foreach}
231 ),
232{/if}
6a488035 233{if $field.pseudoconstant}
800d9240 234 'pseudoconstant' => {$field.pseudoconstant|@print_array},
34745448
CW
235{/if}
236{if $field.readonly || $field.name === $table.primaryKey.name}
237 'readonly' => TRUE,
a9d0587b
CW
238{/if}
239 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
240),
6a488035
TO
241{/foreach} {* table.fields *}
242 );
740dd877 243 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
6a488035 244 {rdelim}
740dd877 245 return Civi::$statics[__CLASS__]['fields'];
6a488035
TO
246 {rdelim}
247
66abbb6d 248 /**
787b31c7 249 * Return a mapping from field-name to the corresponding key (as used in fields()).
66abbb6d 250 *
66abbb6d 251 * @return array
787b31c7 252 * Array(string $name => string $uniqueName).
66abbb6d 253 */
c3fc2621 254 public static function &fieldKeys( ) {ldelim}
787b31c7
TO
255 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
256 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
257 {rdelim}
258 return Civi::$statics[__CLASS__]['fieldKeys'];
66abbb6d
AS
259 {rdelim}
260
6a488035 261 /**
b6e60008 262 * Returns the names of this table
6a488035 263 *
6a488035
TO
264 * @return string
265 */
c3fc2621 266 public static function getTableName( ) {ldelim}
6a488035
TO
267 {if $table.localizable}
268 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
269 {else}
270 return self::$_tableName;
271 {/if}
272 {rdelim}
273
274 /**
b6e60008 275 * Returns if this table needs to be logged
6a488035 276 *
c3fc2621 277 * @return bool
6a488035 278 */
c3fc2621 279 public function getLog( ) {ldelim}
6a488035
TO
280 return self::$_log;
281 {rdelim}
282
283 /**
b6e60008
CW
284 * Returns the list of fields that can be imported
285 *
286 * @param bool $prefix
6a488035 287 *
b6e60008 288 * @return array
6a488035 289 */
c3fc2621 290 public static function &import( $prefix = FALSE ) {ldelim}
84a0493c 291 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
1832ba99 292 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
84a0493c
TO
293 {if $foreign.import}'{$foreign.className}',{/if}
294 {/foreach}{/if}
295 ));
296 return $r;
6a488035
TO
297 {rdelim}
298
299 /**
b6e60008
CW
300 * Returns the list of fields that can be exported
301 *
302 * @param bool $prefix
303 *
304 * @return array
305 */
c3fc2621 306 public static function &export( $prefix = FALSE ) {ldelim}
84a0493c 307 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
1832ba99 308 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
84a0493c
TO
309 {if $foreign.export}'{$foreign.className}',{/if}
310 {/foreach}{/if}
311 ));
312 return $r;
6a488035
TO
313 {rdelim}
314
6b86d84f
AS
315 /**
316 * Returns the list of indices
c3fc2621
CW
317 *
318 * @param bool $localize
319 *
320 * @return array
6b86d84f
AS
321 */
322 public static function indices($localize = TRUE) {ldelim}
323 $indices = {$indicesPhp};
324 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
325 {rdelim}
c3fc2621 326
6a488035 327{rdelim}