manually merged work on CRM-10592, #527
[civicrm-core.git] / xml / templates / dao.tpl
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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 FK relationships
69 *
70 * @var string
71 * @static
72 */
73 static $_links = null;
74
75 /**
76 * static instance to hold the values that can
77 * be imported
78 *
79 * @var array
80 * @static
81 */
82 static $_import = null;
83
84 /**
85 * static instance to hold the values that can
86 * be exported
87 *
88 * @var array
89 * @static
90 */
91 static $_export = null;
92
93 /**
94 * static value to see if we should log any modifications to
95 * this table in the civicrm_log table
96 *
97 * @var boolean
98 * @static
99 */
100 static $_log = {$table.log};
101
102 {foreach from=$table.fields item=field}
103 /**
104 {if $field.comment}
105 * {$field.comment}
106 {/if}
107 *
108 * @var {$field.phpType}
109 */
110 public ${$field.name};
111
112 {/foreach} {* table.fields *}
113
114 /**
115 * class constructor
116 *
117 * @access public
118 * @return {$table.name}
119 */
120 function __construct( ) {ldelim}
121 $this->__table = '{$table.name}';
122
123 parent::__construct( );
124 {rdelim}
125
126 {if $table.foreignKey || $table.dynamicForeignKey}
127 /**
128 * return foreign keys and entity references
129 *
130 * @static
131 * @access public
132 * @return array of CRM_Core_EntityReference
133 */
134 static function getReferenceColumns() {ldelim}
135 if (!self::$_links) {ldelim}
136 self::$_links = array(
137 {foreach from=$table.foreignKey item=foreign}
138 new CRM_Core_EntityReference(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}'),
139 {/foreach}
140
141 {foreach from=$table.dynamicForeignKey item=foreign}
142 new CRM_Core_EntityReference(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}'),
143 {/foreach}
144 );
145 {rdelim}
146 return self::$_links;
147 {rdelim}
148 {/if} {* table.foreignKey *}
149
150 /**
151 * returns all the column names of this table
152 *
153 * @access public
154 * @return array
155 */
156 static function &fields( ) {ldelim}
157 if ( ! ( self::$_fields ) ) {ldelim}
158 self::$_fields = array (
159 {foreach from=$table.fields item=field}
160
161 {if $field.uniqueName}
162 '{$field.uniqueName}'
163 {else}
164 '{$field.name}'
165 {/if}
166 => array(
167 'name' => '{$field.name}',
168 'type' => {$field.crmType},
169 {if $field.title}
170 'title' => ts('{$field.title}'),
171 {/if}
172 {if $field.required}
173 'required' => {$field.required},
174 {/if} {* field.required *}
175 {if $field.length}
176 'maxlength' => {$field.length},
177 {/if} {* field.length *}
178 {if $field.size}
179 'size' => {$field.size},
180 {/if} {* field.size *}
181 {if $field.rows}
182 'rows' => {$field.rows},
183 {/if} {* field.rows *}
184 {if $field.cols}
185 'cols' => {$field.cols},
186 {/if} {* field.cols *}
187
188 {if $field.import}
189 'import' => {$field.import},
190 'where' => '{$table.name}.{$field.name}',
191 'headerPattern' => '{$field.headerPattern}',
192 'dataPattern' => '{$field.dataPattern}',
193 {/if} {* field.import *}
194 {if $field.export}
195 'export' => {$field.export},
196 {if ! $field.import}
197 'where' => '{$table.name}.{$field.name}',
198 'headerPattern' => '{$field.headerPattern}',
199 'dataPattern' => '{$field.dataPattern}',
200 {/if}
201 {/if} {* field.export *}
202 {if $field.rule}
203 'rule' => '{$field.rule}',
204 {/if} {* field.rule *}
205 {if $field.default}
206 'default' => '{$field.default|substring:1:-1}',
207 {/if} {* field.default *}
208 {if $field.enumValues}
209 'enumValues' => '{$field.enumValues}',
210 {/if} {* field.enumValues *}
211
212 {if $field.FKClassName}
213 'FKClassName' => '{$field.FKClassName}',
214 {/if} {* field.FKClassName *}
215 {if $field.pseudoconstant}
216 {assign var=pseudoOptions value=$field.pseudoconstant}
217 'pseudoconstant' => array(
218 {*{$pseudoOptions|@print_array}*}
219 {foreach from=$pseudoOptions key=optionKey item=optionValue}
220 '{$optionKey}' => '{$optionValue}',
221 {/foreach}
222 )
223 {/if} {* field.pseudoconstant *} ),
224 {/foreach} {* table.fields *}
225 );
226 {rdelim}
227 return self::$_fields;
228 {rdelim}
229
230 /**
231 * returns the names of this table
232 *
233 * @access public
234 * @static
235 * @return string
236 */
237 static function getTableName( ) {ldelim}
238 {if $table.localizable}
239 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
240 {else}
241 return self::$_tableName;
242 {/if}
243 {rdelim}
244
245 /**
246 * returns if this table needs to be logged
247 *
248 * @access public
249 * @return boolean
250 */
251 function getLog( ) {ldelim}
252 return self::$_log;
253 {rdelim}
254
255 /**
256 * returns the list of fields that can be imported
257 *
258 * @access public
259 * return array
260 * @static
261 */
262 static function &import( $prefix = false ) {ldelim}
263 if ( ! ( self::$_import ) ) {ldelim}
264 self::$_import = array ( );
265 $fields = self::fields( );
266 foreach ( $fields as $name => $field ) {ldelim}
267 if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim}
268 if ( $prefix ) {ldelim}
269 self::$_import['{$table.labelName}'] =& $fields[$name];
270 {rdelim} else {ldelim}
271 self::$_import[$name] =& $fields[$name];
272 {rdelim}
273 {rdelim}
274 {rdelim}
275 {if $table.foreignKey}
276 {foreach from=$table.foreignKey item=foreign}
277 {if $foreign.import}
278 self::$_import = array_merge( self::$_import,
279 {$foreign.className}::import( true ) );
280 {/if}
281 {/foreach}
282 {/if}
283 {rdelim}
284 return self::$_import;
285 {rdelim}
286
287 /**
288 * returns the list of fields that can be exported
289 *
290 * @access public
291 * return array
292 * @static
293 */
294 static function &export( $prefix = false ) {ldelim}
295 if ( ! ( self::$_export ) ) {ldelim}
296 self::$_export = array ( );
297 $fields = self::fields( );
298 foreach ( $fields as $name => $field ) {ldelim}
299 if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim}
300 if ( $prefix ) {ldelim}
301 self::$_export['{$table.labelName}'] =& $fields[$name];
302 {rdelim} else {ldelim}
303 self::$_export[$name] =& $fields[$name];
304 {rdelim}
305 {rdelim}
306 {rdelim}
307 {if $table.foreignKey}
308 {foreach from=$table.foreignKey item=foreign}
309 {if $foreign.export}
310 self::$_export = array_merge( self::$_export,
311 {$foreign.className}::export( true ) );
312 {/if}
313 {/foreach}
314 {/if}
315 {rdelim}
316 return self::$_export;
317 {rdelim}
318
319 {if $table.hasEnum}
320 /**
321 * returns an array containing the enum fields of the {$table.name} table
322 *
323 * @return array (reference) the array of enum fields
324 */
325 static function &getEnums() {ldelim}
326 static $enums = array(
327 {foreach from=$table.fields item=field}
328 {if $field.crmType == 'CRM_Utils_Type::T_ENUM'}
329 '{$field.name}',
330 {/if}
331 {/foreach}
332 );
333 return $enums;
334 {rdelim}
335
336 /**
337 * returns a ts()-translated enum value for display purposes
338 *
339 * @param string $field the enum field in question
340 * @param string $value the enum value up for translation
341 *
342 * @return string the display value of the enum
343 */
344 static function tsEnum($field, $value) {ldelim}
345 static $translations = null;
346 if (!$translations) {ldelim}
347 $translations = array(
348 {foreach from=$table.fields item=field}
349 {if $field.crmType == 'CRM_Utils_Type::T_ENUM'}
350 '{$field.name}' => array(
351 {foreach from=$field.values item=value}
352 '{$value}' => ts('{$value}'),
353 {/foreach}
354 ),
355 {/if}
356 {/foreach}
357 );
358 {rdelim}
359 return $translations[$field][$value];
360 {rdelim}
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