PageRenderTime 78ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/FORM_DynTable.inc

https://bitbucket.org/joaoborsoi/avalanche
PHP | 2707 lines | 2214 code | 382 blank | 111 comment | 415 complexity | 1a185d6c9d288c8367261bdb43aab11d MD5 | raw file
  1. <?php
  2. require_once('LANG_Label.inc');
  3. require_once('FORM_Object.inc');
  4. require_once('AV_ModObject.inc');
  5. require_once('LIB_Document.inc');
  6. require_once('AV_Exception.inc');
  7. require_once('FORM_LexAnalyzer.inc');
  8. require_once('AV_ArrayObject.inc');
  9. require_once('AV_FaultTolerantFS.inc');
  10. //--Class: FORM_DynTable
  11. //--Desc: Manages dynamic tables. Dynamic table have property tables
  12. //--Desc: associated.
  13. class FORM_DynTable
  14. {
  15. protected $table; //--Desc: Table's name
  16. protected $dbDriver; //--Desc: Database driver handler
  17. protected $module; //--Desc: Module's handler
  18. protected $pageBuilder;
  19. protected $fieldsDef; //--Desc: Keeps dynamic table fields
  20. //--Desc: definition
  21. protected $keyFields;
  22. protected $oldValues; //--Desc: Array of old values kept
  23. //--Desc: after an update
  24. protected $formulaFieldsDef;
  25. protected $formulas;
  26. protected $formulaType;
  27. protected $lang;
  28. private $lockedField;
  29. //
  30. //--Public
  31. //
  32. function __construct($table, &$module)
  33. {
  34. $this->table = $table;
  35. $this->module = & $module;
  36. $this->lang = $module->pageBuilder->lang;
  37. $this->pageBuilder = & $this->module->pageBuilder;
  38. $this->dbDriver = & $module->dbDriver;
  39. $this->lockedField = NULL;
  40. $this->fieldsDef = NULL;
  41. $this->formulaFieldsDef = NULL;
  42. $this->keyFields = array();
  43. }
  44. function GetReadableFields()
  45. {
  46. // Find readble fields
  47. $SQL = 'SELECT fieldName FROM FORM_Field ';
  48. $SQL .= "WHERE (accessMode = 'r' OR accessMode = 'rw') ";
  49. $SQL .= "AND tableName ='". $this->table ."' AND ";
  50. $SQL .= "type <> 'selectMult' ";
  51. return $this->dbDriver->GetCol($SQL);
  52. }
  53. function LoadFieldsDef($disableFileRequired = false,$disableRequired = false)
  54. {
  55. // prevents fieldsdef reload
  56. if($this->fieldsDef != NULL)
  57. return $this->fieldsDef;
  58. // load fieldsdef from database
  59. $fieldsDef = $this->GetFieldsDef();
  60. // prepare fieldsDef
  61. foreach($fieldsDef as & $fieldDef)
  62. $this->ParseFieldDef($fieldDef,$disableFileRequired,$disableRequired);
  63. return $this->fieldsDef;
  64. }
  65. function Duplicate($sourceKey, $fields, $extraFields = array())
  66. {
  67. // do insertion
  68. $SQL = "INSERT INTO " . $this->table . " (";
  69. $first = true;
  70. foreach($this->fieldsDef as $field)
  71. {
  72. if ($field['keyField'] != 1)
  73. {
  74. if (strpos($field['accessMode'],'w')!==false &&
  75. ((isset($field['list']) && !$field['list']) ||
  76. !isset($field['list'])) &&
  77. $field['type'] != 'selectMult' &&
  78. $field['type'] != 'descr')
  79. {
  80. if(!$first)
  81. {
  82. $SQL .= ', ';
  83. $VALS .= ', ';
  84. }
  85. $first = false;
  86. if($field['type'] == 'file')
  87. {
  88. $SQL .= $field['fieldName'].'FileName, ';
  89. $SQL .= $field['fieldName'].'Type, ';
  90. $VALS .= $field['fieldName'].'FileName, ';
  91. $VALS .= $field['fieldName'].'Type, ';
  92. }
  93. $SQL .= $field['fieldName'];
  94. if(isset($fields[$field['fieldName']]))
  95. $VALS .= "'".$fields[$field['fieldName']]."'";
  96. else
  97. $VALS .= $field['fieldName'];
  98. }
  99. }
  100. else
  101. {
  102. if(isset($fields[$field['fieldName']]))
  103. {
  104. if(!$first)
  105. {
  106. $SQL .= ', ';
  107. $VALS .= ', ';
  108. }
  109. $first = false;
  110. $SQL .= $field['fieldName'];
  111. $VALS .= "'".$fields[$field['fieldName']]."'";
  112. }
  113. }
  114. }
  115. foreach($extraFields as $key => $field)
  116. {
  117. if(!$first)
  118. {
  119. $SQL .= ', ';
  120. $VALS .= ', ';
  121. }
  122. $first = false;
  123. $SQL .= $key;
  124. $VALS .= "$field";
  125. }
  126. $SQL .= ") SELECT " . $VALS;
  127. $SQL .= " FROM " . $this->table;
  128. $SQL .= " WHERE ".$sourceKey['fieldName']."='".$sourceKey['fieldValue']."'";
  129. $this->dbDriver->ExecSQL($SQL);
  130. foreach($this->fieldsDef as $field)
  131. {
  132. switch($field['type'])
  133. {
  134. case "selectMult":
  135. case "selectMultiple":
  136. $this->DuplicateOptions($sourceKey,$field);
  137. break;
  138. case "fileList":
  139. $this->DuplicateFileList($sourceKey,$field);
  140. break;
  141. case "emailList":
  142. $this->DuplicateEmailList($sourceKey,$field);
  143. break;
  144. case "langLabel":
  145. case "langTextArea":
  146. case "langHtmlArea":
  147. $this->DuplicateLabel($sourceKey,$field);
  148. break;
  149. }
  150. }
  151. }
  152. function Insert(& $fields, $extraFields = array())
  153. {
  154. // checks for violated fields
  155. $this->CheckIndexes($fields);
  156. // prevents locale problems inserting float fields
  157. setlocale(LC_ALL, 'en_US');
  158. // do insertion
  159. $SQL = "INSERT INTO " . $this->table . " (";
  160. $first = true;
  161. $fileFields = array();
  162. foreach($this->fieldsDef as $field)
  163. {
  164. if ($field['keyField'] != 1)
  165. {
  166. if($field['type'] == 'file' &&
  167. $fields[$field['fieldName']]['error']==UPLOAD_ERR_OK &&
  168. $fields[$field['fieldName']]['tmp_name'] != NULL)
  169. {
  170. $this->SetFile($field,$fields[$field['fieldName']],$extraFields);
  171. if($this->dbDriver->type=='oracle')
  172. $fileFields[] = $field['fieldName'];
  173. }
  174. if (strpos($field['accessMode'],'w')!==false &&
  175. ((isset($field['list']) && !$field['list']) ||
  176. !isset($field['list'])) &&
  177. $field['type'] != 'selectMult' &&
  178. $field['type'] != 'descr' &&
  179. $fields[$field['fieldName']] !== NULL &&
  180. $fields[$field['fieldName']] !== '')
  181. {
  182. if(!$first)
  183. {
  184. $SQL .= ', ';
  185. $VALS .= ', ';
  186. }
  187. $first = false;
  188. $SQL .= $field['fieldName'];
  189. if(($field['type'] == 'select' ||
  190. $field['type'] == 'readOnlySelect')
  191. && (! isset($fields[$field['fieldName']]) ||
  192. $fields[$field['fieldName']] === 0 ||
  193. $fields[$field['fieldName']] === NULL))
  194. $VALS .= 'NULL';
  195. else
  196. {
  197. if($field['maxValue']!=NULL && $fields[$field['fieldName']] > $field['maxValue'])
  198. $VALS .= "'" .$field['maxValue']. "'";
  199. elseif($field['minValue']!=NULL && $fields[$field['fieldName']] < $field['minValue'])
  200. $VALS .= "'" .$field['minValue']. "'";
  201. else
  202. $VALS .= "'" . $fields[$field['fieldName']]. "'";
  203. }
  204. }
  205. }
  206. else
  207. {
  208. $fieldKey = $field['fieldName'];
  209. }
  210. }
  211. foreach($extraFields as $key => $field)
  212. {
  213. if(!$first)
  214. {
  215. $SQL .= ', ';
  216. $VALS .= ', ';
  217. }
  218. $first = false;
  219. $SQL .= $key;
  220. $VALS .= "$field";
  221. }
  222. // reverts default locale
  223. setlocale(LC_ALL, $this->lang.'.utf8');
  224. $SQL .= ") VALUES (" . $VALS . ")";
  225. if($this->dbDriver->type=='oracle' && count($fileFields)>0)
  226. {
  227. foreach($fileFields as $key=>$file)
  228. {
  229. if($key>0)
  230. {
  231. $FIELDS .= ',';
  232. $VARS .= ',';
  233. }
  234. $FIELDS .= $file;
  235. $VARS .= ':'.$file;
  236. $this->dbDriver->BindBlob($file,$fields[$file]['content']);
  237. }
  238. $SQL .= 'RETURNING '.$FIELDS;
  239. $SQL .= ' INTO '.$VARS;
  240. }
  241. $this->dbDriver->ExecSQL($SQL);
  242. // determine key value
  243. if($fields[$fieldKey]!=NULL)
  244. $keyValue = $fields[$fieldKey];
  245. elseif($extraFields[$fieldKey]!=NULL)
  246. $keyValue = $extraFields[$fieldKey];
  247. else
  248. $keyValue = 'LAST_INSERT_ID()';
  249. foreach($this->fieldsDef as $field)
  250. {
  251. switch($field['type'])
  252. {
  253. case "selectMult":
  254. $this->SetOptions($fields[$field['fieldName']], $field,
  255. $keyValue,$fieldKey);
  256. break;
  257. case "selectMultiple":
  258. $this->SetSelectMultiple($fields[$field['fieldName']], $field,
  259. $keyValue,$fieldKey);
  260. break;
  261. case "fileList":
  262. $this->SetFileList($fields[$field['fieldName']],$field,
  263. $keyValue);
  264. break;
  265. case "objectList":
  266. $this->SetObjectList($fields[$field['fieldName']],$field,
  267. $keyValue,$fieldKey,true);
  268. break;
  269. case "emailList":
  270. $this->SetEmailList($fields[$field['fieldName']],$field,
  271. $keyValue);
  272. break;
  273. case 'schedule':
  274. $this->SetSchedule($fields[$field['fieldName']], $field,
  275. $keyValue,$fieldKey);
  276. break;
  277. case "langLabel":
  278. case "langTextArea":
  279. case "langHtmlArea":
  280. $this->SetLabel($field, $fields[$field['fieldName']], $keyValue,
  281. $fieldKey);
  282. break;
  283. }
  284. }
  285. }
  286. function Set($fields, $extraFields = array())
  287. {
  288. // checks for violated fields
  289. $this->CheckIndexes($fields);
  290. // prevents locale problems inserting float fields
  291. setlocale(LC_ALL, 'en_US');
  292. $blobToFS = $this->pageBuilder->siteConfig->getVar('admin','blobToFS');
  293. // do update operation
  294. $SQL = "UPDATE " . $this->table . " SET ";
  295. $WHERE .= ' WHERE ';
  296. $first = true;
  297. $firstWhere = true;
  298. $fileFields = array();
  299. foreach($this->fieldsDef as $field)
  300. {
  301. if($field['type'] == 'file' &&
  302. $fields[$field['fieldName']]['error']==UPLOAD_ERR_OK &&
  303. $fields[$field['fieldName']]['tmp_name'] != NULL)
  304. {
  305. $this->SetFile($field,$fields[$field['fieldName']],$extraFields);
  306. if($this->dbDriver->type=='oracle' || $blobToFS)
  307. $fileFields[] = $field['fieldName'];
  308. }
  309. if($field['keyField'] == '0')
  310. {
  311. if (strpos($field['accessMode'],'w')!==false &&
  312. ((isset($field['list']) && !$field['list']) ||
  313. !isset($field['list'])) &&
  314. $field['type'] != 'selectMult' &&
  315. $field['type'] != 'descr')
  316. {
  317. if($fields[$field['fieldName']] !== NULL)
  318. {
  319. if(!$first)
  320. $SQL .= ', ';
  321. else
  322. $first = false;
  323. $SQL .= $field['fieldName'] . " = ";
  324. if($fields[$field['fieldName']] === '')
  325. $SQL .= 'NULL';
  326. else
  327. {
  328. if($field['maxValue']!=NULL && $fields[$field['fieldName']] > $field['maxValue'])
  329. $SQL .= "'" .$field['maxValue']. "'";
  330. elseif($field['minValue']!=NULL && $fields[$field['fieldName']] < $field['minValue'])
  331. $SQL .= "'" .$field['minValue']. "'";
  332. else
  333. $SQL .= "'" . $fields[$field['fieldName']] . "'";
  334. }
  335. }
  336. }
  337. }
  338. else
  339. {
  340. $fieldKey = $field['fieldName'];
  341. $fieldName = $field['fieldName'];
  342. if(!$firstWhere)
  343. $WHERE .= ' AND ';
  344. else
  345. $firstWhere = false;
  346. $WHERE .= $fieldName . " = '" . $fields[$fieldName] . "'";
  347. $fieldKeyValue = $fields[$fieldName];
  348. } // if($field['keyField'] == 0)
  349. } // foreach($this->fieldsDef as $field)
  350. foreach($extraFields as $field => $value)
  351. {
  352. if(!$first)
  353. $SQL .= ', ';
  354. else
  355. $first = false;
  356. $SQL .= "$field = $value";
  357. }
  358. // reverts default locale
  359. setlocale(LC_ALL, $this->lang.'.utf8');
  360. // lock rows
  361. $LOCK = 'SELECT * FROM ' . $this->table . $WHERE . ' FOR UPDATE';
  362. $this->oldValues = $this->dbDriver->GetRow($LOCK);
  363. if(count($this->oldValues) == 0)
  364. throw new AV_Exception(NOT_FOUND, $this->lang);
  365. // do update
  366. if(!$first)
  367. {
  368. $SQL .= $WHERE;
  369. if($this->dbDriver->type=='oracle' && count($fileFields)>0 && !$blobToFS)
  370. {
  371. foreach($fileFields as $key=>$file)
  372. {
  373. if($key>0)
  374. {
  375. $FIELDS .= ',';
  376. $VARS .= ',';
  377. }
  378. $FIELDS .= $file;
  379. $VARS .= ':'.$file;
  380. $this->dbDriver->BindBlob($file,$fields[$file]['content']);
  381. }
  382. $SQL .= 'RETURNING '.$FIELDS;
  383. $SQL .= ' INTO '.$VARS;
  384. }
  385. $this->dbDriver->ExecSQL($SQL);
  386. }
  387. // mark old blob filesystem files to be deleted if they were updated
  388. if($blobToFS && count($fileFields)>0)
  389. {
  390. $SQL = "UPDATE LIB_BlobFS SET deleted='1' ";
  391. $SQL .= 'WHERE id IN (';
  392. $firstBlob = true;
  393. foreach($fileFields as $key=>$file)
  394. {
  395. if($firstBlob)
  396. $firstBlob = false;
  397. else
  398. $SQL .= ',';
  399. $SQL .= "'".$this->oldValues[$file]."'";
  400. }
  401. $SQL .= ')';
  402. $this->dbDriver->ExecSQL($SQL);
  403. }
  404. foreach($this->fieldsDef as $field)
  405. {
  406. switch($field['type']){
  407. case "selectMult":
  408. $this->SetOptions($fields[$field['fieldName']], $field,
  409. $fieldKeyValue, $fieldKey);
  410. break;
  411. case "selectMultiple":
  412. $this->SetSelectMultiple($fields[$field['fieldName']], $field,
  413. $fieldKeyValue, $fieldKey);
  414. break;
  415. case "fileList":
  416. $this->oldValues[$field['fieldName']] =
  417. $this->SetFileList($fields[$field['fieldName']],$field,
  418. $fieldKeyValue);
  419. break;
  420. case "objectList":
  421. $this->oldValues[$field['fieldName']] =
  422. $this->SetObjectList($fields[$field['fieldName']],$field,
  423. $fieldKeyValue,$fieldKey,false);
  424. break;
  425. case "emailList":
  426. $this->oldValues[$field['fieldName']] =
  427. $this->SetEmailList($fields[$field['fieldName']],$field,
  428. $fieldKeyValue);
  429. break;
  430. case 'schedule':
  431. $this->SetSchedule($fields[$field['fieldName']], $field,
  432. $fieldKeyValue,$fieldKey);
  433. break;
  434. case "langLabel":
  435. case "langTextArea":
  436. case "langHtmlArea":
  437. $this->SetLabel($field, $fields[$field['fieldName']],
  438. $fieldKeyValue, $fieldKey);
  439. break;
  440. }
  441. }
  442. return $this->oldValues;
  443. }
  444. function Del($fields)
  445. {
  446. // do update operation
  447. $SQL = "DELETE FROM " . $this->table . " WHERE ";
  448. $first = true;
  449. foreach($this->fieldsDef as $field)
  450. {
  451. if($field['keyField'] != '0')
  452. {
  453. $fieldName = $field['fieldName'];
  454. if(!$first)
  455. $SQL .= 'AND ';
  456. else
  457. $first = false;
  458. $SQL .= $fieldName . " = '" . $fields[$fieldName] . "' ";
  459. }
  460. }
  461. if($first)
  462. throw new AV_Exception(FAILED, $this->lang);
  463. // does delete operation
  464. $this->dbDriver->ExecSQL($SQL);
  465. }
  466. function GetForm($keyFields, $fieldName, $sourceId, & $templates,
  467. & $formObject, $defValues = array(),$dateFormat = NULL,
  468. $numberFormat = true, $short = false)
  469. {
  470. // load formula defs
  471. $this->LoadFormulaDefs();
  472. foreach($this->formulaFieldsDef as $value)
  473. {
  474. $fields[$value['fieldName']] = $this->ConvertIf($value['expression']);
  475. $this->formulaType[$value['fieldName']] = $value['formulaType'];
  476. }
  477. $parms['locale'] = localeconv();
  478. foreach($fields as $field => $expression)
  479. $this->formulas[$field] = $this->Decompose($this->formulas, $expression,
  480. $fields,$parms);
  481. $this->LoadFieldsDef();
  482. if($keyFields != NULL)
  483. $values = $this->Get($keyFields);
  484. else
  485. $values = $this->GetDefValues();
  486. $values = ArrayObjectMerge($defValues, $values);
  487. $langLabel = new LANG_Label($this->module);
  488. foreach ($this->fieldsDef as $key=>$value)
  489. {
  490. if($fieldName == '' || $fieldName == $value['fieldName'] || strstr($fieldName,'.',true)==$value['fieldName'])
  491. {
  492. // desabilita template da base em caso de carregamento de campo único
  493. if($fieldName == $value['fieldName'])
  494. unset($this->fieldsDef[$key]['template']);
  495. // ajusta label para todos os campos
  496. if($value['labelId']!=NULL)
  497. $this->fieldsDef[$key]['label'] =
  498. $langLabel->GetLabel($value['labelId'],
  499. $this->lang);
  500. // ajusta outros labels do campo
  501. $this->SetFieldLabels($this->fieldsDef[$key]);
  502. // ajusta xhtmlRequired para todos os campos
  503. if($value['required'])
  504. {
  505. $this->fieldsDef[$key]['xhtmlRequired'] = "class='required'";
  506. $this->fieldsDef[$key]['required'] = "required";
  507. }
  508. else
  509. unset($this->fieldsDef[$key]['required']);
  510. // desabilita campo caso não seja para escrita
  511. if(strpos($value['accessMode'],'w')==false)
  512. $this->fieldsDef[$key]['xhtmlDisabled'] = "disabled='disabled'";
  513. // ajusta versão do avalanche para uso nos campos
  514. $this->fieldsDef[$key]['avVersion'] = $this->pageBuilder->avVersion;
  515. $child = & $this->GetField($this->fieldsDef[$key],$values,$keyFields,
  516. $templates,$sourceId,$dateFormat,
  517. $numberFormat,$fieldName,$short);
  518. if($child != NULL)
  519. {
  520. $formObject->children[$value['orderby']] = & $child;
  521. $formObject->attrs[$value['fieldName']] = $child->attrs['value'];
  522. if($short)
  523. unset($child->attrs['value']);
  524. }
  525. else if(isset($values[$value['fieldName']]))
  526. {
  527. $formObject->attrs[$value['fieldName']] = $values[$value['fieldName']];
  528. }
  529. }
  530. }
  531. return $formObject;
  532. }
  533. function Get($keyFields)
  534. {
  535. $where = NULL;
  536. $select = NULL;
  537. foreach($this->fieldsDef as $key => $value)
  538. {
  539. if($value['type'] != 'selectMult' &&
  540. $value['type'] != 'descr' &&
  541. strpos($value['accessMode'],'r')!==false)
  542. {
  543. if($select != NULL)
  544. $select .= ',';
  545. $select .= $value['fieldName'];
  546. }
  547. if($value['keyField'] == 1)
  548. {
  549. if (!isset($keyFields[$value['fieldName']]))
  550. throw new AV_Exception(INVALID_FIELD, $this->lang,
  551. Array('fieldName'=>$value['fieldName'],
  552. 'table'=>$this->table,
  553. 'addMsg'=>' ('.$value['fieldName']. ')'));
  554. if($where != NULL)
  555. $where .= ' AND ';
  556. $where .= $value['fieldName']. " = '";
  557. $where .= $keyFields[$value['fieldName']]. "'";
  558. }
  559. }
  560. $from = $this->table;
  561. // no fields to be returned
  562. if($select == NULL)
  563. return NULL;
  564. if($where == NULL)
  565. {
  566. throw new AV_Exception(INVALID_FIELD, $this->lang,
  567. Array('fieldName'=>'keyFields',
  568. 'table'=>$this->table,
  569. 'addMsg'=>' (keyFields)'));
  570. }
  571. $SQL = ' SELECT ' .$select. ' FROM ' .$from. ' WHERE ' ;
  572. $SQL .= $where. ' LOCK IN SHARE MODE';
  573. // Executes SQL statement
  574. $values = $this->dbDriver->GetRow($SQL);
  575. if($values==NULL)
  576. {
  577. throw new AV_Exception(NOT_FOUND, $this->lang,
  578. Array('table'=>$this->table));
  579. }
  580. return $values;
  581. }
  582. function GetDefValues()
  583. {
  584. try
  585. {
  586. $desc = $this->dbDriver->GetAll('DESC '.$this->table);
  587. $values = array();
  588. foreach($desc as $field)
  589. {
  590. if($field['Default']!==NULL && $field['Default']!=='' &&
  591. !in_array($field['Field'],$this->keyFields))
  592. $values[$field['Field']] = $field['Default'];
  593. }
  594. return $values;
  595. }
  596. catch(Exception $e)
  597. {
  598. // table may not exist (search fields form)
  599. return array();
  600. }
  601. }
  602. function GetAll($orderBy = NULL)
  603. {
  604. $returnFields = $this->GetReadableFields();
  605. if(count($returnFields)==0)
  606. throw new AV_Exception(FAILED, $this->lang);
  607. $returnFields = implode(',', $returnFields);
  608. $SQL = "SELECT $returnFields FROM ".$this->table.' ';
  609. if($orderBy != NULL)
  610. $SQL .= "ORDER BY $orderBy ";
  611. $SQL .= 'LOCK IN SHARE MODE';
  612. return $this->dbDriver->GetAll($SQL);
  613. }
  614. function AddField($fieldName, $dbType, $labels, $type, $size, $prevField,
  615. $consistType, $required, $allowNull,$accessMode)
  616. {
  617. if($prevField != '')
  618. {
  619. $SQL = 'SELECT orderby FROM FORM_Field WHERE ';
  620. $SQL .= "tableName='".$this->table."' AND fieldName = '$prevField' ";
  621. $SQL .= 'LOCK IN SHARE MODE ';
  622. $order = $this->dbDriver->GetOne($SQL);
  623. if($order == NULL)
  624. throw new AV_Exception(INVALID_FIELD, $this->lang);
  625. $SQL = 'SELECT orderby from FORM_Field WHERE ';
  626. $SQL .= "tableName='".$this->table."' AND orderby > $order ";
  627. $SQL .= 'FOR UPDATE ';
  628. $this->dbDriver->GetCol($SQL);
  629. $SQL = 'UPDATE FORM_Field SET orderby=orderby+1 WHERE ';
  630. $SQL .= "tableName='".$this->table."' AND orderby > $order";
  631. $this->dbDriver->ExecSQL($SQL);
  632. $order++;
  633. }
  634. else
  635. $order = 1;
  636. $labelMan = new LANG_Label($this->module);
  637. $labelId = $this->table . '_' . $fieldName;
  638. $labelMan->SetLabel($labelId, $labels);
  639. $SQL = 'INSERT INTO FORM_Field ';
  640. $SQL .= '(tableName,fieldName,labelId,type,size,orderby,';
  641. $SQL .= 'consistType,required,allowNull,trimField,';
  642. $SQL .= 'uniq,keyField,accessMode,staticProp) VALUES ';
  643. $SQL .= "('".$this->table."','$fieldName','$labelId','$type',$size,";
  644. $SQL .= "$order,'$consistType','$required','$allowNull','1',";
  645. $SQL .= "'0','0','$accessMode','0')";
  646. $this->dbDriver->ExecSQL($SQL);
  647. // clears fieldsDef to force new load
  648. $this->fieldsDef = NULL;
  649. $SQL = 'ALTER TABLE ' . $this->table;
  650. $SQL .= " ADD $fieldName $dbType;";
  651. return $SQL;
  652. }
  653. function ChangeField($newName, $dbType, $labels, $type, $size,
  654. $consistType, $required, $allowNull,$accessMode)
  655. {
  656. if($this->lockedField==NULL)
  657. throw new AV_Exception(FAILED, $this->lang);
  658. if($this->lockedField['staticProp']=='1')
  659. throw new AV_Exception(PERMISSION_DENIED, $this->lang);
  660. $SQL = NULL;
  661. if($labels != NULL)
  662. {
  663. $labelMan = new LANG_Label($this->module);
  664. $labelId = $this->table . '_' . $this->lockedField['fieldName'];
  665. $labelMan->SetLabel($labelId, $labels);
  666. $SQL = "labelId='$labelId'";
  667. }
  668. if($type != NULL)
  669. {
  670. if($SQL != NULL)
  671. $SQL .= ',';
  672. $SQL .= "type='$type'";
  673. }
  674. if($size != NULL)
  675. {
  676. if($SQL != NULL)
  677. $SQL .= ',';
  678. $SQL .= "size=$size";
  679. }
  680. if($consistType != NULL)
  681. {
  682. if($SQL != NULL)
  683. $SQL .= ',';
  684. $SQL .= "consistType='$consistType'";
  685. }
  686. if($required != NULL)
  687. {
  688. if($SQL != NULL)
  689. $SQL .= ',';
  690. $SQL .= "required='$required'";
  691. }
  692. if($allowNull != NULL)
  693. {
  694. if($SQL != NULL)
  695. $SQL .= ',';
  696. $SQL .= "allowNull='$allowNull'";
  697. }
  698. if($accessMode != NULL)
  699. {
  700. if($SQL != NULL)
  701. $SQL .= ',';
  702. $SQL .= "accessMode='$accessMode'";
  703. }
  704. if($SQL != NULL)
  705. {
  706. $SQL = "UPDATE FORM_Field SET $SQL WHERE ";
  707. $SQL .= "tableName='".$this->table."' AND ";
  708. $SQL .= "fieldName='".$this->lockedField['fieldName']."' ";
  709. $this->dbDriver->ExecSQL($SQL);
  710. // clears fieldsDef to force new load
  711. $this->fieldsDef = NULL;
  712. }
  713. if($newName != NULL && $dbType != NULL)
  714. {
  715. $SQL = 'ALTER TABLE ' . $this->table;
  716. $SQL .= " CHANGE ".$this->lockedField['fieldName']." $newName $dbType;";
  717. $this->lockedField = NULL;
  718. return $SQL;
  719. }
  720. else if($newName != NULL || $dbType != NULL)
  721. throw new AV_Exception(INVALID_FIELD, $this->lang);
  722. $this->lockedField = NULL;
  723. return NULL;
  724. }
  725. function DropField($fieldName)
  726. {
  727. $SQL = 'DELETE FROM FORM_Field WHERE ';
  728. $SQL .= "tableName='".$this->table."' AND fieldName='$fieldName' ";
  729. $this->dbDriver->ExecSQL($SQL);
  730. // clears fieldsDef to force new load
  731. $this->fieldsDef = NULL;
  732. $SQL = 'ALTER TABLE ' . $this->table;
  733. $SQL .= " DROP $fieldName";
  734. return $SQL;
  735. }
  736. function SetOrder($fieldName, $up)
  737. {
  738. $SQL = 'SELECT MAX(orderby) FROM FORM_Field WHERE ';
  739. $SQL .= "tableName = '" . $this->table . "' ";
  740. $SQL .= "LOCK IN SHARE MODE";
  741. $last = $this->dbDriver->GetOne($SQL);
  742. if($last == NULL)
  743. throw new AV_Exception(FAILED, $this->lang);
  744. $SQL = 'SELECT orderby FROM FORM_Field WHERE ';
  745. $SQL .= "tableName = '" . $this->table . "' AND fieldName = '$fieldName' ";
  746. $SQL .= "FOR UPDATE";
  747. $order = $this->dbDriver->GetOne($SQL);
  748. if($order == NULL)
  749. return;
  750. //up ou down
  751. if($up)
  752. {
  753. if($order == 1)
  754. return;
  755. $nextOrder = $order-1;
  756. }
  757. else
  758. {
  759. if($last == $order)
  760. return;
  761. $nextOrder = $order+1;
  762. }
  763. $SQL = 'SELECT orderby FROM FORM_Field WHERE ';
  764. $SQL .= "tableName = '" . $this->table . "' AND orderby = $nextOrder ";
  765. $SQL .= "FOR UPDATE";
  766. $nextOrder = $this->dbDriver->GetOne($SQL);
  767. if($nextOrder == NULL)
  768. throw new AV_Exception(FAILED, $this->lang);
  769. $SQL = "UPDATE FORM_Field SET orderby=$order WHERE ";
  770. $SQL .= "tableName = '" . $this->table . "' AND orderby = $nextOrder ";
  771. $this->dbDriver->ExecSQL($SQL);
  772. $SQL = "UPDATE FORM_Field SET orderby=$nextOrder WHERE ";
  773. $SQL .= "tableName = '" . $this->table . "' AND fieldName = '$fieldName' ";
  774. $this->dbDriver->ExecSQL($SQL);
  775. // clears fieldsDef to force new load
  776. $this->fieldsDef = NULL;
  777. }
  778. function GetFieldInfo($fieldName)
  779. {
  780. $SQL = 'SELECT labelId,type,template,langTemplate,size,orderby,';
  781. $SQL .= 'consistType,required,allowNull ';
  782. $SQL .= 'FROM FORM_Field WHERE ';
  783. $SQL .= "tableName = '" . $this->table . "' AND fieldName = '$fieldName' ";
  784. $SQL .= 'LOCK IN SHARE MODE ';
  785. $field = $this->dbDriver->GetRow($SQL);
  786. if(count($field) == 0)
  787. throw new AV_Exception(NOT_FOUND, $this->lang);
  788. $labelMan = new LANG_Label($this->module);
  789. $labelList = $labelMan->GetLabelList($field['labelId']);
  790. foreach($labelList as $label)
  791. $field['descr_'.$label['lang']] = $label['value'];
  792. return $field;
  793. }
  794. function GetNextFieldValue($fieldName, $prefix)
  795. {
  796. $offset = mb_strlen($prefix) + 1;
  797. $SQL = "SELECT MAX(SUBSTR($fieldName,$offset)+0) ";
  798. $SQL .= 'FROM '. $this->table .' WHERE ';
  799. $SQL .= "$fieldName like '$prefix%' ";
  800. $SQL .= 'LOCK IN SHARE MODE';
  801. $num = $this->dbDriver->GetOne($SQL);
  802. if($num == NULL)
  803. return $prefix.'1';
  804. return $prefix . ++$num;
  805. }
  806. function GetSelectFieldInfo($fieldName)
  807. {
  808. $SQL = 'SELECT * FROM FORM_SelectField WHERE ';
  809. $SQL .= "tableName = '".$this->table."' AND fieldName = '$fieldName' ";
  810. $SQL .= 'LOCK IN SHARE MODE ';
  811. return $this->dbDriver->GetRow($SQL);
  812. }
  813. //
  814. //--Protected
  815. //
  816. protected function ParseFieldDef(& $fieldDef,$disableFileRequired,$disableRequired)
  817. {
  818. $key = $fieldDef['orderby'];
  819. $this->fieldsDef[$key] = & $fieldDef;
  820. // sets default stripSlashes to false
  821. $this->fieldsDef[$key]['stripSlashes'] = false;
  822. if($disableRequired)
  823. $fieldDef['required'] = '0';
  824. if($fieldDef['keyField']=='1')
  825. $this->keyFields[] = $fieldDef['fieldName'];
  826. if($fieldDef['attributes'] != NULL)
  827. $fieldDef['attributes'] = json_decode($fieldDef['attributes'],true);
  828. switch($fieldDef['type'])
  829. {
  830. // sets langLabel field as list
  831. case 'langLabel':
  832. case "langTextArea":
  833. case "langHtmlArea":
  834. $this->fieldsDef[$key]['list'] = true;
  835. $this->fieldsDef[$key]['sizeVarName'] = $fieldDef['fieldName'].
  836. '_numOfOptions';
  837. $subfieldDef = array();
  838. $subfieldDef[0]['fieldName'] = 'lang';
  839. $subfieldDef[0]['required'] = $fieldDef['required'];
  840. $subfieldDef[0]['allowNull'] = $fieldDef['allowNull'];
  841. $subfieldDef[0]['stripSlashes'] = false;
  842. $subfieldDef[1]['fieldName'] = 'value';
  843. $subfieldDef[1]['required'] = $fieldDef['required'];
  844. $subfieldDef[1]['allowNull'] = $fieldDef['allowNull'];
  845. $subfieldDef[1]['stripSlashes'] = false;
  846. $subfieldDef[1]['consistType'] = $fieldDef['consistType'];
  847. $this->fieldsDef[$key]['fieldsDef'] = $subfieldDef;
  848. break;
  849. case 'schedule':
  850. $this->fieldsDef[$key]['list'] = true;
  851. $this->fieldsDef[$key]['required'] = $fieldDef['required'];
  852. $this->fieldsDef[$key]['sizeVarName'] = $fieldDef['fieldName'].
  853. '_numOfOptions';
  854. $subfieldDef = array();
  855. $subfieldDef[0]['fieldName'] = 'start';
  856. $subfieldDef[0]['required'] = false;
  857. $subfieldDef[0]['allowNull'] = true;
  858. $subfieldDef[0]['consistType'] = 'datetime';
  859. $subfieldDef[1]['fieldName'] = 'numOfHours';
  860. $subfieldDef[1]['required'] = false;
  861. $subfieldDef[1]['allowNull'] = true;
  862. $subfieldDef[1]['consistType'] = 'integer';
  863. $this->fieldsDef[$key]['fieldsDef'] = $subfieldDef;
  864. break;
  865. case 'selectMult':
  866. $this->fieldsDef[$key]['list'] = true;
  867. $this->fieldsDef[$key]['sizeVarName'] = $fieldDef['fieldName'].
  868. '_numOfOptions';
  869. $subfieldDef = array();
  870. $subfieldDef[0]['fieldName'] = 'value';
  871. $subfieldDef[0]['required'] = false;
  872. $subfieldDef[0]['allowNull'] = true;
  873. $subfieldDef[0]['consistType'] = 'integer';
  874. $this->fieldsDef[$key]['fieldsDef'] = $subfieldDef;
  875. break;
  876. case 'selectMultiple':
  877. // gambiarra pra evitar addslashes
  878. $this->fieldsDef[$key]['stripSlashes'] = true;
  879. break;
  880. case 'fileList':
  881. $subfieldDef = array();
  882. $subfieldDef[0]['fieldName'] = 'docId';
  883. $subfieldDef[0]['required'] = true;
  884. $subfieldDef[0]['allowNull'] = false;
  885. $subfieldDef[0]['consistType'] = 'integer';
  886. $this->fieldsDef[$key]['fieldsDef'] = $subfieldDef;
  887. // gambiarra pra evitar addslashes
  888. $this->fieldsDef[$key]['stripSlashes'] = true;
  889. break;
  890. case 'objectList':
  891. $subfields = $this->LoadObjectListDefs($fieldDef);
  892. $this->fieldsDef[$key]['fieldsDef'] = $subfields;
  893. // gambiarra pra evitar addslashes
  894. $this->fieldsDef[$key]['stripSlashes'] = true;
  895. break;
  896. case 'file':
  897. // gambiarra pra evitar addslashes
  898. $this->fieldsDef[$key]['stripSlashes'] = true;
  899. if($disableFileRequired)
  900. $this->fieldsDef[$key]['required'] = '0';
  901. $this->fieldsDef[$key]['allowNull'] = '1';
  902. break;
  903. case 'emailList':
  904. // gambiarra pra evitar addslashes
  905. $this->fieldsDef[$key]['stripSlashes'] = true;
  906. }
  907. }
  908. protected function LoadObjectListDefs($fieldDef)
  909. {
  910. $data = $this->GetObjectListInfo($fieldDef);
  911. $libDoc = new LIB_Document($this->module);
  912. $fieldsDef = $libDoc->LoadFieldsDef(NULL, $data['path']);
  913. $excludeFields = array('userId','groupId','userRight','groupRight',
  914. 'otherRight','creationDate','lastChanged',
  915. 'lastChangedUserId','keywords');
  916. foreach($fieldsDef as $key => &$field)
  917. if(in_array($field['fieldName'],$excludeFields))
  918. unset($fieldsDef[$key]);
  919. return $fieldsDef;
  920. }
  921. protected function GetObjectListInfo($fieldDef)
  922. {
  923. $SQL = 'SELECT path, ol.folderId, parentIdField ';
  924. $SQL .= 'FROM FORM_ObjectListField ol, LIB_Folder f ';
  925. $SQL .= "WHERE tableName='".$this->table."' ";
  926. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  927. $SQL .= 'AND ol.folderId=f.folderId ';
  928. $data = $this->dbDriver->GetRow($SQL);
  929. if(count($data) == NULL)
  930. throw new AV_Exception(NOT_FOUND, $this->lang);
  931. return $data;
  932. }
  933. protected function GetFieldsDef()
  934. {
  935. $SQL = 'SELECT labelId,fieldName,type,template,langTemplate,outputFuncRef,';
  936. $SQL .= 'size,attributes,consistType,`maxValue`,minValue,allowNull,trimField,required,uniq,';
  937. $SQL .= 'staticProp,accessMode, keyField, orderby ';
  938. $SQL .= 'FROM FORM_Field ';
  939. $SQL .= "WHERE tableName ='". $this->table;
  940. $SQL .= "' ORDER BY orderby";
  941. $SQL .= ' LOCK IN SHARE MODE ';
  942. // Executes SQL statement
  943. return $this->dbDriver->GetAll($SQL);
  944. }
  945. protected function GetTemplate($templates, $fieldName)
  946. {
  947. if(is_array($templates[$fieldName]))
  948. return $templates[$fieldName]['fileName'];
  949. else
  950. return $templates[$fieldName];
  951. }
  952. protected function &GetField(& $fieldDef, & $values, $keyFields,
  953. & $templates, $sourceId, $dateFormat = NULL,
  954. $numberFormat = true, $fieldName = NULL,
  955. $short = false)
  956. {
  957. switch ($fieldDef['type'])
  958. {
  959. case "langLabel":
  960. case "langTextArea":
  961. case "langHtmlArea":
  962. return $this->GetLangField($fieldDef, $values, $templates);
  963. case 'descr':
  964. case "password":
  965. case 'textArea':
  966. case "text":
  967. case 'hidden':
  968. case 'integer':
  969. case 'dateFilter':
  970. case 'currencyFilter':
  971. case 'integerFilter':
  972. return $this->GetTextField($fieldDef, $values);
  973. case 'authSecret':
  974. return $this->GetAuthSecretField($fieldDef, $values);
  975. case 'htmlArea':
  976. if($short)
  977. return $this->GetTextField($fieldDef, $values);
  978. return $this->GetHtmlField($fieldDef, $values);
  979. case 'boolean':
  980. return $this->GetBooleanField($fieldDef, $values);
  981. case "accessRight":
  982. return $this->GetAccessRightField($fieldDef, $values);
  983. case "date":
  984. case "datetime":
  985. return $this->GetDateField($fieldDef, $values, $dateFormat);
  986. case "cpf":
  987. return $this->GetCpfField($fieldDef, $values);
  988. case "cnpj":
  989. return $this->GetCnpjField($fieldDef, $values);
  990. case "cep":
  991. return $this->GetCepField($fieldDef, $values);
  992. case 'schedule':
  993. return $this->GetScheduleField($fieldDef, $values, $keyFields);
  994. case "select":
  995. case "radio":
  996. return $this->GetSelectField($fieldDef, $values, $keyFields,
  997. $templates, $sourceId);
  998. case 'readOnlySelect':
  999. return $this->GetReadOnlySelectField($fieldDef, $values);
  1000. case "selectMult":
  1001. case "selectMultiple":
  1002. return $this->GetSelectMultField($fieldDef, $values, $keyFields,
  1003. $templates, $sourceId);
  1004. case 'float':
  1005. case 'currency':
  1006. return $this->GetFloatField($fieldDef, $values, $numberFormat);
  1007. case 'percent':
  1008. return $this->GetPercentField($fieldDef, $values, $numberFormat);
  1009. case 'file':
  1010. return $this->GetFileField($fieldDef, $values);
  1011. case 'usageTerms':
  1012. return $this->GetUsageTermsField($fieldDef, $values);
  1013. case 'legalEntityRef':
  1014. return $this->GetLEOrNPEntityRef($fieldDef, $values,
  1015. 'legalEntity');
  1016. case 'naturalPersonRef':
  1017. return $this->GetLEOrNPEntityRef($fieldDef, $values,
  1018. 'naturalPerson');
  1019. case 'leOrNpEntityRef':
  1020. return $this->GetLEOrNPEntityRef($fieldDef, $values, NULL);
  1021. case 'fileList':
  1022. return $this->GetFileListField($fieldDef,$keyFields,$templates);
  1023. case 'objectList':
  1024. return $this->GetObjectListField($fieldDef,$keyFields,$dateFormat,
  1025. $numberFormat,$fieldName,$short);
  1026. case 'emailList':
  1027. return $this->GetEmailListField($fieldDef,$keyFields,$templates);
  1028. case 'folder':
  1029. return $this->GetFolderField($fieldDef, $values);
  1030. case 'formula':
  1031. return $this->GetFormulaField($fieldDef, $values);
  1032. default:
  1033. return NULL;
  1034. }
  1035. }
  1036. //--Method: GetLangLabel
  1037. protected function GetLangField(& $fieldDef, & $values, & $templates)
  1038. {
  1039. // retrieves all registered languages
  1040. $langLabel = new LANG_Label($this->module);
  1041. $langList = $langLabel->GetLangList();
  1042. // creates lang field
  1043. $langField = new FORM_Object('field', $this->pageBuilder,
  1044. $templates, 'nextLangField');
  1045. $langField->attrs['fieldName'] = $fieldDef['fieldName'];
  1046. $langField->attrs['numOfOptions'] = count($langList);
  1047. $langField->attrs['type'] = 'langField';
  1048. // creates lang subfields
  1049. foreach($langList as $key => $lang)
  1050. {
  1051. $field = new AV_ModObject('field', $this->pageBuilder);
  1052. // fieldDef pode ser AV_OCIArray no caso de conexão oracle
  1053. if(gettype($fieldDef)=='object')
  1054. $field->attrs = clone $fieldDef;
  1055. else
  1056. $field->attrs = $fieldDef;
  1057. $field->attrs['lang'] = $lang['lang'];
  1058. $field->attrs['langLabel'] = mb_substr($lang['lang'],0,2);
  1059. $field->attrs['fieldIndex'] = $key+1;
  1060. $keys[$lang['lang']] = $key;
  1061. $langField->children[$key] = $field;
  1062. // initiate new html to have at least a <p>&nbsp;</p>
  1063. if($fieldDef['type'] == 'langHtmlArea')
  1064. $field->attrs['value'] = "<p>&nbsp;</p>";
  1065. }
  1066. // set's lang subfields values
  1067. $list = $langLabel->GetLabelList($values[$fieldDef['fieldName']],
  1068. $fieldDef['type']!='langLabel');
  1069. foreach($list as $key=>$value)
  1070. {
  1071. $langField->children[$keys[$value['lang']]]->attrs['value'] =
  1072. $value['value'];
  1073. if($value['lang'] == $this->lang)
  1074. $langField->attrs['value'] = $value['value'];
  1075. }
  1076. return $langField;
  1077. }
  1078. //--Method: GetTextField
  1079. protected function &GetTextField(& $fieldDef, $values)
  1080. {
  1081. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1082. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1083. $modobj->attrs = $fieldDef;
  1084. return $modobj;
  1085. }
  1086. protected function &GetAuthSecretField(& $fieldDef, $values)
  1087. {
  1088. global $SM_siteID;
  1089. $ga = new PHPGangsta_GoogleAuthenticator();
  1090. if($values[$fieldDef['fieldName']]==NULL)
  1091. $fieldDef['value'] = $ga->createSecret();
  1092. else
  1093. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1094. $name = $SM_siteID;
  1095. $fieldDef['qrCodeUrl'] = $ga->getQRCodeGoogleUrl($SM_siteID.'-'.$values['login'],
  1096. $fieldDef['value']);
  1097. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1098. $modobj->attrs = $fieldDef;
  1099. return $modobj;
  1100. }
  1101. //--Method: GetTextField
  1102. protected function GetHtmlField(& $fieldDef, $values)
  1103. {
  1104. // initiate new html to have at least a <p>&nbsp;</p>
  1105. if($values[$fieldDef['fieldName']] == NULL)
  1106. $values[$fieldDef['fieldName']] = "<p>&nbsp;</p>";
  1107. return $this->GetTextField($fieldDef,$values);
  1108. }
  1109. //--Method: GetTextField
  1110. protected function &GetBooleanField(& $fieldDef, $values)
  1111. {
  1112. if($values[$fieldDef['fieldName']])
  1113. $fieldDef['xhtmlCheckedAttr'] = "checked='checked'";
  1114. return $this->GetTextField($fieldDef, $values);
  1115. }
  1116. //--Method: GetTextField
  1117. protected function &GetPercentField(& $fieldDef, $values, $numberFormat)
  1118. {
  1119. if($numberFormat)
  1120. $values[$fieldDef['fieldName']] *= 100;
  1121. return $this->GetFloatField($fieldDef, $values,$numberFormat);
  1122. }
  1123. //--Method: GetTextField
  1124. protected function &GetFloatField(& $fieldDef, $values, $numberFormat)
  1125. {
  1126. if($numberFormat)
  1127. {
  1128. $locale = localeconv();
  1129. if(isset($values[$fieldDef['fieldName']]))
  1130. {
  1131. $fieldDef['value'] = number_format($values[$fieldDef['fieldName']],
  1132. $locale['frac_digits'],
  1133. $locale['mon_decimal_point'],
  1134. $locale['mon_thousands_sep']);
  1135. }
  1136. }
  1137. else
  1138. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1139. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1140. $modobj->attrs = $fieldDef;
  1141. return $modobj;
  1142. }
  1143. protected function GetAccessRightField(& $fieldDef, $values)
  1144. {
  1145. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1146. $fieldDef['read'] = (strpos($fieldDef['value'],'r')===false)?'0':'1';
  1147. $fieldDef['write'] = (strpos($fieldDef['value'],'w')===false)?'0':'1';
  1148. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1149. $modobj->attrs = $fieldDef;
  1150. return $modobj;
  1151. }
  1152. protected function &GetDateField(& $fieldDef, $values, $dateFormat)
  1153. {
  1154. if($values[$fieldDef['fieldName']] != 0)
  1155. {
  1156. $timestamp = strtotime($values[$fieldDef['fieldName']]);
  1157. if($dateFormat != NULL)
  1158. {
  1159. $fieldDef['value'] = strftime($dateFormat,$timestamp);
  1160. $fieldDef['valueUTS'] = $values[$fieldDef['fieldName']];
  1161. }
  1162. else
  1163. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1164. $fieldDef['year'] = date('Y',$timestamp);
  1165. $fieldDef['month'] = date('m',$timestamp);
  1166. $fieldDef['day'] = date('d',$timestamp);
  1167. $fieldDef['hour'] = date('H',$timestamp);
  1168. $fieldDef['minute'] = date('i',$timestamp);
  1169. $fieldDef['second'] = date('s',$timestamp);
  1170. }
  1171. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1172. $modobj->attrs = $fieldDef;
  1173. return $modobj;
  1174. }
  1175. //--Method: GetCpfField
  1176. protected function &GetCpfField(& $fieldDef, $values)
  1177. {
  1178. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1179. $fieldDef['cpf_1'] = mb_substr($values[$fieldDef['fieldName']],0,-2);
  1180. $fieldDef['cpf_2'] = mb_substr($values[$fieldDef['fieldName']],-2);
  1181. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1182. $modobj->attrs = $fieldDef;
  1183. return $modobj;
  1184. }
  1185. //--Method: GetCnpjField
  1186. protected function &GetCnpjField(& $fieldDef, $values)
  1187. {
  1188. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1189. $fieldDef['cnpj_1'] = mb_substr($values[$fieldDef['fieldName']],0,-6);
  1190. $fieldDef['cnpj_2'] = mb_substr($values[$fieldDef['fieldName']],-6, 4);
  1191. $fieldDef['cnpj_3'] = mb_substr($values[$fieldDef['fieldName']],-2);
  1192. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1193. $modobj->attrs = $fieldDef;
  1194. return $modobj;
  1195. }
  1196. //--Method: GetCepField
  1197. protected function &GetCepField(& $fieldDef, $values)
  1198. {
  1199. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1200. $fieldDef['cep_1'] = mb_substr($values[$fieldDef['fieldName']],0,-3);
  1201. $fieldDef['cep_2'] = mb_substr($values[$fieldDef['fieldName']],-3);
  1202. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1203. $modobj->attrs = $fieldDef;
  1204. return $modobj;
  1205. }
  1206. //--Method: GetSchedule
  1207. protected function &GetScheduleField(& $fieldDef, $values, $keyFields)
  1208. {
  1209. $SQL = "SELECT LPAD(DAY(start),2,'0') as startDay, ";
  1210. $SQL .= "LPAD(MONTH(start),2,'0') as startMonth, ";
  1211. $SQL .= "LPAD(YEAR(start),4,'0') as startYear, ";
  1212. $SQL .= "LPAD(HOUR(start),2,'0') as startHour, ";
  1213. $SQL .= "LPAD(MINUTE(start),2,'0') as startMinute, numOfHours ";
  1214. $SQL .= 'FROM AV_Schedule ';
  1215. $SQL .= "WHERE docId='".$keyFields['docId']."' ";
  1216. $SQL .= 'ORDER BY start ';
  1217. $scheduleList = $this->dbDriver->GetAll($SQL);
  1218. $values[$fieldDef['fieldName']] = json_encode($scheduleList);
  1219. return $this->GetTextField($fieldDef,$values);
  1220. }
  1221. //--Method: GetSelectField
  1222. function &GetSelectField(& $fieldDef,$values,$keyFields,$templates,
  1223. $sourceId)
  1224. {
  1225. // retrieves information from FORM_SelectField
  1226. $SQL = ' SELECT * FROM FORM_SelectField WHERE ';
  1227. $SQL .= "tableName='" .$this->table. "' AND ";
  1228. $SQL .= "(fieldName='" . $fieldDef['fieldName']. "' OR ";
  1229. $SQL .= "FIND_IN_SET('".$fieldDef['fieldName']."',targetField)<>0) ";
  1230. $selectFields = $this->dbDriver->GetAll($SQL);
  1231. foreach($selectFields as $key=>$sField)
  1232. {
  1233. if($selectFields[$key]['fieldName']==$fieldDef['fieldName'])
  1234. $field = $selectFields[$key];
  1235. else
  1236. {
  1237. $targetFields = explode(',',$selectFields[$key]['targetField']);
  1238. if(in_array($fieldDef['fieldName'],$targetFields))
  1239. $sourceField=$selectFields[$key]['fieldName'];
  1240. }
  1241. }
  1242. $fieldDef['targetField'] = $field['targetField'];
  1243. $fieldDef['sourceField'] = $field['sourceField'];
  1244. $modobj = new FORM_Object('field', $this->pageBuilder,
  1245. $templates, $fieldDef['type'].'Option');
  1246. $modobj->attrs = & $fieldDef;
  1247. if($field['sourceField'] != NULL)
  1248. {
  1249. if($sourceId != '')
  1250. $sourceValue = $sourceId;
  1251. else
  1252. $sourceValue = $values[$sourceField];
  1253. //$fieldDef['value'] = $sourceValue;
  1254. }
  1255. // else
  1256. $fieldDef['value'] = $values[$fieldDef['fieldName']];
  1257. // retrieves select options if sourceField is NULL (no dependence)
  1258. // or if sourceValue is set (dependence solved)
  1259. if($field['sourceField'] == NULL || $sourceValue != '')
  1260. {
  1261. $currValue = $values[$fieldDef['fieldName']];
  1262. $SQL = "SELECT ";
  1263. if($field['optionTableValue'] == NULL)
  1264. $SQL .= 'value, ';
  1265. else
  1266. $SQL .= $field['optionTableValue'] . ' as value, ';
  1267. $SQL .= $field['optionTableKey'] . " as id,";
  1268. $SQL .= "IF(" . $field['optionTableKey'] . " ='".$currValue;
  1269. $SQL .= "',1,0) as selected, '";
  1270. $SQL .= $fieldDef['type']."Option' as type FROM ";
  1271. $SQL .= $field['optionTable'];
  1272. if($field['optionTableValue'] == NULL)
  1273. $SQL .= " ,LANG_Label";
  1274. $WHERE = '';
  1275. if($sourceValue != '')
  1276. $WHERE = $field['sourceField'] . " = '$sourceValue' ";
  1277. if($field['optionTableValue'] == NULL)
  1278. {
  1279. if($WHERE != '')
  1280. $WHERE .= ' AND ';
  1281. $WHERE .= "labelId = name AND lang = '";
  1282. $WHERE .= $this->lang."'";
  1283. }
  1284. if($WHERE != '')
  1285. $SQL .= ' WHERE ' . $WHERE;
  1286. if($field['orderField'] != NULL)
  1287. {
  1288. $SQL .= ' ORDER BY ' . $field['orderField'];
  1289. $SQL .= ' '.($field['ascOrder']=='1'?'ASC':'DESC');
  1290. }
  1291. $rows = $this->dbDriver->GetAll($SQL);
  1292. $i = 0;
  1293. foreach($rows as & $row)
  1294. {
  1295. $modobj->children[$i] = new AV_ModObject('option',
  1296. $this->pageBuilder);
  1297. if($row['selected'] == 1)
  1298. {
  1299. $row['xhtmlSelectedAttr'] = "selected='selected'";
  1300. $row['xhtmlCheckedAttr'] = "checked='checked'";
  1301. $modobj->attrs['text'] = $row['value'];
  1302. $fieldDef['text'] = $row['value'];
  1303. }
  1304. $modobj->children[$i]->attrs = & $row;
  1305. $modobj->children[$i]->attrs['fieldIndex'] = $i+1;
  1306. $modobj->children[$i]->attrs['fieldName'] = $fieldDef['fieldName'];
  1307. $modobj->children[$i]->attrs['lang'] = $this->lang;
  1308. $i++;
  1309. }
  1310. $modobj->attrs['numOfOptions'] = $i;
  1311. }
  1312. return $modobj;
  1313. }
  1314. protected function GetReadOnlySelectField(& $fieldDef, $values)
  1315. {
  1316. // retrieves information from FORM_SelectField
  1317. $SQL = ' SELECT * FROM FORM_SelectField WHERE ';
  1318. $SQL .= "tableName='" .$this->table. "' AND ";
  1319. $SQL .= "fieldName='" . $fieldDef['fieldName']. "' ";
  1320. $SQL .= 'LOCK IN SHARE MODE';
  1321. $selectField = $this->dbDriver->GetRow($SQL);
  1322. $SQL = "SELECT ";
  1323. if($selectField['optionTableValue'] == NULL)
  1324. $SQL .= 'value ';
  1325. else
  1326. $SQL .= $selectField['optionTableValue'] . ' as value ';
  1327. $SQL .= 'FROM ' . $selectField['optionTable'];
  1328. if($field['optionTableValue'] == NULL)
  1329. $SQL .= " ,LANG_Label";
  1330. $SQL .= ' WHERE ';
  1331. $SQL .= $selectField['optionTableKey'];
  1332. $SQL .= "='".$values[$fieldDef['fieldName']]."' ";
  1333. if($selectField['optionTableValue'] == NULL)
  1334. {
  1335. $SQL .= "AND labelId = name AND lang = '";
  1336. $SQL .= $this->pageBuilder->lang . "'";
  1337. }
  1338. $fieldDef['text'] = $this->dbDriver->GetOne($SQL);
  1339. return $this->GetTextField($fieldDef, $values);
  1340. }
  1341. //--Method: GetSelectMultField
  1342. protected function &GetSelectMultField(&$fieldDef,$values,$keyFields,
  1343. $templates,$sourceId)
  1344. {
  1345. // retrieves information from FORM_SelectField
  1346. $SQL = ' SELECT * FROM FORM_SelectField WHERE ';
  1347. $SQL .= "tableName='" .$this->table. "' AND ";
  1348. $SQL .= "(fieldName='" . $fieldDef['fieldName']. "' OR ";
  1349. $SQL .= "FIND_IN_SET('".$fieldDef['fieldName']."',targetField)<>0) ";
  1350. $selectFields = $this->dbDriver->GetAll($SQL);
  1351. foreach($selectFields as $key=>$sField)
  1352. {
  1353. if($selectFields[$key]['fieldName']==$fieldDef['fieldName'])
  1354. $field = $selectFields[$key];
  1355. else
  1356. {
  1357. $targetFields = explode(',',$selectFields[$key]['targetField']);
  1358. if(in_array($fieldDef['fieldName'],$targetFields))
  1359. $sourceField=$selectFields[$key]['fieldName'];
  1360. }
  1361. }
  1362. $fieldDef['targetField'] = $field['targetField'];
  1363. $fieldDef['sourceField'] = $field['sourceField'];
  1364. $modobj = new FORM_Object('field', $this->pageBuilder,
  1365. $templates, 'selectMultOption');
  1366. if($field['sourceField'] != NULL)
  1367. {
  1368. if($sourceId != '')
  1369. $sourceValue = $sourceId;
  1370. else
  1371. $sourceValue = $values[$sourceField];
  1372. }
  1373. // retrieves select options if sourceField is NULL (no dependence)
  1374. // or if sourceValue is set (dependence solved)
  1375. if($field['sourceField'] == NULL || $sourceValue != '')
  1376. {
  1377. $SQL = 'SELECT ';
  1378. if($field['optionTableValue'] == NULL)
  1379. $SQL .= 'value, ';
  1380. else
  1381. $SQL .= $field['optionTableValue'] . ' as value, ';
  1382. $SQL .= $field['optionTableKey']." as id, ";
  1383. $SQL .= "'selectMultOption' as type ";
  1384. $SQL .= "FROM ".$field['optionTable'];
  1385. if($field['optionTableValue'] == NULL)
  1386. $SQL .= " ,LANG_Label ";
  1387. $WHERE = '';
  1388. if($sourceValue != '')
  1389. $WHERE = $field['sourceField'] . " = '$sourceValue' ";
  1390. if($field['optionTableValue'] == NULL)
  1391. {
  1392. if($WHERE != '')
  1393. $WHERE .= ' AND ';
  1394. $WHERE .= "labelId = name AND lang = '";
  1395. $WHERE .= $this->lang."'";
  1396. }
  1397. if($WHERE != '')
  1398. $SQL .= ' WHERE ' . $WHERE;
  1399. if($field['orderField'] != NULL)
  1400. {
  1401. $SQL .= ' ORDER BY ' . $field['orderField'];
  1402. $SQL .= ' '.($field['ascOrder']=='1'?'ASC':'DESC');
  1403. }
  1404. $rows = $this->dbDriver->GetAll($SQL);
  1405. if($keyFields != NULL)
  1406. {
  1407. // retrieves selected options
  1408. $control = TRUE;
  1409. foreach($keyFields as $key => $value)
  1410. {
  1411. if($control = TRUE)
  1412. {
  1413. $WHERE = $key. " = '" .$value. "'";
  1414. $control = FALSE;
  1415. }
  1416. else
  1417. $WHERE .=" AND " .$key. " = '" .$value. "'";
  1418. }
  1419. $SQL = ' SELECT '.$field['optionTableKey'].' FROM ';
  1420. $SQL .= $field['selectedTable'].' WHERE '.$WHERE;
  1421. $temp1 = $this->dbDriver->GetCol($SQL);
  1422. }
  1423. foreach ($rows as $key => $value)
  1424. {
  1425. if (in_array($value['id'],$temp1))
  1426. {
  1427. $rows[$key]['selected'] = 1;
  1428. $rows[$key]['xhtmlCheckedAttr'] = "checked='checked'";
  1429. $selected[] = $value['id'];
  1430. $selectedText[] = $value['value'];
  1431. }
  1432. else
  1433. $rows[$key]['selected'] = 0;
  1434. $rows[$key]['fieldName'] = $fieldDef['fieldName'];
  1435. }
  1436. $fieldDef['value'] = $selected;
  1437. $fieldDef['text'] = implode(',', $selectedText);
  1438. $modobj->LoadChildrenFromRows($rows, "option", "numOfOptions");
  1439. }
  1440. else
  1441. $fieldDef['numOfOptions'] = '0';
  1442. $modobj->attrs = ArrayObjectMerge($modobj->attrs,$fieldDef);
  1443. return $modobj;
  1444. }
  1445. protected function &GetFileListField(&$fieldDef,&$keyFields,&$templates)
  1446. {
  1447. $fileListField = new AV_ModObject('file', $this->pageBuilder,
  1448. $this->GetTemplate($templates,
  1449. 'fileListItem'),
  1450. 'imageListItem');
  1451. $fileListField->attrs = & $fieldDef;
  1452. $fileListField->isArray = true;
  1453. $SQL = 'SELECT path,contentFileTable,contentFileFK,maxFiles ';
  1454. $SQL .= 'FROM LIB_Folder f, LIB_FileListField cif ';
  1455. $SQL .= "WHERE tableName='".$this->table."' ";
  1456. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  1457. $SQL .= 'AND f.folderId = cif.folderId ';
  1458. $data = $this->dbDriver->GetRow($SQL);
  1459. if(count($data) == NULL)
  1460. throw new AV_Exception(NOT_FOUND, $this->lang);
  1461. $fileListField->attrs['path'] = $data['path'];
  1462. $fileListField->attrs['maxFiles'] = $data['maxFiles'];
  1463. // retrives file list
  1464. if($keyFields != NULL)
  1465. {
  1466. $SQL = "SELECT fieldName,fileId as docId,value as title,contentType,";
  1467. $SQL .= 'creationDate, lastChanged ';
  1468. $SQL .= 'FROM '.$data['contentFileTable'].' ci, ';
  1469. $SQL .= 'LIB_FileDocument d, LIB_Node n, LANG_Label l ';
  1470. $SQL .= "WHERE tableName='".$this->table."' ";
  1471. $SQL .= "AND fieldName='".$fieldDef['fieldName']."'";
  1472. reset($keyFields);
  1473. $SQL .= "AND ci.".$data['contentFileFK']."='".current($keyFields)."' ";
  1474. $SQL .= 'AND fileId=d.docId AND d.docId=n.nodeId AND d.title=l.labelId ';
  1475. $SQL .= "AND lang='".$this->lang."' ";
  1476. $SQL .= 'ORDER BY idx';
  1477. $fileListField->attrs['value'] = $this->dbDriver->GetAll($SQL);
  1478. }
  1479. return $fileListField;
  1480. }
  1481. protected function &GetObjectListField(&$fieldDef,&$keyFields,$dateFormat,
  1482. $numberFormat,$fieldName,$short)
  1483. {
  1484. $objectListField = new AV_ModObject('object', $this->pageBuilder);
  1485. $objectListField->attrs = & $fieldDef;
  1486. $objectListField->isArray = true;
  1487. $data = $this->GetObjectListInfo($fieldDef);
  1488. $objectListField->attrs['path'] = $data['path'];
  1489. $libDoc = new LIB_Document($this->module);
  1490. $formObject = new FORM_Object('form', $this->pageBuilder);
  1491. $keyFieldsAux = NULL;
  1492. $libDoc->GetForm($keyFieldsAux,$data['path'],NULL,NULL,$templates,
  1493. $formObject,NULL,$dateFormat,$numberFormat,$short);
  1494. // remove LIB_Node fields
  1495. $objectListField->children = $formObject->children;
  1496. $excludeFields = array('userId','groupId','userRight','groupRight',
  1497. 'otherRight','creationDate','lastChanged',
  1498. 'lastChangedUserId','keywords');
  1499. foreach($objectListField->children as $key=>& $child)
  1500. if(in_array($child->attrs['fieldName'],$excludeFields))
  1501. unset($objectListField->children[$key]);
  1502. // retrives object list data
  1503. if($keyFields != NULL)
  1504. {
  1505. $search['path'] = $data['path'];
  1506. $exlusionFilter['fieldName'] = $data['parentIdField'];
  1507. $exlusionFilter['value'] = current($keyFields);
  1508. $exlusionFilter['operator'] = '=';
  1509. $search['exclusionFilters'][$data['parentIdField']][] = $exlusionFilter;
  1510. $search['orderBy'] = 'itemOrder';
  1511. $search['order'] = '1';
  1512. $search['dateFormat'] = $dateFormat;
  1513. $search['numberFormat'] = $numberFormat;
  1514. $search['tempTable'] = '0';
  1515. $objectListField->attrs['value'] = $libDoc->Search($search);
  1516. }
  1517. else
  1518. $objectListField->attrs['value'] = array();
  1519. return $objectListField;
  1520. }
  1521. protected function &GetEmailListField(&$fieldDef,&$keyFields,&$templates)
  1522. {
  1523. $field = new AV_ModObject('file', $this->pageBuilder);
  1524. $field->attrs = & $fieldDef;
  1525. $field->isArray = true;
  1526. $SQL = 'SELECT listTable, f.fieldName as keyField ';
  1527. $SQL .= 'FROM FORM_EmailListField el, FORM_Field f ';
  1528. $SQL .= "WHERE el.tableName='".$this->table."' ";
  1529. $SQL .= "AND el.fieldName='".$fieldDef['fieldName']."' ";
  1530. $SQL .= 'AND el.tableName = f.tableName ';
  1531. $SQL .= "AND keyField = '1' ";
  1532. $data = $this->dbDriver->GetRow($SQL);
  1533. if(count($data) == NULL)
  1534. throw new AV_Exception(NOT_FOUND, $this->lang);
  1535. // retrives email list
  1536. if($keyFields != NULL)
  1537. {
  1538. reset($keyFields);
  1539. $SQL = 'SELECT email FROM '.$data['listTable'].' WHERE ';
  1540. $SQL .= $data['keyField']."='".current($keyFields)."' ";
  1541. $SQL .= 'ORDER BY idx';
  1542. $field->attrs['value'] = $this->dbDriver->GetCol($SQL);
  1543. }
  1544. return $field;
  1545. }
  1546. protected function &GetFolderField(&$fieldDef, $values)
  1547. {
  1548. $modobj = new AV_ModObject('field', $this->pageBuilder);
  1549. $modobj->attrs = & $fieldDef;
  1550. if($values[$fieldDef['fieldName']]!=NULL)
  1551. {
  1552. $folders = explode('/',$values[$fieldDef['fieldName']]);
  1553. if(count($folders)<2)
  1554. throw new AV_Exception(INVALID_FIELD, $this->lang,
  1555. Array('fieldName'=>$fieldDef['fieldName'],
  1556. 'table'=>$this->table));
  1557. // eats empty item
  1558. array_pop($folders);
  1559. $fieldDef['value'] = array_pop($folders);
  1560. $fieldDef['folderPath'] = implode('/',$folders).'/';
  1561. }
  1562. return $modobj;
  1563. }
  1564. //--Method: GetFileField
  1565. protected function GetFileField(& $fieldDef, $values)
  1566. {
  1567. $labelMan = new LANG_Label($this->module);
  1568. $fieldDef['adminMaxFileSizeTag']=$labelMan->GetLabel('adminMaxFileSizeTag',
  1569. $this->lang);
  1570. $fieldDef['maxFileSize'] = ini_get('upload_max_filesize');
  1571. return $this->GetTextField($fieldDef, $values);
  1572. }
  1573. //--Method: GetUsageTermsField
  1574. protected function GetUsageTermsField(& $fieldDef, $values)
  1575. {
  1576. if($values[$fieldDef['fieldName']]==NULL)
  1577. {
  1578. $libDoc = new LIB_Document($this->module);
  1579. $params['path'] = '/content/'.$this->pageBuilder->siteConfig->getVar('cms','usageTermsPath');
  1580. $params['orderBy'] = 'fixedOrder';
  1581. $params['order'] = '1';
  1582. $params['limit'] = 1;
  1583. $rows = $libDoc->Search($params);
  1584. if(count($rows)==0)
  1585. throw new AV_Exception(NOT_FOUND, $this->lang,
  1586. Array('fieldName'=>$fieldDef['fieldName']));
  1587. $docId = $rows[0]['docId'];
  1588. }
  1589. else
  1590. $docId = $values[$fieldDef['fieldName']];
  1591. $label = $fieldDef['acceptUseTermsTag1'];
  1592. $label .= " <a href='content/$docId' target='_blank'>";
  1593. $label .= $fieldDef['acceptUseTermsTag2']."</a> ";
  1594. $label .= $fieldDef['acceptUseTermsTag3'];
  1595. $fieldDef['label'] = $label;
  1596. $fieldDef['docId'] = $docId;
  1597. return $this->GetTextField($fieldDef, $values);
  1598. }
  1599. protected function GetLEOrNPEntityRef(& $fieldDef, $values, $histEntityType)
  1600. {
  1601. $fieldDef['lang'] = $this->lang;
  1602. $labelMan = new LANG_Label($this->module);
  1603. $fieldDef['searchCriteriaTag'] = $labelMan->GetLabel('searchCriteriaTag',
  1604. $this->lang);
  1605. $fieldDef['searchTag'] = $labelMan->GetLabel('searchTag',$this->lang);
  1606. $fieldDef['newTag'] = $labelMan->GetLabel('newTag',$this->lang);
  1607. $fieldDef['registerTag'] = $labelMan->GetLabel('registerTag',$this->lang);
  1608. $fieldDef['changeSelectionTag'] = $labelMan->GetLabel('changeSelectionTag',
  1609. $this->lang);
  1610. $fieldDef['legalEntityTag'] = $labelMan->GetLabel('legalEntityTag',
  1611. $this->lang);
  1612. $fieldDef['naturalPersonTag'] = $labelMan->GetLabel('naturalPersonTag',
  1613. $this->lang);
  1614. if($values[$fieldDef['fieldName']] != NULL)
  1615. {
  1616. if($histEntityType == NULL)
  1617. {
  1618. $libDoc = new LIB_Document($this->module);
  1619. $docData = $libDoc->GetDocData($values[$fieldDef['fieldName']],
  1620. 'share');
  1621. $histEntityType = $docData['type'];
  1622. }
  1623. switch($histEntityType)
  1624. {
  1625. case 'legalEntity':
  1626. $table = new FORM_DynTable('EM_LegalEntity', $this->module);
  1627. break;
  1628. case 'naturalPerson':
  1629. $table = new FORM_DynTable('EM_NaturalPerson', $this->module);
  1630. break;
  1631. }
  1632. $table->LoadFieldsDef();
  1633. $keyFields = Array('docId'=>$values[$fieldDef['fieldName']]);
  1634. $legalEntityData = $table->Get($keyFields);
  1635. switch($histEntityType)
  1636. {
  1637. case 'legalEntity':
  1638. $cnpj_1 = mb_substr($legalEntityData['cnpj'],0,-6);
  1639. $cnpj_2 = mb_substr($legalEntityData['cnpj'],-6, 4);
  1640. $cnpj_3 = mb_substr($legalEntityData['cnpj'],-2);
  1641. $fieldDef['id'] = $cnpj_1 . '/' . $cnpj_2 . '-' . $cnpj_3;
  1642. $fieldDef['title'] = $legalEntityData['fantasyName'];
  1643. break;
  1644. case 'naturalPerson':
  1645. $cpf_1 = mb_substr($legalEntityData['cpf'],0,-2);
  1646. $cpf_2 = mb_substr($legalEntityData['cpf'],-2);
  1647. $fieldDef['id'] = $cpf_1 . '-' . $cpf_2;
  1648. $fieldDef['title'] = $legalEntityData['name'];
  1649. break;
  1650. }
  1651. $fieldDef['entityRefType'] = $histEntityType;
  1652. }
  1653. return $this->GetTextField($fieldDef, $values);
  1654. }
  1655. protected function GetNextFieldName($prefix)
  1656. {
  1657. $offset = mb_strlen($prefix) + 1;
  1658. $SQL = "SELECT MAX(SUBSTR(fieldName,$offset)+0) ";
  1659. $SQL .= 'FROM FORM_Field WHERE ';
  1660. $SQL .= "tableName='" . $this->table . "' AND fieldName like '$prefix%' ";
  1661. $SQL .= 'LOCK IN SHARE MODE';
  1662. $num = $this->dbDriver->GetOne($SQL);
  1663. if($num == NULL)
  1664. return $prefix.'1';
  1665. return $prefix . ++$num;
  1666. }
  1667. protected function LockFieldForUpdate($fieldName)
  1668. {
  1669. $SQL = 'SELECT * FROM FORM_Field WHERE ';
  1670. $SQL .= "tableName='".$this->table."' AND fieldName='$fieldName' ";
  1671. $SQL .= 'FOR UPDATE ';
  1672. $fieldDef = $this->dbDriver->GetRow($SQL);
  1673. if(count($fieldDef) == 0)
  1674. throw new AV_Exception(NOT_FOUND, $this->lang);
  1675. $this->lockedField = $fieldDef;
  1676. return $fieldDef;
  1677. }
  1678. protected function InsSelectField($fieldName, $optionTable, $optionTableKey,
  1679. $selectedTable, $sourceField, $targetField)
  1680. {
  1681. $SQL = 'INSERT INTO FORM_SelectField (tableName,fieldName,optionTable,';
  1682. $SQL .= 'optionTableKey,selectedTable,sourceField,targetField) VALUES ';
  1683. $SQL .= "('".$this->table."','$fieldName','$optionTable',";
  1684. $SQL .= "'$optionTableKey','$selectedTable','$sourceField',";
  1685. $SQL .= "'$targetField')";
  1686. $this->dbDriver->ExecSQL($SQL);
  1687. }
  1688. protected function SetSelectField($fieldName, $optionTable, $optionTableKey,
  1689. $selectedTable, $sourceField, $targetField)
  1690. {
  1691. $SQL = 'SELECT * FROM FORM_SelectField WHERE ';
  1692. $SQL .= "tableName='".$this->table."' AND fieldName='$fieldName' ";
  1693. $SQL .= 'FOR UPDATE';
  1694. $field = $this->dbDriver->GetRow($SQL);
  1695. if(cont($field)==0)
  1696. throw new AV_Exception(NOT_FOUND, $this->lang);
  1697. $SQL = NULL;
  1698. if($optionTable != NULL)
  1699. $SQL .= "optionTable = '$optionTable'";
  1700. if($optionTableKey != NULL)
  1701. {
  1702. if($SQL != NULL)
  1703. $SQL .= ',';
  1704. $SQL .= "optionTableKey='$optionTableKey'";
  1705. }
  1706. if($selectedTable != NULL)
  1707. {
  1708. if($SQL != NULL)
  1709. $SQL .= ',';
  1710. $SQL .= "selectedTable='$selectedTable'";
  1711. }
  1712. if($sourceField != NULL)
  1713. {
  1714. if($SQL != NULL)
  1715. $SQL .= ',';
  1716. $SQL .= "sourceField='$sourceField'";
  1717. }
  1718. if($targetField != NULL)
  1719. {
  1720. if($SQL != NULL)
  1721. $SQL .= ',';
  1722. $SQL .= "targetField='$targetField'";
  1723. }
  1724. if($SQL == NULL)
  1725. return;
  1726. $SQL = 'UPDATE FORM_SelectField SET ' . $SQL;
  1727. $SQL .= "tableName='".$this->table."' AND fieldName='$fieldName' ";
  1728. $this->dbDriver->ExecSQL($SQL);
  1729. }
  1730. private function ConvertIf($expression)
  1731. {
  1732. $ctrl = array();
  1733. $pos = 0;
  1734. while($pos<strlen($expression))
  1735. {
  1736. if(strtolower(substr($expression,$pos,2))=='if')
  1737. {
  1738. // remove if
  1739. $expression = substr_replace($expression,'',$pos,2);
  1740. $ctrl[] = 'if';
  1741. $pos++;
  1742. }
  1743. else if($expression[$pos]==',')
  1744. {
  1745. if(end($ctrl)=='if')
  1746. {
  1747. $expression = substr_replace($expression,')?',$pos,1);
  1748. $ctrl[count($ctrl)-1] = 'then';
  1749. $pos+=2;
  1750. }
  1751. else
  1752. {
  1753. $expression = substr_replace($expression,':',$pos,1);
  1754. $pos++;
  1755. }
  1756. }
  1757. else if($expression[$pos]==')')
  1758. {
  1759. if(end($ctrl)=='then')
  1760. $expression = substr_replace($expression,'',$pos,1);
  1761. else
  1762. $pos++; // )
  1763. array_pop($ctrl);
  1764. }
  1765. else if($expression[$pos]=='(')
  1766. {
  1767. $ctrl[] = '(';
  1768. $pos++;
  1769. }
  1770. else if($expression[$pos]=='=')
  1771. {
  1772. $expression = substr_replace($expression,'==',$pos,1);
  1773. $pos+=2;
  1774. }
  1775. else
  1776. $pos++;
  1777. }
  1778. return $expression;
  1779. }
  1780. //--Method: GetFormulaField
  1781. protected function &GetFormulaField(& $fieldDef, $values)
  1782. {
  1783. // campos com pow não são calculados na interface
  1784. $found = (stristr($this->formulas[$fieldDef['fieldName']], 'pow') ||
  1785. stristr($this->formulas[$fieldDef['fieldName']], 'log'));
  1786. if($found===false)
  1787. {
  1788. $fieldDef['expression']="SetNodeValue('".$fieldDef['fieldName']."',\\\"";
  1789. $fieldDef['expression'].=$this->formulas[$fieldDef['fieldName']];
  1790. $fieldDef['expression'].="\\\");";
  1791. }
  1792. $fieldDef['type'] = $this->formulaType[$fieldDef['fieldName']];
  1793. $field = & $this->GetField($fieldDef, $values, NULL, $templates, NULL);
  1794. $fieldDef['type'] = 'formula';
  1795. $fieldDef['formulaType'] = $this->formulaType[$fieldDef['fieldName']];
  1796. return $field;
  1797. }
  1798. //--Method: Decompose
  1799. protected function Decompose(&$formulas, $formula, &$formulaFields, &$params)
  1800. {
  1801. $lex = new FORM_LexAnalyzer($formula);
  1802. while($lex->NextToken())
  1803. {
  1804. if($lex->token == varTk)
  1805. {
  1806. // check if variable is a formula
  1807. if(isset($formulaFields[$lex->value]))
  1808. {
  1809. // check if the formula variable has already been decomposed
  1810. if(isset($formulas[$lex->value]))
  1811. $out .= '(' . $formulas[$lex->value] . ')';
  1812. else
  1813. {
  1814. // decompose formula variable
  1815. $out .= '(' . $this->Decompose($formulas,
  1816. $formulaFields[$lex->value],
  1817. $formulaFields, $params);
  1818. $out .= ')';
  1819. }
  1820. }
  1821. else
  1822. // prints normal variable
  1823. $out .= $this->PrintVar($lex->value, $params);
  1824. }
  1825. else
  1826. $out .= $lex->value;
  1827. }
  1828. return $out;
  1829. }
  1830. protected function PrintVar($fieldName, & $params)
  1831. {
  1832. $out = "eval(StrToFloat(GetNodeValue('$fieldName'), '";
  1833. $out .= $params['locale']['mon_decimal_point'] . "','";
  1834. $out .= $params['locale']['mon_thousands_sep'] . "'))";
  1835. return $out;
  1836. }
  1837. //--Method: LoadFormulaDefs
  1838. protected function LoadFormulaDefs()
  1839. {
  1840. if($this->formulaFieldsDef != NULL)
  1841. return $this->formulaFieldsDef;
  1842. $SQL = 'SELECT * FROM FORM_Formula formula, FORM_Field field ';
  1843. $SQL .= "WHERE formula.tableName = '" .$this->table. "' ";
  1844. $SQL .= 'AND formula.tableName=field.tableName ';
  1845. $SQL .= 'AND formula.fieldName=field.fieldName ';
  1846. $SQL .= 'LOCK IN SHARE MODE ' ;
  1847. $this->formulaFieldsDef = $this->dbDriver->GetAll($SQL);
  1848. return $this->formulaFieldsDef;
  1849. }
  1850. protected function GetFieldLabel($fieldDef)
  1851. {
  1852. if($fieldDef['labelId'] != NULL)
  1853. {
  1854. $langLabel = new LANG_Label($this->module);
  1855. return ' ('.$langLabel->GetLabel($fieldDef['labelId'],$this->lang).')';
  1856. }
  1857. else
  1858. return ' ('.$fieldDef['fieldName'].')';
  1859. }
  1860. //
  1861. //--Private
  1862. //
  1863. private function CheckIndexes($fields)
  1864. {
  1865. $rows = $this->GetViolatedIndexes($fields);
  1866. if(count($rows) != 0)
  1867. {
  1868. $fieldDef = $this->GetViolatedIndexField($fields, $rows[0]);
  1869. throw new AV_Exception(INDEX_VIOLATED, $this->lang,
  1870. array('fieldName'=>$fieldDef['fieldName'],
  1871. 'addMsg'=>$this->GetFieldLabel($fieldDef)));
  1872. }
  1873. }
  1874. private function GetViolatedIndexes($fields)
  1875. {
  1876. // compose index search SQL from dynamic table unique fields
  1877. $firstField = true;
  1878. $firstWhere = true;
  1879. $firstKey = true;
  1880. foreach($this->fieldsDef as $key => $field)
  1881. {
  1882. $fieldName = $field['fieldName'];
  1883. if($field['keyField'] != '1')
  1884. {
  1885. if($field['uniq'] == '1' &&
  1886. $fields[$fieldName] !== NULL &&
  1887. $fields[$fieldName] !== '')
  1888. {
  1889. if(!$firstField)
  1890. $FIELDS .= ', ';
  1891. else $firstField = false;
  1892. if(!$firstWhere)
  1893. $WHERE .= ' OR ';
  1894. else $firstWhere = false;
  1895. $FIELDS .= $fieldName;
  1896. $WHERE .= $fieldName . " = '";
  1897. $WHERE .= $fields[$fieldName] . "'";
  1898. } // if($field['uniq'] == '1')
  1899. }
  1900. else // $field['keyField'] != '0'
  1901. {
  1902. if(!$firstKey)
  1903. $WHERE2 .= ' OR ';
  1904. else
  1905. $firstKey = false;
  1906. $WHERE2 .= $fieldName . " <> '" . $fields[$fieldName] . "'";
  1907. } // if($field['keyField'] == '0')
  1908. } // foreach($this->fieldsDef as $field)
  1909. if(isset($WHERE2))
  1910. $WHERE = '(' . $WHERE . ') AND (' . $WHERE2 . ')';
  1911. // searches for the index
  1912. if($FIELDS !== NULL)
  1913. {
  1914. $SQL = 'SELECT ' . $FIELDS . ' FROM ' . $this->table;
  1915. $SQL .= ' WHERE ' . $WHERE . ' LOCK IN SHARE MODE ';
  1916. $rows = $this->dbDriver->GetAll($SQL);
  1917. }
  1918. return $rows;
  1919. }
  1920. private function GetViolatedIndexField($fields, & $row)
  1921. {
  1922. // searches violated index from dynamic table unique fields
  1923. foreach($this->fieldsDef as $field)
  1924. {
  1925. if($field['uniq'] == '1')
  1926. {
  1927. $fieldName = $field['fieldName'];
  1928. if(isset($row[$fieldName]) && $row[$fieldName] === $fields[$fieldName])
  1929. return $field;
  1930. }
  1931. }
  1932. throw new AV_Exception(FATAL_ERROR, $this->lang);
  1933. }
  1934. private function SetOptions(& $options, $fieldDef, $fieldValue, $fieldKey)
  1935. {
  1936. if($options == '')
  1937. return;
  1938. $field = $this->GetSelectFieldInfo($fieldDef['fieldName']);
  1939. if($fieldValue != 'LAST_INSERT_ID()')
  1940. {
  1941. $SQL = 'DELETE FROM '.$field['selectedTable']. ' WHERE '.$fieldKey.' = ';
  1942. $SQL .= $fieldValue;
  1943. $this->dbDriver->ExecSQL($SQL);
  1944. }
  1945. $SQL = 'INSERT INTO '.$field['selectedTable'].' ('.$fieldKey;
  1946. $SQL .= ', '.$field['optionTableKey'].') VALUES ';
  1947. $first = true;
  1948. foreach ($options as $opt)
  1949. {
  1950. if($opt['value'] != NULL)
  1951. {
  1952. if($first)
  1953. $first = false;
  1954. else
  1955. $SQL .= ', ';
  1956. $SQL .="(".$fieldValue.",".$opt['value'].")";
  1957. }
  1958. }
  1959. if($first)
  1960. {
  1961. if($fieldDef['required'])
  1962. {
  1963. throw new AV_Exception(FIELD_REQUIRED,$this->lang,
  1964. array('fieldName'=>$fieldDef['fieldName'],
  1965. 'addMsg'=>$this->GetFieldLabel($fieldDef)));
  1966. }
  1967. return;
  1968. }
  1969. $this->dbDriver->ExecSQL($SQL);
  1970. }
  1971. private function SetSelectMultiple(& $options, $fieldDef, $keyValue, $fieldKey)
  1972. {
  1973. if($options == NULL)
  1974. return;
  1975. $field = $this->GetSelectFieldInfo($fieldDef['fieldName']);
  1976. if($keyValue != 'LAST_INSERT_ID()')
  1977. {
  1978. $SQL = 'DELETE FROM '.$field['selectedTable']. ' WHERE '.$fieldKey.' = ';
  1979. $SQL .= $keyValue;
  1980. $this->dbDriver->ExecSQL($SQL);
  1981. }
  1982. $SQL = 'INSERT INTO '.$field['selectedTable'].' ('.$fieldKey;
  1983. $SQL .= ', '.$field['optionTableKey'].') VALUES ';
  1984. $first = true;
  1985. foreach ($options as $opt)
  1986. {
  1987. if($opt != NULL)
  1988. {
  1989. if($first)
  1990. $first = false;
  1991. else
  1992. $SQL .= ', ';
  1993. $SQL .="(".$keyValue.",".$opt.")";
  1994. }
  1995. }
  1996. if($first)
  1997. {
  1998. if($fieldDef['required'])
  1999. {
  2000. throw new AV_Exception(FIELD_REQUIRED,$this->lang,
  2001. array('fieldName'=>$fieldDef['fieldName'],
  2002. 'addMsg'=>$this->GetFieldLabel($fieldDef)));
  2003. }
  2004. return;
  2005. }
  2006. $this->dbDriver->ExecSQL($SQL);
  2007. }
  2008. private function DuplicateOptions($sourceKey, $fieldDef)
  2009. {
  2010. $field = $this->GetSelectFieldInfo($fieldDef['fieldName']);
  2011. $SQL = 'INSERT INTO '.$field['selectedTable'].' ('.$sourceKey['fieldName'];
  2012. $SQL .= ', '.$field['optionTableKey'].') SELECT LAST_INSERT_ID()';
  2013. $SQL .= ", ".$field['optionTableKey']." FROM ".$field['selectedTable'];
  2014. $SQL .= ' WHERE '.$sourceKey['fieldName']."='".$sourceKey['fieldValue']."'";
  2015. $this->dbDriver->ExecSQL($SQL);
  2016. }
  2017. private function SetFileList(& $options, $fieldDef, $fieldValue)
  2018. {
  2019. if($options == '')
  2020. return NULL;
  2021. $SQL = 'SELECT contentFileTable,contentFileFK,maxFiles ';
  2022. $SQL .= 'FROM LIB_FileListField cif ';
  2023. $SQL .= "WHERE tableName='".$this->table."' ";
  2024. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  2025. $data = $this->dbDriver->GetRow($SQL);
  2026. if(count($data) == NULL)
  2027. throw new AV_Exception(NOT_FOUND, $this->lang);
  2028. $table = $data['contentFileTable'];
  2029. $tableField = $data['contentFileFK'];
  2030. // remove old files
  2031. if ($fieldValue != 'LAST_INSERT_ID()')
  2032. {
  2033. $SQL = "SELECT fileId ";
  2034. $SQL .= "FROM $table ci ";
  2035. $SQL .= "WHERE tableName='".$this->table."' ";
  2036. $SQL .= "AND fieldName='".$fieldDef['fieldName']."'";
  2037. $SQL .= "AND $tableField='$fieldValue' ";
  2038. $SQL .= 'ORDER BY idx';
  2039. $oldValues = $this->dbDriver->GetCol($SQL);
  2040. $SQL = "DELETE FROM $table ";
  2041. $SQL .= "WHERE tableName='".$this->table."' ";
  2042. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  2043. $SQL .= "AND $tableField='$fieldValue'";
  2044. $this->dbDriver->ExecSQL($SQL);
  2045. }
  2046. $SQL = "INSERT INTO $table ";
  2047. $SQL .= "(tableName,fieldName,$tableField,fileId,idx) VALUES ";
  2048. $first = true;
  2049. foreach ($options as $key=>$opt)
  2050. {
  2051. if($opt['docId'] != NULL)
  2052. {
  2053. if($first)
  2054. $first = false;
  2055. else
  2056. $SQL .= ',';
  2057. $SQL.="('".$this->table."','".$fieldDef['fieldName']."',$fieldValue,";
  2058. $SQL.=$opt['docId'].",$key)";
  2059. }
  2060. }
  2061. if(!$first)
  2062. $this->dbDriver->ExecSQL($SQL);
  2063. return $oldValues;
  2064. }
  2065. private function SetObjectList(& $objects, $fieldDef, $fieldValue, $fieldKey,$new)
  2066. {
  2067. if($objects == '')
  2068. return NULL;
  2069. $data = $this->GetObjectListInfo($fieldDef);
  2070. $libDoc = new LIB_Document($this->module);
  2071. // read current object list
  2072. if(!$new)
  2073. {
  2074. $formObject = new FORM_Object('form',$this->pageBuilder,$templates);
  2075. $keyFields[$fieldKey] = $fieldValue;
  2076. $libDoc->GetForm($keyFields,$data['path'],$fieldDef['fieldName'],NULL,
  2077. $templates,$formObject);
  2078. if(count($formObject->children) != 1)
  2079. throw new AV_Exception(FAILED, $this->lang);
  2080. $objectList = current($formObject->children);
  2081. }
  2082. foreach($objects as $key=>& $obj)
  2083. {
  2084. $obj['itemOrder'] = $key;
  2085. if($obj['docId']!=NULL)
  2086. {
  2087. // remove docId from current object list, so it won't be removed
  2088. foreach($objectList->attrs['value'] as $key=>$value)
  2089. if($value['docId']==$obj['docId'])
  2090. unset($objectList->attrs['value'][$key]);
  2091. }
  2092. // /insert/set object
  2093. $libDoc = new LIB_Document($this->module);
  2094. $libDoc->LoadFieldsDef($obj['docId'],$data['path']);
  2095. $obj[$data['parentIdField']] = $fieldValue;
  2096. if($obj['docId']!=NULL)
  2097. $libDoc->Set($obj);
  2098. else
  2099. {
  2100. $obj['path'] = $data['path'];
  2101. $libDoc->Insert($obj);
  2102. }
  2103. }
  2104. // removes unlisted objects
  2105. if(!$new)
  2106. {
  2107. foreach($objectList->attrs['value'] as $value)
  2108. $libDoc->DelDocLink($data['path'],$value['docId']);
  2109. return array_values($objectList->attrs['value']);
  2110. }
  2111. return NULL;
  2112. }
  2113. private function SetEmailList(& $options, $fieldDef, $fieldValue)
  2114. {
  2115. if($options == '')
  2116. return NULL;
  2117. $SQL = 'SELECT listTable, f.fieldName as keyField ';
  2118. $SQL .= 'FROM FORM_EmailListField el, FORM_Field f ';
  2119. $SQL .= "WHERE el.tableName='".$this->table."' ";
  2120. $SQL .= "AND el.fieldName='".$fieldDef['fieldName']."' ";
  2121. $SQL .= 'AND el.tableName = f.tableName ';
  2122. $SQL .= "AND keyField = '1' ";
  2123. $data = $this->dbDriver->GetRow($SQL);
  2124. if(count($data) == NULL)
  2125. throw new AV_Exception(NOT_FOUND, $this->lang);
  2126. // remove old emails
  2127. if ($fieldValue != 'LAST_INSERT_ID()')
  2128. {
  2129. $SQL = 'SELECT email FROM '.$data['listTable'].' ';
  2130. $SQL .= 'WHERE '.$data['keyField']."='".$fieldValue."' ";
  2131. $SQL .= 'ORDER BY idx';
  2132. $oldValues = $this->dbDriver->GetCol($SQL);
  2133. $SQL = 'DELETE FROM '.$data['listTable'].' ';
  2134. $SQL .= 'WHERE '.$data['keyField']."='".$fieldValue."' ";
  2135. $this->dbDriver->ExecSQL($SQL);
  2136. }
  2137. $SQL = 'INSERT INTO '.$data['listTable'];
  2138. $SQL .= '('.$data['keyField'].',email,idx) VALUES ';
  2139. $first = true;
  2140. foreach ($options as $key=>$opt)
  2141. {
  2142. if($first)
  2143. $first = false;
  2144. else
  2145. $SQL .= ',';
  2146. $SQL.="('$fieldValue','".$opt."',$key)";
  2147. }
  2148. if(!$first)
  2149. $this->dbDriver->ExecSQL($SQL);
  2150. return $oldValues;
  2151. }
  2152. private function DuplicateFileList($sourceKey, $fieldDef)
  2153. {
  2154. $SQL = 'SELECT contentFileTable,contentFileFK,maxFiles ';
  2155. $SQL .= 'FROM LIB_FileListField cif ';
  2156. $SQL .= "WHERE tableName='".$this->table."' ";
  2157. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  2158. $data = $this->dbDriver->GetRow($SQL);
  2159. if(count($data) == NULL)
  2160. throw new AV_Exception(NOT_FOUND, $this->lang);
  2161. $table = $data['contentFileTable'];
  2162. $tableField = $data['contentFileFK'];
  2163. $SQL = "INSERT INTO $table ";
  2164. $SQL .= "(tableName,fieldName,$tableField,fileId,idx) SELECT ";
  2165. $SQL .= "tableName,fieldName,LAST_INSERT_ID(),fileId,idx ";
  2166. $SQL .= "FROM $table ";
  2167. $SQL .= "WHERE tableName='".$this->table."' ";
  2168. $SQL .= "AND fieldName='".$fieldDef['fieldName']."' ";
  2169. $SQL .= "AND $tableField='".$sourceKey['fieldValue']."'";
  2170. $this->dbDriver->ExecSQL($SQL);
  2171. }
  2172. private function DuplicateEmailList($sourceKey, $fieldDef)
  2173. {
  2174. $SQL = 'SELECT listTable, f.fieldName as keyField ';
  2175. $SQL .= 'FROM FORM_EmailListField el, FORM_Field f ';
  2176. $SQL .= "WHERE el.tableName='".$this->table."' ";
  2177. $SQL .= "AND el.fieldName='".$fieldDef['fieldName']."' ";
  2178. $SQL .= 'AND el.tableName = f.tableName ';
  2179. $SQL .= "AND keyField = '1' ";
  2180. $data = $this->dbDriver->GetRow($SQL);
  2181. if(count($data) == NULL)
  2182. throw new AV_Exception(NOT_FOUND, $this->lang);
  2183. $SQL = 'INSERT INTO '.$data['listTable'];
  2184. $SQL .= '('.$data['keyField'].',email,idx) SELECT ';
  2185. $SQL .= 'LAST_INSERT_ID(),email,idx ';
  2186. $SQL .= 'FROM '.$data['listTable'].' ';
  2187. $SQL .= 'WHERE '.$data['keyField']."='".$sourceKey['fieldValue']."' ";
  2188. $this->dbDriver->ExecSQL($SQL);
  2189. }
  2190. private function SetSchedule(& $schedule, $fieldDef, $fieldValue, $fieldKey)
  2191. {
  2192. if($schedule == '')
  2193. return;
  2194. // remove old files
  2195. if ($fieldValue != 'LAST_INSERT_ID()')
  2196. {
  2197. $SQL = "DELETE FROM AV_Schedule ";
  2198. $SQL .= "WHERE docId='$fieldValue' ";
  2199. $this->dbDriver->ExecSQL($SQL);
  2200. }
  2201. $SQL = "INSERT INTO AV_Schedule ";
  2202. $SQL .= "(docId, start, numOfHours) VALUES ";
  2203. $first = true;
  2204. foreach ($schedule as $key=>$item)
  2205. {
  2206. if($item['start'] != NULL && $item['numOfHours'] != NULL)
  2207. {
  2208. if($first)
  2209. $first = false;
  2210. else
  2211. $SQL .= ',';
  2212. $SQL.="($fieldValue,'".$item['start']."','".$item['numOfHours']."')";
  2213. }
  2214. }
  2215. if($first)
  2216. {
  2217. if($fieldDef['required'])
  2218. throw new AV_Exception(FIELD_REQUIRED,$this->lang,
  2219. array('fieldName'=>$fieldDef['fieldName'],
  2220. 'addMsg'=>$this->GetFieldLabel($fieldDef)));
  2221. }
  2222. else
  2223. $this->dbDriver->ExecSQL($SQL);
  2224. }
  2225. private function SetLabel($fieldDef, $fields, $labelName, $fieldKey)
  2226. {
  2227. if(count($fields)==0)
  2228. return;
  2229. $labelMan = new LANG_Label($this->module);
  2230. if($labelName == 'LAST_INSERT_ID()')
  2231. {
  2232. $SQL = "SELECT LAST_INSERT_ID() FROM DUAL";
  2233. $returnlabelId = $this->dbDriver->GetOne($SQL);
  2234. }
  2235. else
  2236. $returnlabelId = $labelName;
  2237. $labelId = $this->table."_".$fieldDef['fieldName']."_".$returnlabelId;
  2238. $labelMan->SetLabel($labelId,$fields,$fieldDef['type']!='langLabel');
  2239. $WHERE = " WHERE ".$fieldKey." = '".$returnlabelId."'";
  2240. $SQL = 'SELECT * FROM ' . $this->table . $WHERE . ' FOR UPDATE';
  2241. $this->dbDriver->GetRow($SQL);
  2242. $SQL = "UPDATE " .$this->table. " SET " . $fieldDef['fieldName']." = '";
  2243. $SQL .= $labelId. "' WHERE ".$fieldKey." = '".$returnlabelId."'";
  2244. $this->dbDriver->ExecSQL($SQL);
  2245. }
  2246. private function DuplicateLabel($sourceKey, $fieldDef)
  2247. {
  2248. $labelMan = new LANG_Label($this->module);
  2249. $SQL = "SELECT LAST_INSERT_ID() FROM DUAL";
  2250. $returnlabelId = $this->dbDriver->GetOne($SQL);
  2251. $sourceLabelId = $this->table."_".$fieldDef['fieldName']."_".$sourceKey['fieldValue'];
  2252. $labelId = $this->table."_".$fieldDef['fieldName']."_".$returnlabelId;
  2253. $labelMan->DuplicateLabel($sourceLabelId,$labelId,$fieldDef['type']!='langLabel');
  2254. $WHERE = " WHERE ".$sourceKey['fieldName']." = '".$returnlabelId."'";
  2255. $SQL = 'SELECT * FROM ' . $this->table . $WHERE . ' FOR UPDATE';
  2256. $this->dbDriver->GetRow($SQL);
  2257. $SQL = "UPDATE " .$this->table. " SET " . $fieldDef['fieldName']." = '";
  2258. $SQL .= $labelId. "' WHERE ".$sourceKey['fieldName']." = '".$returnlabelId."'";
  2259. $this->dbDriver->ExecSQL($SQL);
  2260. }
  2261. private function SetFieldLabels(& $fieldDef)
  2262. {
  2263. $SQL = 'SELECT l.labelId, value FROM FORM_FieldLabels fl, LANG_Label l ';
  2264. $SQL .= "WHERE tableName = '" . $this->table . "' AND ";
  2265. $SQL .= "fieldName = '" . $fieldDef['fieldName'] . "' AND ";
  2266. $SQL .= "fl.labelId = l.labelId AND ";
  2267. $SQL .= "lang = '".$this->lang."'";
  2268. $fieldLabels = $this->dbDriver->GetAll($SQL);
  2269. foreach($fieldLabels as $key=>$label)
  2270. $fieldDef[$label['labelId']] = $label['value'];
  2271. }
  2272. private function SetFile(&$fieldDef,&$value,&$extraFields)
  2273. {
  2274. $extraFields[$fieldDef['fieldName'].'FileName'] = "'".$value['name']."'";
  2275. $extraFields[$fieldDef['fieldName'].'Type'] = "'".$value['type']."'";
  2276. if(!$this->pageBuilder->siteConfig->getVar('admin','blobToFS'))
  2277. {
  2278. $data = file_get_contents($value['tmp_name']);
  2279. if(!$data)
  2280. throw new AV_Exception(FAILED, $this->lang,
  2281. array('addMsg'=>' (unable to open file)',
  2282. 'tmp_name'=>$value['tmp_name']));
  2283. $extraFields[$fieldDef['fieldName'].'Size'] = "'".
  2284. $this->PrepareFileSize(strlen($data))."'";
  2285. switch($this->dbDriver->type)
  2286. {
  2287. case 'mysql':
  2288. $data = "'".addslashes($data)."'";
  2289. $extraFields[$fieldDef['fieldName']] = & $data;
  2290. break;
  2291. case 'oracle':
  2292. $extraFields[$fieldDef['fieldName']] = 'EMPTY_BLOB()';
  2293. $value['content'] = $data;
  2294. break;
  2295. }
  2296. }
  2297. else
  2298. {
  2299. $libPath = $this->pageBuilder->siteConfig->getVar("dirs",
  2300. "library");
  2301. $this->dbDriver->ExecSQL('INSERT INTO LIB_BlobFS VALUES ()');
  2302. $blobId = $this->dbDriver->GetOne('SELECT LAST_INSERT_ID() FROM DUAL');
  2303. if(!ft_rename($value['tmp_name'], $libPath . $blobId))
  2304. throw new AV_Exception(FAILED, $this->lang,
  2305. array('addMsg'=>' (unable to rename file)',
  2306. 'tmp_name'=>$value['tmp_name']));
  2307. $extraFields[$fieldDef['fieldName']] = $blobId;
  2308. }
  2309. }
  2310. private function PrepareFileSize($fileSize)
  2311. {
  2312. if($fileSize >= 1073741824)
  2313. $ret = number_format(($fileSize / 1073741824),3) . " GB";
  2314. elseif ($fileSize >= 1048576)
  2315. $ret = number_format(($fileSize / 1048576),2) . " MB";
  2316. elseif ($fileSize >= 1024)
  2317. $ret = number_format(($fileSize / 1024),0) . " KB";
  2318. elseif ($fileSize >= 0)
  2319. $ret = $fileSize . " B";
  2320. else
  2321. $ret = "0 B";
  2322. return $ret;
  2323. }
  2324. }
  2325. ?>