CRM-14181 fixes, deleted tsEnum() and code fixes
[civicrm-core.git] / xml / templates / dao.tpl
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.4 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 *
34 * Generated from {$table.sourceFile}
35 * {$generated}
36 */
37
38 require_once 'CRM/Core/DAO.php';
39 require_once 'CRM/Utils/Type.php';
40
41 {if $table.foreignKey}
42 {foreach from=$table.foreignKey item=foreign}
43 {if $foreign.import}
44 require_once '{$foreign.fileName}';
45 {/if}
46 {/foreach}
47 {/if}
48
49 class {$table.className} extends CRM_Core_DAO {ldelim}
50
51 /**
52 * static instance to hold the table name
53 *
54 * @var string
55 * @static
56 */
57 static $_tableName = '{$table.name}';
58
59 /**
60 * static instance to hold the field values
61 *
62 * @var array
63 * @static
64 */
65 static $_fields = null;
66
67 /**
68 * static instance to hold the keys used in $_fields for each field.
69 *
70 * @var array
71 * @static
72 */
73 static $_fieldKeys = null;
74
75 /**
76 * static instance to hold the FK relationships
77 *
78 * @var string
79 * @static
80 */
81 static $_links = null;
82
83 /**
84 * static instance to hold the values that can
85 * be imported
86 *
87 * @var array
88 * @static
89 */
90 static $_import = null;
91
92 /**
93 * static instance to hold the values that can
94 * be exported
95 *
96 * @var array
97 * @static
98 */
99 static $_export = null;
100
101 /**
102 * static value to see if we should log any modifications to
103 * this table in the civicrm_log table
104 *
105 * @var boolean
106 * @static
107 */
108 static $_log = {$table.log};
109
110 {foreach from=$table.fields item=field}
111 /**
112 {if $field.comment}
113 * {$field.comment}
114 {/if}
115 *
116 * @var {$field.phpType}
117 */
118 public ${$field.name};
119
120 {/foreach} {* table.fields *}
121
122 /**
123 * class constructor
124 *
125 * @access public
126 * @return {$table.name}
127 */
128 function __construct( ) {ldelim}
129 $this->__table = '{$table.name}';
130
131 parent::__construct( );
132 {rdelim}
133
134 {if $table.foreignKey || $table.dynamicForeignKey}
135 /**
136 * return foreign keys and entity references
137 *
138 * @static
139 * @access public
140 * @return array of CRM_Core_EntityReference
141 */
142 static function getReferenceColumns() {ldelim}
143 if (!self::$_links) {ldelim}
144 self::$_links = array(
145 {foreach from=$table.foreignKey item=foreign}
146 new CRM_Core_EntityReference(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'),
147 {/foreach}
148
149 {foreach from=$table.dynamicForeignKey item=foreign}
150 new CRM_Core_EntityReference(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'),
151 {/foreach}
152 );
153 {rdelim}
154 return self::$_links;
155 {rdelim}
156 {/if} {* table.foreignKey *}
157
158 /**
159 * returns all the column names of this table
160 *
161 * @access public
162 * @return array
163 */
164 static function &fields( ) {ldelim}
165 if ( ! ( self::$_fields ) ) {ldelim}
166 self::$_fields = array (
167 {foreach from=$table.fields item=field}
168
169 {if $field.uniqueName}
170 '{$field.uniqueName}'
171 {else}
172 '{$field.name}'
173 {/if}
174 => array(
175 'name' => '{$field.name}',
176 'type' => {$field.crmType},
177 {if $field.title}
178 'title' => ts('{$field.title}'),
179 {/if}
180 {if $field.required}
181 'required' => {$field.required},
182 {/if} {* field.required *}
183 {if $field.length}
184 'maxlength' => {$field.length},
185 {/if} {* field.length *}
186 {if $field.size}
187 'size' => {$field.size},
188 {/if} {* field.size *}
189 {if $field.rows}
190 'rows' => {$field.rows},
191 {/if} {* field.rows *}
192 {if $field.cols}
193 'cols' => {$field.cols},
194 {/if} {* field.cols *}
195
196 {if $field.import}
197 'import' => {$field.import},
198 'where' => '{$table.name}.{$field.name}',
199 'headerPattern' => '{$field.headerPattern}',
200 'dataPattern' => '{$field.dataPattern}',
201 {/if} {* field.import *}
202 {if $field.export}
203 'export' => {$field.export},
204 {if ! $field.import}
205 'where' => '{$table.name}.{$field.name}',
206 'headerPattern' => '{$field.headerPattern}',
207 'dataPattern' => '{$field.dataPattern}',
208 {/if}
209 {/if} {* field.export *}
210 {if $field.rule}
211 'rule' => '{$field.rule}',
212 {/if} {* field.rule *}
213 {if $field.default}
214 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
215 {/if} {* field.default *}
216 {if $field.enumValues}
217 'enumValues' => '{$field.enumValues}',
218 {/if} {* field.enumValues *}
219
220 {if $field.FKClassName}
221 'FKClassName' => '{$field.FKClassName}',
222 {/if} {* field.FKClassName *}
223 {if $field.html}
224 {assign var=htmlOptions value=$field.html}
225 'html' => array(
226 {*{$htmlOptions|@print_array}*}
227 {foreach from=$htmlOptions key=optionKey item=optionValue}
228 '{$optionKey}' => '{$optionValue}',
229 {/foreach}
230 ),
231 {/if} {* field.html *}
232 {if $field.pseudoconstant}
233 {assign var=pseudoOptions value=$field.pseudoconstant}
234 'pseudoconstant' => array(
235 {*{$pseudoOptions|@print_array}*}
236 {foreach from=$pseudoOptions key=optionKey item=optionValue}
237 '{$optionKey}' => '{$optionValue}',
238 {/foreach}
239 )
240 {/if} {* field.pseudoconstant *} ),
241 {/foreach} {* table.fields *}
242 );
243 {rdelim}
244 return self::$_fields;
245 {rdelim}
246
247 /**
248 * Returns an array containing, for each field, the arary key used for that
249 * field in self::$_fields.
250 *
251 * @access public
252 * @return array
253 */
254 static function &fieldKeys( ) {ldelim}
255 if ( ! ( self::$_fieldKeys ) ) {ldelim}
256 self::$_fieldKeys = array (
257 {foreach from=$table.fields item=field}
258 '{$field.name}' =>
259 {if $field.uniqueName}
260 '{$field.uniqueName}'
261 {else}
262 '{$field.name}'
263 {/if},
264
265 {/foreach} {* table.fields *}
266 );
267 {rdelim}
268 return self::$_fieldKeys;
269 {rdelim}
270
271 /**
272 * returns the names of this table
273 *
274 * @access public
275 * @static
276 * @return string
277 */
278 static function getTableName( ) {ldelim}
279 {if $table.localizable}
280 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
281 {else}
282 return self::$_tableName;
283 {/if}
284 {rdelim}
285
286 /**
287 * returns if this table needs to be logged
288 *
289 * @access public
290 * @return boolean
291 */
292 function getLog( ) {ldelim}
293 return self::$_log;
294 {rdelim}
295
296 /**
297 * returns the list of fields that can be imported
298 *
299 * @access public
300 * return array
301 * @static
302 */
303 static function &import( $prefix = false ) {ldelim}
304 if ( ! ( self::$_import ) ) {ldelim}
305 self::$_import = array ( );
306 $fields = self::fields( );
307 foreach ( $fields as $name => $field ) {ldelim}
308 if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim}
309 if ( $prefix ) {ldelim}
310 self::$_import['{$table.labelName}'] =& $fields[$name];
311 {rdelim} else {ldelim}
312 self::$_import[$name] =& $fields[$name];
313 {rdelim}
314 {rdelim}
315 {rdelim}
316 {if $table.foreignKey}
317 {foreach from=$table.foreignKey item=foreign}
318 {if $foreign.import}
319 self::$_import = array_merge( self::$_import,
320 {$foreign.className}::import( true ) );
321 {/if}
322 {/foreach}
323 {/if}
324 {rdelim}
325 return self::$_import;
326 {rdelim}
327
328 /**
329 * returns the list of fields that can be exported
330 *
331 * @access public
332 * return array
333 * @static
334 */
335 static function &export( $prefix = false ) {ldelim}
336 if ( ! ( self::$_export ) ) {ldelim}
337 self::$_export = array ( );
338 $fields = self::fields( );
339 foreach ( $fields as $name => $field ) {ldelim}
340 if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim}
341 if ( $prefix ) {ldelim}
342 self::$_export['{$table.labelName}'] =& $fields[$name];
343 {rdelim} else {ldelim}
344 self::$_export[$name] =& $fields[$name];
345 {rdelim}
346 {rdelim}
347 {rdelim}
348 {if $table.foreignKey}
349 {foreach from=$table.foreignKey item=foreign}
350 {if $foreign.export}
351 self::$_export = array_merge( self::$_export,
352 {$foreign.className}::export( true ) );
353 {/if}
354 {/foreach}
355 {/if}
356 {rdelim}
357 return self::$_export;
358 {rdelim}
359
360 {if $table.hasEnum}
361
362 /**
363 * adds $value['foo_display'] for each $value['foo'] enum from {$table.name}
364 *
365 * @param array $values (reference) the array up for enhancing
366 * @return void
367 */
368 static function addDisplayEnums(&$values) {ldelim}
369 $enumFields =& {$table.className}::getEnums();
370 foreach ($enumFields as $enum) {ldelim}
371 if (isset($values[$enum])) {ldelim}
372 $values[$enum.'_display'] = {$table.className}::tsEnum($enum, $values[$enum]);
373 {rdelim}
374 {rdelim}
375 {rdelim}
376 {/if}
377
378 {rdelim}
379
380