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