Merge branch '4.5' into master
[civicrm-core.git] / xml / templates / dao.tpl
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2014
31 *
32 * Generated from {$table.sourceFile}
33 * {$generated}
34 */
35
36 require_once 'CRM/Core/DAO.php';
37 require_once 'CRM/Utils/Type.php';
38
39 {if $table.foreignKey}
40 {foreach from=$table.foreignKey item=foreign}
41 {if $foreign.import}
42 require_once '{$foreign.fileName}';
43 {/if}
44 {/foreach}
45 {/if}
46
47 class {$table.className} extends CRM_Core_DAO {ldelim}
48
49 /**
50 * static instance to hold the table name
51 *
52 * @var string
53 */
54 static $_tableName = '{$table.name}';
55
56 /**
57 * static instance to hold the field values
58 *
59 * @var array
60 */
61 static $_fields = null;
62
63 /**
64 * static instance to hold the keys used in $_fields for each field.
65 *
66 * @var array
67 */
68 static $_fieldKeys = null;
69
70 /**
71 * static instance to hold the FK relationships
72 *
73 * @var string
74 */
75 static $_links = null;
76
77 /**
78 * static instance to hold the values that can
79 * be imported
80 *
81 * @var array
82 */
83 static $_import = null;
84
85 /**
86 * static instance to hold the values that can
87 * be exported
88 *
89 * @var array
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 */
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 * @return {$table.name}
117 */
118 function __construct( ) {ldelim}
119 $this->__table = '{$table.name}';
120
121 parent::__construct( );
122 {rdelim}
123
124 {if $table.foreignKey || $table.dynamicForeignKey}
125 /**
126 * Returns foreign keys and entity references
127 *
128 * @return array
129 * [CRM_Core_Reference_Interface]
130 */
131 static function getReferenceColumns() {ldelim}
132 if (!self::$_links) {ldelim}
133 self::$_links = static::createReferenceColumns(__CLASS__);
134 {foreach from=$table.foreignKey item=foreign}
135 self::$_links[] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
136 {/foreach}
137
138 {foreach from=$table.dynamicForeignKey item=foreign}
139 self::$_links[] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
140 {/foreach}
141 {rdelim}
142 return self::$_links;
143 {rdelim}
144 {/if} {* table.foreignKey *}
145
146 /**
147 * Returns all the column names of this table
148 *
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.precision}
174 'precision' => array({$field.precision}),
175 {/if}
176 {if $field.size}
177 'size' => {$field.size},
178 {/if} {* field.size *}
179 {if $field.rows}
180 'rows' => {$field.rows},
181 {/if} {* field.rows *}
182 {if $field.cols}
183 'cols' => {$field.cols},
184 {/if} {* field.cols *}
185
186 {if $field.import}
187 'import' => {$field.import},
188 'where' => '{$table.name}.{$field.name}',
189 'headerPattern' => '{$field.headerPattern}',
190 'dataPattern' => '{$field.dataPattern}',
191 {/if} {* field.import *}
192 {if $field.export}
193 'export' => {$field.export},
194 {if ! $field.import}
195 'where' => '{$table.name}.{$field.name}',
196 'headerPattern' => '{$field.headerPattern}',
197 'dataPattern' => '{$field.dataPattern}',
198 {/if}
199 {/if} {* field.export *}
200 {if $field.rule}
201 'rule' => '{$field.rule}',
202 {/if} {* field.rule *}
203 {if $field.default}
204 'default' => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
205 {/if} {* field.default *}
206
207 {if $field.FKClassName}
208 'FKClassName' => '{$field.FKClassName}',
209 {/if} {* field.FKClassName *}
210 {if $field.html}
211 {assign var=htmlOptions value=$field.html}
212 'html' => array(
213 {*{$htmlOptions|@print_array}*}
214 {foreach from=$htmlOptions key=optionKey item=optionValue}
215 '{$optionKey}' => '{$optionValue}',
216 {/foreach}
217 ),
218 {/if} {* field.html *}
219 {if $field.pseudoconstant}
220 {assign var=pseudoOptions value=$field.pseudoconstant}
221 'pseudoconstant' => array(
222 {*{$pseudoOptions|@print_array}*}
223 {foreach from=$pseudoOptions key=optionKey item=optionValue}
224 '{$optionKey}' => '{$optionValue}',
225 {/foreach}
226 )
227 {/if} {* field.pseudoconstant *} ),
228 {/foreach} {* table.fields *}
229 );
230 {rdelim}
231 return self::$_fields;
232 {rdelim}
233
234 /**
235 * Returns an array containing, for each field, the arary key used for that
236 * field in self::$_fields.
237 *
238 * @return array
239 */
240 static function &fieldKeys( ) {ldelim}
241 if ( ! ( self::$_fieldKeys ) ) {ldelim}
242 self::$_fieldKeys = array (
243 {foreach from=$table.fields item=field}
244 '{$field.name}' =>
245 {if $field.uniqueName}
246 '{$field.uniqueName}'
247 {else}
248 '{$field.name}'
249 {/if},
250
251 {/foreach} {* table.fields *}
252 );
253 {rdelim}
254 return self::$_fieldKeys;
255 {rdelim}
256
257 /**
258 * Returns the names of this table
259 *
260 * @return string
261 */
262 static function getTableName( ) {ldelim}
263 {if $table.localizable}
264 return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
265 {else}
266 return self::$_tableName;
267 {/if}
268 {rdelim}
269
270 /**
271 * Returns if this table needs to be logged
272 *
273 * @return boolean
274 */
275 function getLog( ) {ldelim}
276 return self::$_log;
277 {rdelim}
278
279 /**
280 * Returns the list of fields that can be imported
281 *
282 * @param bool $prefix
283 *
284 * @return array
285 */
286 static function &import( $prefix = false ) {ldelim}
287 if ( ! ( self::$_import ) ) {ldelim}
288 self::$_import = array ( );
289 $fields = self::fields( );
290 foreach ( $fields as $name => $field ) {ldelim}
291 if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim}
292 if ( $prefix ) {ldelim}
293 self::$_import['{$table.labelName}'] =& $fields[$name];
294 {rdelim} else {ldelim}
295 self::$_import[$name] =& $fields[$name];
296 {rdelim}
297 {rdelim}
298 {rdelim}
299 {if $table.foreignKey}
300 {foreach from=$table.foreignKey item=foreign}
301 {if $foreign.import}
302 self::$_import = array_merge( self::$_import,
303 {$foreign.className}::import( true ) );
304 {/if}
305 {/foreach}
306 {/if}
307 {rdelim}
308 return self::$_import;
309 {rdelim}
310
311 /**
312 * Returns the list of fields that can be exported
313 *
314 * @param bool $prefix
315 *
316 * @return array
317 */
318 static function &export( $prefix = false ) {ldelim}
319 if ( ! ( self::$_export ) ) {ldelim}
320 self::$_export = array ( );
321 $fields = self::fields( );
322 foreach ( $fields as $name => $field ) {ldelim}
323 if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim}
324 if ( $prefix ) {ldelim}
325 self::$_export['{$table.labelName}'] =& $fields[$name];
326 {rdelim} else {ldelim}
327 self::$_export[$name] =& $fields[$name];
328 {rdelim}
329 {rdelim}
330 {rdelim}
331 {if $table.foreignKey}
332 {foreach from=$table.foreignKey item=foreign}
333 {if $foreign.export}
334 self::$_export = array_merge( self::$_export,
335 {$foreign.className}::export( true ) );
336 {/if}
337 {/foreach}
338 {/if}
339 {rdelim}
340 return self::$_export;
341 {rdelim}
342
343 {rdelim}
344
345