dao.tpl 8.14 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/**
 * @package CRM
4
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
5
 *
6 7
 * Generated from {$table.sourceFile}
 * {$generated}
8
 * (GenCodeChecksum:{$genCodeChecksum})
totten's avatar
totten committed
9 10
 */

eileen's avatar
eileen committed
11
/**
12
 * Database access object for the {$table.entity} entity.
eileen's avatar
eileen committed
13
 */
totten's avatar
totten committed
14 15 16
class {$table.className} extends CRM_Core_DAO {ldelim}

     /**
eileen's avatar
eileen committed
17
      * Static instance to hold the table name.
totten's avatar
totten committed
18 19 20
      *
      * @var string
      */
21
      public static $_tableName = '{$table.name}';
totten's avatar
totten committed
22 23

      /**
eileen's avatar
eileen committed
24
       * Should CiviCRM log any modifications to this table in the civicrm_log table.
totten's avatar
totten committed
25
       *
26
       * @var bool
totten's avatar
totten committed
27
       */
28
      public static $_log = {$table.log|strtoupper};
totten's avatar
totten committed
29 30 31 32 33 34

{foreach from=$table.fields item=field}
    /**
{if $field.comment}
     * {$field.comment}
     *
35
{/if}
totten's avatar
totten committed
36 37 38 39 40 41 42
     * @var {$field.phpType}
     */
    public ${$field.name};

{/foreach} {* table.fields *}

    /**
eileen's avatar
eileen committed
43
     * Class constructor.
totten's avatar
totten committed
44
     */
45
    public function __construct( ) {ldelim}
totten's avatar
totten committed
46 47 48 49 50
        $this->__table = '{$table.name}';

        parent::__construct( );
    {rdelim}

51
{if $table.foreignKey || $table.dynamicForeignKey}
totten's avatar
totten committed
52
    /**
eileen's avatar
eileen committed
53
     * Returns foreign keys and entity references.
totten's avatar
totten committed
54
     *
55 56
     * @return array
     *   [CRM_Core_Reference_Interface]
totten's avatar
totten committed
57
     */
58
    public static function getReferenceColumns() {ldelim}
59 60
      if (!isset(Civi::$statics[__CLASS__]['links'])) {ldelim}
        Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__);
totten's avatar
totten committed
61
{foreach from=$table.foreignKey item=foreign}
62
        Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), '{$foreign.name}', '{$foreign.table}', '{$foreign.key}');
63 64 65
{/foreach}

{foreach from=$table.dynamicForeignKey item=foreign}
66
        Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Dynamic(self::getTableName(), '{$foreign.idColumn}', NULL, '{$foreign.key|default:'id'}', '{$foreign.typeColumn}');
totten's avatar
totten committed
67
{/foreach}
68
        CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']);
69
      {rdelim}
70
      return Civi::$statics[__CLASS__]['links'];
totten's avatar
totten committed
71 72 73 74
    {rdelim}
{/if} {* table.foreignKey *}

      /**
75
       * Returns all the column names of this table
totten's avatar
totten committed
76 77 78
       *
       * @return array
       */
79
      public static function &fields( ) {ldelim}
80
        if ( ! isset(Civi::$statics[__CLASS__]['fields']) ) {ldelim}
81
          Civi::$statics[__CLASS__]['fields'] = array(
totten's avatar
totten committed
82 83 84
{foreach from=$table.fields item=field}

{if $field.uniqueName}
85
  '{$field.uniqueName}'
totten's avatar
totten committed
86 87 88 89
{else}
                                            '{$field.name}'
{/if}
               => array(
90
                 'name'      => '{$field.name}',
totten's avatar
totten committed
91 92
                                                                      'type'      => {$field.crmType},
{if $field.title}
93
                                                                      'title'     => {$tsFunctionName}('{$field.title}'),
totten's avatar
totten committed
94
{/if}
95
{if $field.comment}
96
                                                                      'description'     => {$tsFunctionName}('{$field.comment|replace:"'":"\'"}'),
97
{/if}
totten's avatar
totten committed
98
{if $field.required}
99
                                        'required'  => {$field.required|strtoupper},
totten's avatar
totten committed
100 101 102 103
{/if} {* field.required *}
{if $field.length}
                      'maxlength' => {$field.length},
{/if} {* field.length *}
104 105 106
{if $field.precision}
                      'precision'      => array({$field.precision}),
{/if}
totten's avatar
totten committed
107 108 109 110 111 112 113 114 115 116 117
{if $field.size}
                      'size'      => {$field.size},
{/if} {* field.size *}
{if $field.rows}
                      'rows'      => {$field.rows},
{/if} {* field.rows *}
{if $field.cols}
                      'cols'      => {$field.cols},
{/if} {* field.cols *}

{if $field.import}
118
                      'import'    => {$field.import|strtoupper},
totten's avatar
totten committed
119 120 121 122 123
                                                                      'where'     => '{$table.name}.{$field.name}',
                                      'headerPattern' => '{$field.headerPattern}',
                                      'dataPattern' => '{$field.dataPattern}',
{/if} {* field.import *}
{if $field.export}
124
                      'export'    => {$field.export|strtoupper},
totten's avatar
totten committed
125 126 127 128 129 130 131 132 133
                                      {if ! $field.import}
                      'where'     => '{$table.name}.{$field.name}',
                                      'headerPattern' => '{$field.headerPattern}',
                                      'dataPattern' => '{$field.dataPattern}',
              {/if}
{/if} {* field.export *}
{if $field.rule}
                      'rule'      => '{$field.rule}',
{/if} {* field.rule *}
134
{if $field.default || $field.default === '0'}
135
                         'default'   => '{if ($field.default[0]=="'" or $field.default[0]=='"')}{$field.default|substring:1:-1}{else}{$field.default}{/if}',
totten's avatar
totten committed
136
{/if} {* field.default *}
137 138 139
  'table_name' => '{$table.name}',
  'entity' => '{$table.entity}',
  'bao' => '{$table.bao}',
colemanw's avatar
colemanw committed
140
  'localizable' => {if $field.localizable}1{else}0{/if},
141
  {if $field.localize_context}'localize_context' => '{$field.localize_context}',{/if}
totten's avatar
totten committed
142 143 144

{if $field.FKClassName}
                      'FKClassName' => '{$field.FKClassName}',
145 146 147 148
{/if}
{if $field.serialize}
  'serialize' => self::SERIALIZE_{$field.serialize|strtoupper},
{/if}
149
{if $field.html}
colemanw's avatar
colemanw committed
150 151
  'html' => array(
  {foreach from=$field.html item=val key=key}
152
    '{$key}' => {if $key eq 'label'}{$tsFunctionName}("{$val}"){else}'{$val}'{/if},
colemanw's avatar
colemanw committed
153 154 155
  {/foreach}
  ),
{/if}
totten's avatar
totten committed
156
{if $field.pseudoconstant}
157
  'pseudoconstant' => {$field.pseudoconstant|@print_array}
totten's avatar
totten committed
158 159 160
{/if} {* field.pseudoconstant *}                                                                    ),
{/foreach} {* table.fields *}
                                      );
161
            CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
totten's avatar
totten committed
162
          {rdelim}
163
          return Civi::$statics[__CLASS__]['fields'];
totten's avatar
totten committed
164 165
      {rdelim}

166
      /**
167
       * Return a mapping from field-name to the corresponding key (as used in fields()).
168 169
       *
       * @return array
170
       *   Array(string $name => string $uniqueName).
171
       */
172
      public static function &fieldKeys( ) {ldelim}
173 174 175 176
        if (!isset(Civi::$statics[__CLASS__]['fieldKeys'])) {ldelim}
          Civi::$statics[__CLASS__]['fieldKeys'] = array_flip(CRM_Utils_Array::collect('name', self::fields()));
        {rdelim}
        return Civi::$statics[__CLASS__]['fieldKeys'];
177 178
      {rdelim}

totten's avatar
totten committed
179
      /**
180
       * Returns the names of this table
totten's avatar
totten committed
181 182 183
       *
       * @return string
       */
184
      public static function getTableName( ) {ldelim}
totten's avatar
totten committed
185 186 187 188 189 190 191 192
        {if $table.localizable}
          return CRM_Core_DAO::getLocaleTableName( self::$_tableName );
        {else}
          return self::$_tableName;
        {/if}
      {rdelim}

      /**
193
       * Returns if this table needs to be logged
totten's avatar
totten committed
194
       *
195
       * @return bool
totten's avatar
totten committed
196
       */
197
      public function getLog( ) {ldelim}
totten's avatar
totten committed
198 199 200 201
          return self::$_log;
      {rdelim}

      /**
202 203 204
       * Returns the list of fields that can be imported
       *
       * @param bool $prefix
totten's avatar
totten committed
205
       *
206
       * @return array
totten's avatar
totten committed
207
       */
208
       public static function &import( $prefix = FALSE ) {ldelim}
209 210 211 212 213 214
            $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array(
            {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
              {if $foreign.import}'{$foreign.className}',{/if}
            {/foreach}{/if}
            ));
            return $r;
totten's avatar
totten committed
215 216 217
      {rdelim}

       /**
218 219 220 221 222 223
        * Returns the list of fields that can be exported
        *
        * @param bool $prefix
        *
        * @return array
        */
224
       public static function &export( $prefix = FALSE ) {ldelim}
225 226 227 228 229 230
            $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array(
            {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign}
              {if $foreign.export}'{$foreign.className}',{/if}
            {/foreach}{/if}
            ));
            return $r;
totten's avatar
totten committed
231 232
      {rdelim}

233 234
      /**
       * Returns the list of indices
235 236 237 238
       *
       * @param bool $localize
       *
       * @return array
239 240 241 242 243
       */
      public static function indices($localize = TRUE) {ldelim}
        $indices = {$indicesPhp};
        return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
      {rdelim}
244

totten's avatar
totten committed
245
{rdelim}