distmaker - Include `mixin/*` files
[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 {capture assign=unquotedDefault}{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}{/capture}
202 'default' => {if ($unquotedDefault==='NULL')}NULL{else}'{$unquotedDefault}'{/if},
203 {/if} {* field.default *}
204 'table_name' => '{$table.name}',
205 'entity' => '{$table.entity}',
206 'bao' => '{$table.bao}',
207 'localizable' => {if $field.localizable}1{else}0{/if},
208 {if isset($field.localize_context)}'localize_context' => '{$field.localize_context}',{/if}
209
210 {if isset($field.FKClassName)}
211 'FKClassName' => '{$field.FKClassName}',
212 {/if}
213 {if !empty($field.component)}
214 'component' => '{$field.component}',
215 {/if}
216 {if $field.serialize}
217 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
218 {/if}
219 {if $field.uniqueTitle}
220 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
221 {/if}
222 {if $field.html}
223 'html' => array(
224 {foreach from=$field.html item=val key=key}
225 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
226 {/foreach}
227 ),
228 {/if}
229 {if $field.pseudoconstant}
230 'pseudoconstant' => {$field.pseudoconstant|@print_array},
231 {/if}
232 {if $field.readonly || $field.name === $table.primaryKey.name}
233 'readonly' => TRUE,
234 {/if}
235 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
236 ),
237 {/foreach} {* table.fields *}
238 );
239 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
240 {rdelim}
241 return Civi::$statics[__CLASS__]['fields'];
242 {rdelim}
243
244 /**
245 * Return a mapping from field-name to the corresponding key (as used in fields()).
246 *
247 * @return array
248 * Array(string $name => string $uniqueName).
249 */
250 public static function &fieldKeys( ) {ldelim}
251 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
252 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
253 {rdelim}
254 return Civi::$statics[__CLASS__]['fieldKeys'];
255 {rdelim}
256
257 /**
258 * Returns the names of this table
259 *
260 * @return string
261 */
262 public static function getTableName( ) {ldelim}
263 {if $table.localizable}
264 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
265 {else}
266 return self::$_tableName;
267 {/if}
268 {rdelim}
269
270 /**
271 * Returns if this table needs to be logged
272 *
273 * @return bool
274 */
275 public function getLog( ) {ldelim}
276 return self::$_log;
277 {rdelim}
278
279 /**
280 * Returns the list of fields that can be imported
281 *
282 * @param bool $prefix
283 *
284 * @return array
285 */
286 public static function &import( $prefix = FALSE ) {ldelim}
287 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
288 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
289 {if $foreign.import}'{$foreign.className}',{/if}
290 {/foreach}{/if}
291 ));
292 return $r;
293 {rdelim}
294
295 /**
296 * Returns the list of fields that can be exported
297 *
298 * @param bool $prefix
299 *
300 * @return array
301 */
302 public static function &export( $prefix = FALSE ) {ldelim}
303 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
304 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
305 {if $foreign.export}'{$foreign.className}',{/if}
306 {/foreach}{/if}
307 ));
308 return $r;
309 {rdelim}
310
311 /**
312 * Returns the list of indices
313 *
314 * @param bool $localize
315 *
316 * @return array
317 */
318 public static function indices($localize = TRUE) {ldelim}
319 $indices = {$indicesPhp};
320 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
321 {rdelim}
322
323 {rdelim}