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