Merge pull request #23062 from MegaphoneJon/fix-batch-url
[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}{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.
80 */
81 public ${$field.name};
82
83 {/foreach} {* table.fields *}
84
85 /**
86 * Class constructor.
87 */
88 public function __construct( ) {ldelim}
89 $this->__table = '{$table.name}';
90
91 parent::__construct( );
92 {rdelim}
93
94 /**
95 * Returns localized title of this entity.
96 *
97 * @param bool $plural
98 * Whether to return the plural version of the title.
99 */
100 public static function getEntityTitle($plural = FALSE) {ldelim}
101 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
102 {rdelim}
103
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}
114
115
116 {if !empty($table.foreignKey) || !empty($table.dynamicForeignKey)}
117 /**
118 * Returns foreign keys and entity references.
119 *
120 * @return array
121 * [CRM_Core_Reference_Interface]
122 */
123 public static function getReferenceColumns() {ldelim}
124 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
125 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
126 {if isset($table.foreignKey)}
127 {foreach from=$table.foreignKey item=foreign}
128 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
129 {/foreach}
130 {/if}
131 {if isset($table.dynamicForeignKey)}
132 {foreach from=$table.dynamicForeignKey item=foreign}
133 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
134 {/foreach}
135 {/if}
136 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
137 {rdelim}
138 return Civi::$statics[__CLASS__]['links'];
139 {rdelim}
140 {/if} {* table.foreignKey *}
141
142 /**
143 * Returns all the column names of this table
144 *
145 * @return array
146 */
147 public static function &fields( ) {ldelim}
148 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
149 Civi::$statics[__CLASS__]['fields'] = array(
150 {foreach from=$table.fields item=field}
151
152 {if $field.uniqueName}
153 '{$field.uniqueName}'
154 {else}
155 '{$field.name}'
156 {/if}
157 => array(
158 'name' => '{$field.name}',
159 'type' => {$field.crmType},
160 {if $field.title}
161 'title' => {$tsFunctionName}('{$field.title}'),
162 {/if}
163 {if $field.comment}
164 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
165 {/if}
166 {if $field.required}
167 'required' => {$field.required|strtoupper},
168 {/if} {* field.required *}
169 {if isset($field.length)}
170 'maxlength' => {$field.length},
171 {/if} {* field.length *}
172 {if isset($field.precision)}
173 'precision' => array({$field.precision}),
174 {/if}
175 {if isset($field.size)}
176 'size' => {$field.size},
177 {/if} {* field.size *}
178 {if isset($field.rows)}
179 'rows' => {$field.rows},
180 {/if} {* field.rows *}
181 {if isset($field.cols)}
182 'cols' => {$field.cols},
183 {/if} {* field.cols *}
184
185 {if $field.import}
186 'import' => {$field.import|strtoupper},
187
188 {/if} {* field.import *}
189 'where' => '{$table.name}.{$field.name}',
190 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
191 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
192 {if $field.export}
193 'export' => {$field.export|strtoupper},
194 {/if} {* field.export *}
195 {if $field.contactType}
196 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
197 {/if}
198 {if $field.rule}
199 'rule' => '{$field.rule}',
200 {/if} {* field.rule *}
201 {if !empty($field.permission)}
202 'permission' => {$field.permission|@print_array},
203 {/if}
204 {if $field.default || $field.default === '0'}
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},
207 {/if} {* field.default *}
208 'table_name' => '{$table.name}',
209 'entity' => '{$table.entity}',
210 'bao' => '{$table.bao}',
211 'localizable' => {if $field.localizable}1{else}0{/if},
212 {if isset($field.localize_context)}'localize_context' => '{$field.localize_context}',{/if}
213
214 {if isset($field.FKClassName)}
215 'FKClassName' => '{$field.FKClassName}',
216 {/if}
217 {if !empty($field.component)}
218 'component' => '{$field.component}',
219 {/if}
220 {if $field.serialize}
221 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
222 {/if}
223 {if $field.uniqueTitle}
224 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
225 {/if}
226 {if $field.html}
227 'html' => array(
228 {foreach from=$field.html item=val key=key}
229 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
230 {/foreach}
231 ),
232 {/if}
233 {if $field.pseudoconstant}
234 'pseudoconstant' => {$field.pseudoconstant|@print_array},
235 {/if}
236 {if $field.readonly || $field.name === $table.primaryKey.name}
237 'readonly' => TRUE,
238 {/if}
239 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
240 ),
241 {/foreach} {* table.fields *}
242 );
243 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
244 {rdelim}
245 return Civi::$statics[__CLASS__]['fields'];
246 {rdelim}
247
248 /**
249 * Return a mapping from field-name to the corresponding key (as used in fields()).
250 *
251 * @return array
252 * Array(string $name => string $uniqueName).
253 */
254 public static function &fieldKeys( ) {ldelim}
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'];
259 {rdelim}
260
261 /**
262 * Returns the names of this table
263 *
264 * @return string
265 */
266 public static function getTableName( ) {ldelim}
267 {if $table.localizable}
268 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
269 {else}
270 return self::$_tableName;
271 {/if}
272 {rdelim}
273
274 /**
275 * Returns if this table needs to be logged
276 *
277 * @return bool
278 */
279 public function getLog( ) {ldelim}
280 return self::$_log;
281 {rdelim}
282
283 /**
284 * Returns the list of fields that can be imported
285 *
286 * @param bool $prefix
287 *
288 * @return array
289 */
290 public static function &import( $prefix = FALSE ) {ldelim}
291 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
292 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
293 {if $foreign.import}'{$foreign.className}',{/if}
294 {/foreach}{/if}
295 ));
296 return $r;
297 {rdelim}
298
299 /**
300 * Returns the list of fields that can be exported
301 *
302 * @param bool $prefix
303 *
304 * @return array
305 */
306 public static function &export( $prefix = FALSE ) {ldelim}
307 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
308 {if isset($table.foreignKey)}{foreach from=$table.foreignKey item=foreign}
309 {if $foreign.export}'{$foreign.className}',{/if}
310 {/foreach}{/if}
311 ));
312 return $r;
313 {rdelim}
314
315 /**
316 * Returns the list of indices
317 *
318 * @param bool $localize
319 *
320 * @return array
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}
326
327 {rdelim}