Merge pull request #2546 from brylie/master
[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
217 {if $field.FKClassName}
218 'FKClassName' => '{$field.FKClassName}',
219 {/if} {* field.FKClassName *}
220 {if $field.html}
221 {assign var=htmlOptions value=$field.html}
222 'html' => array(
223 {*{$htmlOptions|@print_array}*}
224 {foreach from=$htmlOptions key=optionKey item=optionValue}
225 '{$optionKey}' => '{$optionValue}',
226 {/foreach}
227 ),
228 {/if} {* field.html *}
229 {if $field.pseudoconstant}
230 {assign var=pseudoOptions value=$field.pseudoconstant}
231 'pseudoconstant' => array(
232 {*{$pseudoOptions|@print_array}*}
233 {foreach from=$pseudoOptions key=optionKey item=optionValue}
234 '{$optionKey}' => '{$optionValue}',
235 {/foreach}
236 )
237 {/if} {* field.pseudoconstant *} ),
238 {/foreach} {* table.fields *}
239 );
240 {rdelim}
241 return self::$_fields;
242 {rdelim}
243
244 /**
245 * Returns an array containing, for each field, the arary key used for that
246 * field in self::$_fields.
247 *
248 * @access public
249 * @return array
250 */
251 static function &fieldKeys( ) {ldelim}
252 if ( ! ( self::$_fieldKeys ) ) {ldelim}
253 self::$_fieldKeys = array (
254 {foreach from=$table.fields item=field}
255 '{$field.name}' =>
256 {if $field.uniqueName}
257 '{$field.uniqueName}'
258 {else}
259 '{$field.name}'
260 {/if},
261
262 {/foreach} {* table.fields *}
263 );
264 {rdelim}
265 return self::$_fieldKeys;
266 {rdelim}
267
268 /**
269 * returns the names of this table
270 *
271 * @access public
272 * @static
273 * @return string
274 */
275 static function getTableName( ) {ldelim}
276 {if $table.localizable}
277 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
278 {else}
279 return self::$_tableName;
280 {/if}
281 {rdelim}
282
283 /**
284 * returns if this table needs to be logged
285 *
286 * @access public
287 * @return boolean
288 */
289 function getLog( ) {ldelim}
290 return self::$_log;
291 {rdelim}
292
293 /**
294 * returns the list of fields that can be imported
295 *
296 * @access public
297 * return array
298 * @static
299 */
300 static function &import( $prefix = false ) {ldelim}
301 if ( ! ( self::$_import ) ) {ldelim}
302 self::$_import = array ( );
303 $fields = self::fields( );
304 foreach ( $fields as $name => $field ) {ldelim}
305 if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim}
306 if ( $prefix ) {ldelim}
307 self::$_import['{$table.labelName}'] =& $fields[$name];
308 {rdelim} else {ldelim}
309 self::$_import[$name] =& $fields[$name];
310 {rdelim}
311 {rdelim}
312 {rdelim}
313 {if $table.foreignKey}
314 {foreach from=$table.foreignKey item=foreign}
315 {if $foreign.import}
316 self::$_import = array_merge( self::$_import,
317 {$foreign.className}::import( true ) );
318 {/if}
319 {/foreach}
320 {/if}
321 {rdelim}
322 return self::$_import;
323 {rdelim}
324
325 /**
326 * returns the list of fields that can be exported
327 *
328 * @access public
329 * return array
330 * @static
331 */
332 static function &export( $prefix = false ) {ldelim}
333 if ( ! ( self::$_export ) ) {ldelim}
334 self::$_export = array ( );
335 $fields = self::fields( );
336 foreach ( $fields as $name => $field ) {ldelim}
337 if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim}
338 if ( $prefix ) {ldelim}
339 self::$_export['{$table.labelName}'] =& $fields[$name];
340 {rdelim} else {ldelim}
341 self::$_export[$name] =& $fields[$name];
342 {rdelim}
343 {rdelim}
344 {rdelim}
345 {if $table.foreignKey}
346 {foreach from=$table.foreignKey item=foreign}
347 {if $foreign.export}
348 self::$_export = array_merge( self::$_export,
349 {$foreign.className}::export( true ) );
350 {/if}
351 {/foreach}
352 {/if}
353 {rdelim}
354 return self::$_export;
355 {rdelim}
356
357 {rdelim}
358
359