Set version to 5.37.beta1
[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 */
fbb7b4d8 10{$useHelper}
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
449c4e6b
CW
27 {if $table.icon}
28 /**
29 * Icon associated with this entity.
30 *
31 * @var string
32 */
33 public static $_icon = '{$table.icon}';
34 {/if}
8ab43c93
CW
35
36 {if $table.labelField}
37 /**
38 * Field to show when displaying a record.
39 *
40 * @var string
41 */
42 public static $_labelField = '{$table.labelField}';
43 {/if}
6a488035 44 /**
f41f0342 45 * Should CiviCRM log any modifications to this table in the civicrm_log table.
6a488035 46 *
c3fc2621 47 * @var bool
6a488035 48 */
fa45b5b9 49 public static $_log = {$table.log|strtoupper};
a7bd99ff
CW
50 {if $table.paths}
51 /**
52 * Paths for accessing this entity in the UI.
53 *
54 * @var string[]
55 */
56 protected static $_paths = {$table.paths|@print_array};
57 {/if}
6a488035
TO
58
59{foreach from=$table.fields item=field}
60 /**
61{if $field.comment}
fb607354 62 * {$field.comment|regex_replace:"/\n[ ]*/":"\n* "}
6a488035 63 *
c3fc2621 64{/if}
6a488035
TO
65 * @var {$field.phpType}
66 */
67 public ${$field.name};
68
69{/foreach} {* table.fields *}
70
71 /**
f41f0342 72 * Class constructor.
6a488035 73 */
c3fc2621 74 public function __construct( ) {ldelim}
6a488035
TO
75 $this->__table = '{$table.name}';
76
77 parent::__construct( );
78 {rdelim}
79
449c4e6b
CW
80 /**
81 * Returns localized title of this entity.
7b66c3b5
AH
82 *
83 * @param bool $plural
84 * Whether to return the plural version of the title.
449c4e6b 85 */
7b66c3b5
AH
86 public static function getEntityTitle($plural = FALSE) {ldelim}
87 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
449c4e6b
CW
88 {rdelim}
89
90
91
71e5aa5c 92{if $table.foreignKey || $table.dynamicForeignKey}
6a488035 93 /**
f41f0342 94 * Returns foreign keys and entity references.
6a488035 95 *
b6e60008
CW
96 * @return array
97 * [CRM_Core_Reference_Interface]
6a488035 98 */
c3fc2621 99 public static function getReferenceColumns() {ldelim}
740dd877
TO
100 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
101 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
6a488035 102{foreach from=$table.foreignKey item=foreign}
740dd877 103 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
71e5aa5c
ARW
104{/foreach}
105
106{foreach from=$table.dynamicForeignKey item=foreign}
740dd877 107 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
6a488035 108{/foreach}
740dd877 109 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
71e5aa5c 110 {rdelim}
740dd877 111 return Civi::$statics[__CLASS__]['links'];
6a488035
TO
112 {rdelim}
113{/if} {* table.foreignKey *}
114
115 /**
b6e60008 116 * Returns all the column names of this table
6a488035 117 *
6a488035
TO
118 * @return array
119 */
c3fc2621 120 public static function &fields( ) {ldelim}
740dd877 121 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
c3fc2621 122 Civi::$statics[__CLASS__]['fields'] = array(
6a488035
TO
123{foreach from=$table.fields item=field}
124
125{if $field.uniqueName}
3cef3c00 126 '{$field.uniqueName}'
6a488035
TO
127{else}
128 '{$field.name}'
129{/if}
130 => array(
3cef3c00 131 'name' => '{$field.name}',
6a488035
TO
132 'type' => {$field.crmType},
133{if $field.title}
8af97d90 134 'title' => {$tsFunctionName}('{$field.title}'),
6a488035 135{/if}
5c7169eb 136{if $field.comment}
8af97d90 137 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
5c7169eb 138{/if}
6a488035 139{if $field.required}
c3fc2621 140 'required' => {$field.required|strtoupper},
6a488035
TO
141{/if} {* field.required *}
142{if $field.length}
143 'maxlength' => {$field.length},
144{/if} {* field.length *}
ac5f2ccd
TO
145{if $field.precision}
146 'precision' => array({$field.precision}),
147{/if}
6a488035
TO
148{if $field.size}
149 'size' => {$field.size},
150{/if} {* field.size *}
151{if $field.rows}
152 'rows' => {$field.rows},
153{/if} {* field.rows *}
154{if $field.cols}
155 'cols' => {$field.cols},
156{/if} {* field.cols *}
157
158{if $field.import}
c3fc2621 159 'import' => {$field.import|strtoupper},
bf144e74 160
6a488035 161{/if} {* field.import *}
bf144e74 162 'where' => '{$table.name}.{$field.name}',
163 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
164 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
6a488035 165{if $field.export}
c3fc2621 166 'export' => {$field.export|strtoupper},
6a488035 167{/if} {* field.export *}
f5c0f096
CW
168{if $field.contactType}
169 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
170{/if}
6a488035
TO
171{if $field.rule}
172 'rule' => '{$field.rule}',
173{/if} {* field.rule *}
1713a0ec
CW
174{if !empty($field.permission)}
175 'permission' => {$field.permission|@print_array},
042043f1 176{/if}
45a83e42 177{if $field.default || $field.default === '0'}
9e1ed301 178 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
6a488035 179{/if} {* field.default *}
3cef3c00 180 'table_name' => '{$table.name}',
181 'entity' => '{$table.entity}',
182 'bao' => '{$table.bao}',
fbdb9a4d 183 'localizable' => {if $field.localizable}1{else}0{/if},
65c86f7d 184 {if $field.localize_context}'localize_context' => '{$field.localize_context}',{/if}
6a488035
TO
185
186{if $field.FKClassName}
187 'FKClassName' => '{$field.FKClassName}',
2a5c9b4d
CW
188{/if}
189{if $field.serialize}
190 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
191{/if}
74db51d3 192{if $field.uniqueTitle}
193 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
194{/if}
5e545f38 195{if $field.html}
4361de53
CW
196 'html' => array(
197 {foreach from=$field.html item=val key=key}
8af97d90 198 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
4361de53
CW
199 {/foreach}
200 ),
201{/if}
6a488035 202{if $field.pseudoconstant}
800d9240 203 'pseudoconstant' => {$field.pseudoconstant|@print_array},
34745448
CW
204{/if}
205{if $field.readonly || $field.name === $table.primaryKey.name}
206 'readonly' => TRUE,
a9d0587b
CW
207{/if}
208 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
209),
6a488035
TO
210{/foreach} {* table.fields *}
211 );
740dd877 212 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
6a488035 213 {rdelim}
740dd877 214 return Civi::$statics[__CLASS__]['fields'];
6a488035
TO
215 {rdelim}
216
66abbb6d 217 /**
787b31c7 218 * Return a mapping from field-name to the corresponding key (as used in fields()).
66abbb6d 219 *
66abbb6d 220 * @return array
787b31c7 221 * Array(string $name => string $uniqueName).
66abbb6d 222 */
c3fc2621 223 public static function &fieldKeys( ) {ldelim}
787b31c7
TO
224 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
225 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
226 {rdelim}
227 return Civi::$statics[__CLASS__]['fieldKeys'];
66abbb6d
AS
228 {rdelim}
229
6a488035 230 /**
b6e60008 231 * Returns the names of this table
6a488035 232 *
6a488035
TO
233 * @return string
234 */
c3fc2621 235 public static function getTableName( ) {ldelim}
6a488035
TO
236 {if $table.localizable}
237 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
238 {else}
239 return self::$_tableName;
240 {/if}
241 {rdelim}
242
243 /**
b6e60008 244 * Returns if this table needs to be logged
6a488035 245 *
c3fc2621 246 * @return bool
6a488035 247 */
c3fc2621 248 public function getLog( ) {ldelim}
6a488035
TO
249 return self::$_log;
250 {rdelim}
251
252 /**
b6e60008
CW
253 * Returns the list of fields that can be imported
254 *
255 * @param bool $prefix
6a488035 256 *
b6e60008 257 * @return array
6a488035 258 */
c3fc2621 259 public static function &import( $prefix = FALSE ) {ldelim}
84a0493c
TO
260 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
261 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
262 {if $foreign.import}'{$foreign.className}',{/if}
263 {/foreach}{/if}
264 ));
265 return $r;
6a488035
TO
266 {rdelim}
267
268 /**
b6e60008
CW
269 * Returns the list of fields that can be exported
270 *
271 * @param bool $prefix
272 *
273 * @return array
274 */
c3fc2621 275 public static function &export( $prefix = FALSE ) {ldelim}
84a0493c
TO
276 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
277 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
278 {if $foreign.export}'{$foreign.className}',{/if}
279 {/foreach}{/if}
280 ));
281 return $r;
6a488035
TO
282 {rdelim}
283
6b86d84f
AS
284 /**
285 * Returns the list of indices
c3fc2621
CW
286 *
287 * @param bool $localize
288 *
289 * @return array
6b86d84f
AS
290 */
291 public static function indices($localize = TRUE) {ldelim}
292 $indices = {$indicesPhp};
293 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
294 {rdelim}
c3fc2621 295
6a488035 296{rdelim}