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