<?php
namespace Entity; class GdsProfileTemplate extends \App\Entity\GeneralAbstract { protected $saveInsideTransaction = true; protected function setTableName(): void { global $DBTGdsProfileTemplates; $this->tableName=$DBTGdsProfileTemplates; } protected function getAttribute($var) { switch ($var) { case 'addUser': { return User::select($this->fields['idAddUser'], $this->selectForUpdate); } case 'fieldsDetails': case 'staticFieldsDetails': case 'dynamicFieldsDetails': { if (!$this->fields[$var]) { global $DBTGdsProfileFields, $DBTGdsProfileTemplatesFields; $db = \DB::getInstance(); $sql = "
						SELECT f.id, f.category, f.label, f.description, f.type, f.inputType,
							f.possibleValues, f.validator, f.remarkVar, f.tagSabre, tf.mandatory
						FROM `{$DBTGdsProfileTemplatesFields}` AS tf
							INNER JOIN `{$DBTGdsProfileFields}` AS f ON tf.idProfileField=f.id
						WHERE tf.idProfileTemplate=?
							AND f.deleted=0
					"; $fieldsDetails = $db->GetAssoc($sql, [$this->id]); if ($db->ErrorNo()) { throw new \DBException('SQL error while getting some GDS profile templates.'); } foreach ($fieldsDetails as $idField=>$fieldDet) { $possibleValues = unserialize($fieldDet['possibleValues']); $validator = unserialize($fieldDet['validator']); $fieldsDetails[$idField]['possibleValues'] = ($possibleValues?:null); $fieldsDetails[$idField]['validator'] = ($validator?:null); } $this->fields['fieldsDetails'] = $fieldsDetails; $this->fields['staticFieldsDetails'] = array_filter(array_map(function($elm) { if ($elm['type']=='static') { return $elm; } }, $fieldsDetails)); $this->fields['dynamicFieldsDetails'] = array_filter(array_map(function($elm) { if ($elm['type']=='dynamic') { return $elm; } }, $fieldsDetails)); } return $this->fields[$var]; } default: { return parent::getAttribute($var); } } } protected function setAttribute($var, $value) { if ($this->operation!='insert' and $this->operation!='update') { throw new \Exception('You can call '.__METHOD__.' only for insert/update operations.'); } switch ($var) { case 'category': if ($this->operation != 'insert') { throw new \Exception("The field `{$var}` can only be set for insert operation"); } $this->fields[$var] = $value; break; case 'name': { $this->fields[$var]=$value; break; } case 'templateFields': if (!is_array($value)) { throw new \Exception('Profiles fields list can only be an array'); } $this->fields[$var] = $value; break; default: { throw new \Exception("The field '$var' is not valid for the `".__CLASS__."` entity."); } } } protected function save_filterSaveData(Array $saveData) { unset($saveData['templateFields']); return $saveData; } protected function save_postProcess(Array $saveData) { global $DBTGdsProfileTemplatesFields; if ( $this->operation=='insert' or $this->operation=='delete' or ($this->operation=='update' && $this->fields['templateFields']) ) { if (in_array($this->operation, ['delete', 'update'])) { $this->save_executeSql_delete($DBTGdsProfileTemplatesFields, 'idProfileTemplate', $this->id); } if (in_array($this->operation, ['insert', 'update'])) { foreach ($this->fields['templateFields'] as $idField=>$fieldDet) { $this->save_executeSql_insert($DBTGdsProfileTemplatesFields, ['idProfileTemplate'=>$this->id, 'idProfileField'=>$idField, 'mandatory'=>$fieldDet['mandatory']]); } } } } } 