Merge pull request #17305 from mlutfy/core1755
[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
TO
9 */
10
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
16 /**
f41f0342 17 * Static instance to hold the table name.
6a488035
TO
18 *
19 * @var string
6a488035 20 */
fa45b5b9 21 public static $_tableName = '{$table.name}';
6a488035 22
449c4e6b
CW
23 {if $table.icon}
24 /**
25 * Icon associated with this entity.
26 *
27 * @var string
28 */
29 public static $_icon = '{$table.icon}';
30 {/if}
6a488035 31 /**
f41f0342 32 * Should CiviCRM log any modifications to this table in the civicrm_log table.
6a488035 33 *
c3fc2621 34 * @var bool
6a488035 35 */
fa45b5b9 36 public static $_log = {$table.log|strtoupper};
6a488035
TO
37
38{foreach from=$table.fields item=field}
39 /**
40{if $field.comment}
fb607354 41 * {$field.comment|regex_replace:"/\n[ ]*/":"\n* "}
6a488035 42 *
c3fc2621 43{/if}
6a488035
TO
44 * @var {$field.phpType}
45 */
46 public ${$field.name};
47
48{/foreach} {* table.fields *}
49
50 /**
f41f0342 51 * Class constructor.
6a488035 52 */
c3fc2621 53 public function __construct( ) {ldelim}
6a488035
TO
54 $this->__table = '{$table.name}';
55
56 parent::__construct( );
57 {rdelim}
58
449c4e6b
CW
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
71e5aa5c 68{if $table.foreignKey || $table.dynamicForeignKey}
6a488035 69 /**
f41f0342 70 * Returns foreign keys and entity references.
6a488035 71 *
b6e60008
CW
72 * @return array
73 * [CRM_Core_Reference_Interface]
6a488035 74 */
c3fc2621 75 public static function getReferenceColumns() {ldelim}
740dd877
TO
76 if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
77 Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
6a488035 78{foreach from=$table.foreignKey item=foreign}
740dd877 79 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
71e5aa5c
ARW
80{/foreach}
81
82{foreach from=$table.dynamicForeignKey item=foreign}
740dd877 83 Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
6a488035 84{/foreach}
740dd877 85 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
71e5aa5c 86 {rdelim}
740dd877 87 return Civi::$statics[__CLASS__]['links'];
6a488035
TO
88 {rdelim}
89{/if} {* table.foreignKey *}
90
91 /**
b6e60008 92 * Returns all the column names of this table
6a488035 93 *
6a488035
TO
94 * @return array
95 */
c3fc2621 96 public static function &fields( ) {ldelim}
740dd877 97 if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
c3fc2621 98 Civi::$statics[__CLASS__]['fields'] = array(
6a488035
TO
99{foreach from=$table.fields item=field}
100
101{if $field.uniqueName}
3cef3c00 102 '{$field.uniqueName}'
6a488035
TO
103{else}
104 '{$field.name}'
105{/if}
106 => array(
3cef3c00 107 'name' => '{$field.name}',
6a488035
TO
108 'type' => {$field.crmType},
109{if $field.title}
8af97d90 110 'title' => {$tsFunctionName}('{$field.title}'),
6a488035 111{/if}
5c7169eb 112{if $field.comment}
8af97d90 113 'description' => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
5c7169eb 114{/if}
6a488035 115{if $field.required}
c3fc2621 116 'required' => {$field.required|strtoupper},
6a488035
TO
117{/if} {* field.required *}
118{if $field.length}
119 'maxlength' => {$field.length},
120{/if} {* field.length *}
ac5f2ccd
TO
121{if $field.precision}
122 'precision' => array({$field.precision}),
123{/if}
6a488035
TO
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}
c3fc2621 135 'import' => {$field.import|strtoupper},
bf144e74 136
6a488035 137{/if} {* field.import *}
bf144e74 138 'where' => '{$table.name}.{$field.name}',
139 {if $field.headerPattern}'headerPattern' => '{$field.headerPattern}',{/if}
140 {if $field.dataPattern}'dataPattern' => '{$field.dataPattern}',{/if}
6a488035 141{if $field.export}
c3fc2621 142 'export' => {$field.export|strtoupper},
6a488035 143{/if} {* field.export *}
f5c0f096
CW
144{if $field.contactType}
145 'contactType' => {if $field.contactType == 'null'}NULL{else}'{$field.contactType}'{/if},
146{/if}
6a488035
TO
147{if $field.rule}
148 'rule' => '{$field.rule}',
149{/if} {* field.rule *}
1713a0ec
CW
150{if !empty($field.permission)}
151 'permission' => {$field.permission|@print_array},
042043f1 152{/if}
45a83e42 153{if $field.default || $field.default === '0'}
9e1ed301 154 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
6a488035 155{/if} {* field.default *}
3cef3c00 156 'table_name' => '{$table.name}',
157 'entity' => '{$table.entity}',
158 'bao' => '{$table.bao}',
fbdb9a4d 159 'localizable' => {if $field.localizable}1{else}0{/if},
65c86f7d 160 {if $field.localize_context}'localize_context' => '{$field.localize_context}',{/if}
6a488035
TO
161
162{if $field.FKClassName}
163 'FKClassName' => '{$field.FKClassName}',
2a5c9b4d
CW
164{/if}
165{if $field.serialize}
166 'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
167{/if}
74db51d3 168{if $field.uniqueTitle}
169 'unique_title' => {$tsFunctionName}('{$field.uniqueTitle}'),
170{/if}
5e545f38 171{if $field.html}
4361de53
CW
172 'html' => array(
173 {foreach from=$field.html item=val key=key}
8af97d90 174 '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
4361de53
CW
175 {/foreach}
176 ),
177{/if}
6a488035 178{if $field.pseudoconstant}
800d9240 179 'pseudoconstant' => {$field.pseudoconstant|@print_array},
a9d0587b
CW
180{/if}
181 'add' => {if $field.add}'{$field.add}'{else}NULL{/if},
182),
6a488035
TO
183{/foreach} {* table.fields *}
184 );
740dd877 185 CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
6a488035 186 {rdelim}
740dd877 187 return Civi::$statics[__CLASS__]['fields'];
6a488035
TO
188 {rdelim}
189
66abbb6d 190 /**
787b31c7 191 * Return a mapping from field-name to the corresponding key (as used in fields()).
66abbb6d 192 *
66abbb6d 193 * @return array
787b31c7 194 * Array(string $name => string $uniqueName).
66abbb6d 195 */
c3fc2621 196 public static function &fieldKeys( ) {ldelim}
787b31c7
TO
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'];
66abbb6d
AS
201 {rdelim}
202
6a488035 203 /**
b6e60008 204 * Returns the names of this table
6a488035 205 *
6a488035
TO
206 * @return string
207 */
c3fc2621 208 public static function getTableName( ) {ldelim}
6a488035
TO
209 {if $table.localizable}
210 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
211 {else}
212 return self::$_tableName;
213 {/if}
214 {rdelim}
215
216 /**
b6e60008 217 * Returns if this table needs to be logged
6a488035 218 *
c3fc2621 219 * @return bool
6a488035 220 */
c3fc2621 221 public function getLog( ) {ldelim}
6a488035
TO
222 return self::$_log;
223 {rdelim}
224
225 /**
b6e60008
CW
226 * Returns the list of fields that can be imported
227 *
228 * @param bool $prefix
6a488035 229 *
b6e60008 230 * @return array
6a488035 231 */
c3fc2621 232 public static function &import( $prefix = FALSE ) {ldelim}
84a0493c
TO
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;
6a488035
TO
239 {rdelim}
240
241 /**
b6e60008
CW
242 * Returns the list of fields that can be exported
243 *
244 * @param bool $prefix
245 *
246 * @return array
247 */
c3fc2621 248 public static function &export( $prefix = FALSE ) {ldelim}
84a0493c
TO
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;
6a488035
TO
255 {rdelim}
256
6b86d84f
AS
257 /**
258 * Returns the list of indices
c3fc2621
CW
259 *
260 * @param bool $localize
261 *
262 * @return array
6b86d84f
AS
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}
c3fc2621 268
6a488035 269{rdelim}