| 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 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' => '{$field.default|substring:1:-1}', |
| 215 | {/if} {* field.default *} |
| 216 | {if $field.enumValues} |
| 217 | 'enumValues' => '{$field.enumValues}', |
| 218 | {/if} {* field.enumValues *} |
| 219 | |
| 220 | {if $field.FKClassName} |
| 221 | 'FKClassName' => '{$field.FKClassName}', |
| 222 | {/if} {* field.FKClassName *} |
| 223 | {if $field.pseudoconstant} |
| 224 | {assign var=pseudoOptions value=$field.pseudoconstant} |
| 225 | 'pseudoconstant' => array( |
| 226 | {*{$pseudoOptions|@print_array}*} |
| 227 | {foreach from=$pseudoOptions key=optionKey item=optionValue} |
| 228 | '{$optionKey}' => '{$optionValue}', |
| 229 | {/foreach} |
| 230 | ) |
| 231 | {/if} {* field.pseudoconstant *} ), |
| 232 | {/foreach} {* table.fields *} |
| 233 | ); |
| 234 | {rdelim} |
| 235 | return self::$_fields; |
| 236 | {rdelim} |
| 237 | |
| 238 | /** |
| 239 | * Returns an array containing, for each field, the arary key used for that |
| 240 | * field in self::$_fields. |
| 241 | * |
| 242 | * @access public |
| 243 | * @return array |
| 244 | */ |
| 245 | static function &fieldKeys( ) {ldelim} |
| 246 | if ( ! ( self::$_fieldKeys ) ) {ldelim} |
| 247 | self::$_fieldKeys = array ( |
| 248 | {foreach from=$table.fields item=field} |
| 249 | '{$field.name}' => |
| 250 | {if $field.uniqueName} |
| 251 | '{$field.uniqueName}' |
| 252 | {else} |
| 253 | '{$field.name}' |
| 254 | {/if}, |
| 255 | |
| 256 | {/foreach} {* table.fields *} |
| 257 | ); |
| 258 | {rdelim} |
| 259 | return self::$_fieldKeys; |
| 260 | {rdelim} |
| 261 | |
| 262 | /** |
| 263 | * returns the names of this table |
| 264 | * |
| 265 | * @access public |
| 266 | * @static |
| 267 | * @return string |
| 268 | */ |
| 269 | static function getTableName( ) {ldelim} |
| 270 | {if $table.localizable} |
| 271 | return CRM_Core_DAO::getLocaleTableName( self::$_tableName ); |
| 272 | {else} |
| 273 | return self::$_tableName; |
| 274 | {/if} |
| 275 | {rdelim} |
| 276 | |
| 277 | /** |
| 278 | * returns if this table needs to be logged |
| 279 | * |
| 280 | * @access public |
| 281 | * @return boolean |
| 282 | */ |
| 283 | function getLog( ) {ldelim} |
| 284 | return self::$_log; |
| 285 | {rdelim} |
| 286 | |
| 287 | /** |
| 288 | * returns the list of fields that can be imported |
| 289 | * |
| 290 | * @access public |
| 291 | * return array |
| 292 | * @static |
| 293 | */ |
| 294 | static function &import( $prefix = false ) {ldelim} |
| 295 | if ( ! ( self::$_import ) ) {ldelim} |
| 296 | self::$_import = array ( ); |
| 297 | $fields = self::fields( ); |
| 298 | foreach ( $fields as $name => $field ) {ldelim} |
| 299 | if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim} |
| 300 | if ( $prefix ) {ldelim} |
| 301 | self::$_import['{$table.labelName}'] =& $fields[$name]; |
| 302 | {rdelim} else {ldelim} |
| 303 | self::$_import[$name] =& $fields[$name]; |
| 304 | {rdelim} |
| 305 | {rdelim} |
| 306 | {rdelim} |
| 307 | {if $table.foreignKey} |
| 308 | {foreach from=$table.foreignKey item=foreign} |
| 309 | {if $foreign.import} |
| 310 | self::$_import = array_merge( self::$_import, |
| 311 | {$foreign.className}::import( true ) ); |
| 312 | {/if} |
| 313 | {/foreach} |
| 314 | {/if} |
| 315 | {rdelim} |
| 316 | return self::$_import; |
| 317 | {rdelim} |
| 318 | |
| 319 | /** |
| 320 | * returns the list of fields that can be exported |
| 321 | * |
| 322 | * @access public |
| 323 | * return array |
| 324 | * @static |
| 325 | */ |
| 326 | static function &export( $prefix = false ) {ldelim} |
| 327 | if ( ! ( self::$_export ) ) {ldelim} |
| 328 | self::$_export = array ( ); |
| 329 | $fields = self::fields( ); |
| 330 | foreach ( $fields as $name => $field ) {ldelim} |
| 331 | if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim} |
| 332 | if ( $prefix ) {ldelim} |
| 333 | self::$_export['{$table.labelName}'] =& $fields[$name]; |
| 334 | {rdelim} else {ldelim} |
| 335 | self::$_export[$name] =& $fields[$name]; |
| 336 | {rdelim} |
| 337 | {rdelim} |
| 338 | {rdelim} |
| 339 | {if $table.foreignKey} |
| 340 | {foreach from=$table.foreignKey item=foreign} |
| 341 | {if $foreign.export} |
| 342 | self::$_export = array_merge( self::$_export, |
| 343 | {$foreign.className}::export( true ) ); |
| 344 | {/if} |
| 345 | {/foreach} |
| 346 | {/if} |
| 347 | {rdelim} |
| 348 | return self::$_export; |
| 349 | {rdelim} |
| 350 | |
| 351 | {if $table.hasEnum} |
| 352 | /** |
| 353 | * returns an array containing the enum fields of the {$table.name} table |
| 354 | * |
| 355 | * @return array (reference) the array of enum fields |
| 356 | */ |
| 357 | static function &getEnums() {ldelim} |
| 358 | static $enums = array( |
| 359 | {foreach from=$table.fields item=field} |
| 360 | {if $field.crmType == 'CRM_Utils_Type::T_ENUM'} |
| 361 | '{$field.name}', |
| 362 | {/if} |
| 363 | {/foreach} |
| 364 | ); |
| 365 | return $enums; |
| 366 | {rdelim} |
| 367 | |
| 368 | /** |
| 369 | * returns a ts()-translated enum value for display purposes |
| 370 | * |
| 371 | * @param string $field the enum field in question |
| 372 | * @param string $value the enum value up for translation |
| 373 | * |
| 374 | * @return string the display value of the enum |
| 375 | */ |
| 376 | static function tsEnum($field, $value) {ldelim} |
| 377 | static $translations = null; |
| 378 | if (!$translations) {ldelim} |
| 379 | $translations = array( |
| 380 | {foreach from=$table.fields item=field} |
| 381 | {if $field.crmType == 'CRM_Utils_Type::T_ENUM'} |
| 382 | '{$field.name}' => array( |
| 383 | {foreach from=$field.values item=value} |
| 384 | '{$value}' => ts('{$value}'), |
| 385 | {/foreach} |
| 386 | ), |
| 387 | {/if} |
| 388 | {/foreach} |
| 389 | ); |
| 390 | {rdelim} |
| 391 | return $translations[$field][$value]; |
| 392 | {rdelim} |
| 393 | |
| 394 | /** |
| 395 | * adds $value['foo_display'] for each $value['foo'] enum from {$table.name} |
| 396 | * |
| 397 | * @param array $values (reference) the array up for enhancing |
| 398 | * @return void |
| 399 | */ |
| 400 | static function addDisplayEnums(&$values) {ldelim} |
| 401 | $enumFields =& {$table.className}::getEnums(); |
| 402 | foreach ($enumFields as $enum) {ldelim} |
| 403 | if (isset($values[$enum])) {ldelim} |
| 404 | $values[$enum.'_display'] = {$table.className}::tsEnum($enum, $values[$enum]); |
| 405 | {rdelim} |
| 406 | {rdelim} |
| 407 | {rdelim} |
| 408 | {/if} |
| 409 | |
| 410 | {rdelim} |
| 411 | |
| 412 | |