Merge pull request #18455 from civicrm/5.30
[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 {$useHelper}
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
19 /**
20 * Static instance to hold the table name.
21 *
22 * @var string
23 */
24 public static $_tableName = '{$table.name}';
25
26 {if $table.icon}
27 /**
28 * Icon associated with this entity.
29 *
30 * @var string
31 */
32 public static $_icon = '{$table.icon}';
33 {/if}
34 /**
35 * Should CiviCRM log any modifications to this table in the civicrm_log table.
36 *
37 * @var bool
38 */
39 public static $_log = {$table.log|strtoupper};
40
41 {foreach from=$table.fields item=field}
42 /**
43 {if $field.comment}
44 * {$field.comment|regex_replace:"/\n[ ]*/":"\n* "}
45 *
46 {/if}
47 * @var {$field.phpType}
48 */
49 public ${$field.name};
50
51 {/foreach} {* table.fields *}
52
53 /**
54 * Class constructor.
55 */
56 public function __construct( ) {ldelim}
57 $this->__table = '{$table.name}';
58
59 parent::__construct( );
60 {rdelim}
61
62 /**
63 * Returns localized title of this entity.
64 *
65 * @param bool $plural
66 * Whether to return the plural version of the title.
67 */
68 public static function getEntityTitle($plural = FALSE) {ldelim}
69 return $plural ? {$tsFunctionName}('{$table.titlePlural}') : {$tsFunctionName}('{$table.title}');
70 {rdelim}
71
72
73
74 {if $table.foreignKey || $table.dynamicForeignKey}
75 /**
76 * Returns foreign keys and entity references.
77 *
78 * @return array
79 * [CRM_Core_Reference_Interface]
80 */
81 public static function getReferenceColumns() {ldelim}
82 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
83 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
84 {foreach from=$table.foreignKey item=foreign}
85 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
86 {/foreach}
87
88 {foreach from=$table.dynamicForeignKey item=foreign}
89 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
90 {/foreach}
91 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
92 {rdelim}
93 return Civi::$statics[__CLASS__]['links'];
94 {rdelim}
95 {/if} {* table.foreignKey *}
96
97 /**
98 * Returns all the column names of this table
99 *
100 * @return array
101 */
102 public static function &fields( ) {ldelim}
103 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
104 Civi::$statics[__CLASS__]['fields'] = array(
105 {foreach from=$table.fields item=field}
106
107 {if $field.uniqueName}
108 '{$field.uniqueName}'
109 {else}
110 '{$field.name}'
111 {/if}
112 => array(
113 'name' => '{$field.name}',
114 'type' => {$field.crmType},
115 {if $field.title}
116 'title' => {$tsFunctionName}('{$field.title}'),
117 {/if}
118 {if $field.comment}
119 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
120 {/if}
121 {if $field.required}
122 'required' => {$field.required|strtoupper},
123 {/if} {* field.required *}
124 {if $field.length}
125 'maxlength' => {$field.length},
126 {/if} {* field.length *}
127 {if $field.precision}
128 'precision' => array({$field.precision}),
129 {/if}
130 {if $field.size}
131 'size' => {$field.size},
132 {/if} {* field.size *}
133 {if $field.rows}
134 'rows' => {$field.rows},
135 {/if} {* field.rows *}
136 {if $field.cols}
137 'cols' => {$field.cols},
138 {/if} {* field.cols *}
139
140 {if $field.import}
141 'import' => {$field.import|strtoupper},
142
143 {/if} {* field.import *}
144 'where' => '{$table.name}.{$field.name}',
145 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
146 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
147 {if $field.export}
148 'export' => {$field.export|strtoupper},
149 {/if} {* field.export *}
150 {if $field.contactType}
151 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
152 {/if}
153 {if $field.rule}
154 'rule' => '{$field.rule}',
155 {/if} {* field.rule *}
156 {if !empty($field.permission)}
157 'permission' => {$field.permission|@print_array},
158 {/if}
159 {if $field.default || $field.default === '0'}
160 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
161 {/if} {* field.default *}
162 'table_name' => '{$table.name}',
163 'entity' => '{$table.entity}',
164 'bao' => '{$table.bao}',
165 'localizable' => {if $field.localizable}1{else}0{/if},
166 {if $field.localize_context}'localize_context' => '{$field.localize_context}',{/if}
167
168 {if $field.FKClassName}
169 'FKClassName' => '{$field.FKClassName}',
170 {/if}
171 {if $field.serialize}
172 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
173 {/if}
174 {if $field.uniqueTitle}
175 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
176 {/if}
177 {if $field.html}
178 'html' => array(
179 {foreach from=$field.html item=val key=key}
180 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
181 {/foreach}
182 ),
183 {/if}
184 {if $field.pseudoconstant}
185 'pseudoconstant' => {$field.pseudoconstant|@print_array},
186 {/if}
187 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
188 ),
189 {/foreach} {* table.fields *}
190 );
191 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
192 {rdelim}
193 return Civi::$statics[__CLASS__]['fields'];
194 {rdelim}
195
196 /**
197 * Return a mapping from field-name to the corresponding key (as used in fields()).
198 *
199 * @return array
200 * Array(string $name => string $uniqueName).
201 */
202 public static function &fieldKeys( ) {ldelim}
203 if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
204 Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
205 {rdelim}
206 return Civi::$statics[__CLASS__]['fieldKeys'];
207 {rdelim}
208
209 /**
210 * Returns the names of this table
211 *
212 * @return string
213 */
214 public static function getTableName( ) {ldelim}
215 {if $table.localizable}
216 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
217 {else}
218 return self::$_tableName;
219 {/if}
220 {rdelim}
221
222 /**
223 * Returns if this table needs to be logged
224 *
225 * @return bool
226 */
227 public function getLog( ) {ldelim}
228 return self::$_log;
229 {rdelim}
230
231 /**
232 * Returns the list of fields that can be imported
233 *
234 * @param bool $prefix
235 *
236 * @return array
237 */
238 public static function &import( $prefix = FALSE ) {ldelim}
239 $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
240 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
241 {if $foreign.import}'{$foreign.className}',{/if}
242 {/foreach}{/if}
243 ));
244 return $r;
245 {rdelim}
246
247 /**
248 * Returns the list of fields that can be exported
249 *
250 * @param bool $prefix
251 *
252 * @return array
253 */
254 public static function &export( $prefix = FALSE ) {ldelim}
255 $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
256 {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
257 {if $foreign.export}'{$foreign.className}',{/if}
258 {/foreach}{/if}
259 ));
260 return $r;
261 {rdelim}
262
263 /**
264 * Returns the list of indices
265 *
266 * @param bool $localize
267 *
268 * @return array
269 */
270 public static function indices($localize = TRUE) {ldelim}
271 $indices = {$indicesPhp};
272 return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
273 {rdelim}
274
275 {rdelim}