Annotate generated files with the source filename
[civicrm-core.git] / xml / templates / dao.tpl
CommitLineData
6a488035
TO
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
6a488035 33 *
d5864876
ARW
34 * Generated from {$table.sourceFile}
35 * {$generated}
6a488035
TO
36 */
37
38require_once 'CRM/Core/DAO.php';
39require_once 'CRM/Utils/Type.php';
40
41{if $table.foreignKey}
42 {foreach from=$table.foreignKey item=foreign}
43 {if $foreign.import}
44require_once '{$foreign.fileName}';
45 {/if}
46 {/foreach}
47{/if}
48
49class {$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}
127 /**
128 * return foreign links
129 *
130 * @access public
131 * @return array
132 */
133 function links( ) {ldelim}
134 if ( ! ( self::$_links ) ) {ldelim}
135 self::$_links = array(
136{foreach from=$table.foreignKey item=foreign}
137 '{$foreign.name}' => '{$foreign.table}:{$foreign.key}',
138{/foreach}
139 );
140 {rdelim}
141 return self::$_links;
142 {rdelim}
143{/if} {* table.foreignKey *}
144
145 /**
146 * returns all the column names of this table
147 *
148 * @access public
149 * @return array
150 */
151 static function &fields( ) {ldelim}
152 if ( ! ( self::$_fields ) ) {ldelim}
153 self::$_fields = array (
154{foreach from=$table.fields item=field}
155
156{if $field.uniqueName}
157 '{$field.uniqueName}'
158{else}
159 '{$field.name}'
160{/if}
161 => array(
162 'name' => '{$field.name}',
163 'type' => {$field.crmType},
164{if $field.title}
165 'title' => ts('{$field.title}'),
166{/if}
167{if $field.required}
168 'required' => {$field.required},
169{/if} {* field.required *}
170{if $field.length}
171 'maxlength' => {$field.length},
172{/if} {* field.length *}
173{if $field.size}
174 'size' => {$field.size},
175{/if} {* field.size *}
176{if $field.rows}
177 'rows' => {$field.rows},
178{/if} {* field.rows *}
179{if $field.cols}
180 'cols' => {$field.cols},
181{/if} {* field.cols *}
182
183{if $field.import}
184 'import' => {$field.import},
185 'where' => '{$table.name}.{$field.name}',
186 'headerPattern' => '{$field.headerPattern}',
187 'dataPattern' => '{$field.dataPattern}',
188{/if} {* field.import *}
189{if $field.export}
190 'export' => {$field.export},
191 {if ! $field.import}
192 'where' => '{$table.name}.{$field.name}',
193 'headerPattern' => '{$field.headerPattern}',
194 'dataPattern' => '{$field.dataPattern}',
195 {/if}
196{/if} {* field.export *}
197{if $field.rule}
198 'rule' => '{$field.rule}',
199{/if} {* field.rule *}
200{if $field.default}
201 'default' => '{$field.default|substring:1:-1}',
202{/if} {* field.default *}
203{if $field.enumValues}
204 'enumValues' => '{$field.enumValues}',
205{/if} {* field.enumValues *}
206
207{if $field.FKClassName}
208 'FKClassName' => '{$field.FKClassName}',
209{/if} {* field.FKClassName *}
210{if $field.pseudoconstant}
211{assign var=pseudoOptions value=$field.pseudoconstant}
212'pseudoconstant' => array(
213{*{$pseudoOptions|@print_array}*}
214{foreach from=$pseudoOptions key=optionKey item=optionValue}
215 '{$optionKey}' => '{$optionValue}',
216 {/foreach}
217 )
218{/if} {* field.pseudoconstant *} ),
219{/foreach} {* table.fields *}
220 );
221 {rdelim}
222 return self::$_fields;
223 {rdelim}
224
225 /**
226 * returns the names of this table
227 *
228 * @access public
229 * @static
230 * @return string
231 */
232 static function getTableName( ) {ldelim}
233 {if $table.localizable}
234 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
235 {else}
236 return self::$_tableName;
237 {/if}
238 {rdelim}
239
240 /**
241 * returns if this table needs to be logged
242 *
243 * @access public
244 * @return boolean
245 */
246 function getLog( ) {ldelim}
247 return self::$_log;
248 {rdelim}
249
250 /**
251 * returns the list of fields that can be imported
252 *
253 * @access public
254 * return array
255 * @static
256 */
257 static function &import( $prefix = false ) {ldelim}
258 if ( ! ( self::$_import ) ) {ldelim}
259 self::$_import = array ( );
260 $fields = self::fields( );
261 foreach ( $fields as $name => $field ) {ldelim}
262 if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim}
263 if ( $prefix ) {ldelim}
264 self::$_import['{$table.labelName}'] =& $fields[$name];
265 {rdelim} else {ldelim}
266 self::$_import[$name] =& $fields[$name];
267 {rdelim}
268 {rdelim}
269 {rdelim}
270 {if $table.foreignKey}
271 {foreach from=$table.foreignKey item=foreign}
272 {if $foreign.import}
273 self::$_import = array_merge( self::$_import,
274 {$foreign.className}::import( true ) );
275 {/if}
276 {/foreach}
277 {/if}
278 {rdelim}
279 return self::$_import;
280 {rdelim}
281
282 /**
283 * returns the list of fields that can be exported
284 *
285 * @access public
286 * return array
287 * @static
288 */
289 static function &export( $prefix = false ) {ldelim}
290 if ( ! ( self::$_export ) ) {ldelim}
291 self::$_export = array ( );
292 $fields = self::fields( );
293 foreach ( $fields as $name => $field ) {ldelim}
294 if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim}
295 if ( $prefix ) {ldelim}
296 self::$_export['{$table.labelName}'] =& $fields[$name];
297 {rdelim} else {ldelim}
298 self::$_export[$name] =& $fields[$name];
299 {rdelim}
300 {rdelim}
301 {rdelim}
302 {if $table.foreignKey}
303 {foreach from=$table.foreignKey item=foreign}
304 {if $foreign.export}
305 self::$_export = array_merge( self::$_export,
306 {$foreign.className}::export( true ) );
307 {/if}
308 {/foreach}
309 {/if}
310 {rdelim}
311 return self::$_export;
312 {rdelim}
313
314{if $table.hasEnum}
315 /**
316 * returns an array containing the enum fields of the {$table.name} table
317 *
318 * @return array (reference) the array of enum fields
319 */
320 static function &getEnums() {ldelim}
321 static $enums = array(
322 {foreach from=$table.fields item=field}
323 {if $field.crmType == 'CRM_Utils_Type::T_ENUM'}
324 '{$field.name}',
325 {/if}
326 {/foreach}
327 );
328 return $enums;
329 {rdelim}
330
331 /**
332 * returns a ts()-translated enum value for display purposes
333 *
334 * @param string $field the enum field in question
335 * @param string $value the enum value up for translation
336 *
337 * @return string the display value of the enum
338 */
339 static function tsEnum($field, $value) {ldelim}
340 static $translations = null;
341 if (!$translations) {ldelim}
342 $translations = array(
343 {foreach from=$table.fields item=field}
344 {if $field.crmType == 'CRM_Utils_Type::T_ENUM'}
345 '{$field.name}' => array(
346 {foreach from=$field.values item=value}
347 '{$value}' => ts('{$value}'),
348 {/foreach}
349 ),
350 {/if}
351 {/foreach}
352 );
353 {rdelim}
354 return $translations[$field][$value];
355 {rdelim}
356
357 /**
358 * adds $value['foo_display'] for each $value['foo'] enum from {$table.name}
359 *
360 * @param array $values (reference) the array up for enhancing
361 * @return void
362 */
363 static function addDisplayEnums(&$values) {ldelim}
364 $enumFields =& {$table.className}::getEnums();
365 foreach ($enumFields as $enum) {ldelim}
366 if (isset($values[$enum])) {ldelim}
367 $values[$enum.'_display'] = {$table.className}::tsEnum($enum, $values[$enum]);
368 {rdelim}
369 {rdelim}
370 {rdelim}
371{/if}
372
373{rdelim}
374
375