1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12:
13: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
14:
15: 16: 17: 18: 19: 20:
21: class PifaFormCollection extends ItemCollection {
22:
23: 24: 25: 26: 27:
28: public function __construct($where = false) {
29: $cfg = cRegistry::getConfig();
30: parent::__construct(cRegistry::getDbTableName('pifa_form'), 'idform');
31: $this->_setItemClass('PifaForm');
32: if (false !== $where) {
33: $this->select($where);
34: }
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44:
45: private static function _getBy($client, $lang) {
46:
47:
48: $conditions = array();
49:
50:
51: $client = cSecurity::toInteger($client);
52: if (0 < $client) {
53: $conditions[] = 'idclient=' . $client;
54: }
55:
56:
57: $lang = cSecurity::toInteger($lang);
58: if (0 < $lang) {
59: $conditions[] = 'idlang=' . $lang;
60: }
61:
62:
63: $forms = new PifaFormCollection();
64: $succ = $forms->select(implode(' AND ', $conditions));
65:
66:
67:
68:
69:
70:
71:
72:
73:
74: if (false === $succ) {
75: return false;
76: }
77:
78: return $forms;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88:
89: public static function getByClient($client) {
90: if (0 >= cSecurity::toInteger($client)) {
91: $msg = Pifa::i18n('MISSING_CLIENT');
92: throw new PifaException($msg);
93: }
94:
95: return self::_getBy($client, 0);
96: }
97:
98: 99: 100: 101: 102: 103: 104: 105:
106: public static function getByLang($lang) {
107: if (0 >= cSecurity::toInteger($lang)) {
108: $msg = Pifa::i18n('MISSING_LANG');
109: throw new PifaException($msg);
110: }
111:
112: return self::_getBy(0, $lang);
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
125: public static function getByClientAndLang($client, $lang) {
126: if (0 >= cSecurity::toInteger($client)) {
127: $msg = Pifa::i18n('MISSING_CLIENT');
128: throw new PifaException($msg);
129: }
130:
131: if (0 >= cSecurity::toInteger($lang)) {
132: $msg = Pifa::i18n('MISSING_LANG');
133: throw new PifaException($msg);
134: }
135:
136: return self::_getBy($client, $lang);
137: }
138:
139: }
140:
141: 142: 143: 144: 145: 146:
147: class PifaForm extends Item {
148:
149: 150: 151: 152: 153:
154: private $_fields = NULL;
155:
156: 157: 158: 159: 160:
161: private $_errors = array();
162:
163: 164: 165: 166:
167: private $_lastInsertedId = NULL;
168:
169: 170: 171: 172: 173:
174: public function __construct($id = false) {
175: $cfg = cRegistry::getConfig();
176: parent::__construct(cRegistry::getDbTableName('pifa_form'), 'idform');
177: $this->setFilters(array(), array());
178: if (false !== $id) {
179: $this->loadByPrimaryKey($id);
180: }
181: }
182:
183: 184: 185: 186:
187: public function getErrors() {
188: return $this->_errors;
189: }
190:
191: 192: 193: 194:
195: public function setErrors($_errors) {
196: $this->_errors = $_errors;
197: }
198:
199: 200: 201:
202: public function loadFields() {
203: $col = new PifaFieldCollection();
204: $col->setWhere('PifaFieldCollection.idform', $this->get('idform'));
205: $col->setOrder('PifaFieldCollection.field_rank');
206: $col->query();
207: $this->_fields = array();
208: while (false !== $pifaField = $col->next()) {
209: $this->columnNames[] = $pifaField->get('column_name');
210: $this->_fields[] = clone $pifaField;
211: }
212: }
213:
214: 215: 216: 217: 218: 219: 220:
221: public function getFields() {
222: if (NULL === $this->_fields) {
223: $this->loadFields();
224: }
225:
226: return $this->_fields;
227: }
228:
229: 230: 231: 232:
233: public function getLastInsertedId() {
234: return $this->_lastInsertedId;
235: }
236:
237: 238: 239: 240:
241: public function setLastInsertedId($_lastInsertedId) {
242: $this->_lastInsertedId = $_lastInsertedId;
243: }
244:
245: 246: 247: 248: 249: 250:
251: public function getValues() {
252: $values = array();
253: foreach ($this->getFields() as $pifaField) {
254:
255: try {
256: $isStored = NULL !== $pifaField->getDbDataType();
257: } catch (PifaException $e) {
258: $isStored = false;
259: }
260: if (false === $isStored) {
261: continue;
262: }
263: $values[$pifaField->get('column_name')] = $pifaField->getValue();
264: }
265:
266: return $values;
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279:
280: public function setValues(array $values = NULL, $clear = false) {
281: if (NULL === $values) {
282: return;
283: }
284:
285: foreach ($this->getFields() as $pifaField) {
286: $columnName = $pifaField->get('column_name');
287: if (array_key_exists($columnName, $values)) {
288: $value = $values[$columnName];
289: $pifaField->setValue($value);
290: } else if (true === $clear) {
291: $pifaField->setValue(NULL);
292: }
293: }
294: }
295:
296: 297: 298: 299: 300: 301:
302: public function getFiles() {
303: $files = array();
304: foreach ($this->getFields() as $pifaField) {
305:
306: if (PifaField::INPUTFILE !== cSecurity::toInteger($pifaField->get('field_type'))) {
307: continue;
308: }
309: $files[$pifaField->get('column_name')] = $pifaField->getFile();
310: }
311:
312: return $files;
313: }
314:
315: 316: 317: 318: 319:
320: public function setFiles(array $files = NULL) {
321: if (NULL === $files) {
322: return;
323: }
324:
325: foreach ($this->getFields() as $pifaField) {
326:
327: if (PifaField::INPUTFILE !== cSecurity::toInteger($pifaField->get('field_type'))) {
328: continue;
329: }
330: $columnName = $pifaField->get('column_name');
331: if (array_key_exists($columnName, $files)) {
332: $file = $files[$columnName];
333: $pifaField->setFile($file);
334:
335: $pifaField->setValue($file['name']);
336: }
337: }
338: }
339:
340: 341: 342:
343: public function getLastError() {
344: return $this->lasterror;
345: }
346:
347: 348:
349: public function fromForm() {
350:
351:
352: switch (cString::toUpperCase($this->get('method'))) {
353: case 'GET':
354: $this->setValues($_GET);
355: break;
356: case 'POST':
357: $this->setValues($_POST);
358: if (isset($_FILES)) {
359: $this->setFiles($_FILES);
360: }
361: break;
362: }
363: }
364:
365: 366: 367: 368: 369: 370:
371: public function toHtml(array $opt = NULL) {
372:
373:
374: $opt = array_merge(array(
375:
376: 'name' => 'pifa-form',
377: 'action' => 'main.php',
378: 'method' => $this->get('method'),
379: 'class' => 'pifa-form jqtransform'
380: ), $opt);
381: $idform = $this->get('idform');
382: $headline = '';
383: if(isset($opt['headline']) && cString::getStringLength($opt['headline']) > 0) {
384: $headline = '<h1 class="pifa-headline">' . $opt['headline'] . '</h1>';
385: }
386:
387:
388: $htmlForm = new cHTMLForm($opt['name'], $opt['action'], $opt['method'], $opt['class']);
389:
390:
391: $htmlForm->removeAttribute('id')->setID('pifa-form-' . $idform);
392:
393:
394:
395: $htmlForm->appendContent("<input type=\"hidden\" name=\"idform\" value=\"$idform\">");
396:
397:
398: foreach ($this->getFields() as $pifaField) {
399:
400: if (PifaField::INPUTFILE === cSecurity::toInteger($pifaField->get('field_type'))) {
401: $htmlForm->setAttribute('enctype', 'multipart/form-data');
402: }
403: $errors = $this->getErrors();
404: $htmlField = $pifaField->toHtml($errors);
405: if (NULL !== $htmlField) {
406: $htmlForm->appendContent($htmlField);
407: }
408: }
409: $htmlForm->appendContent("\n");
410:
411: return $headline . $htmlForm->render();
412: }
413:
414: 415: 416: 417: 418: 419:
420: public function validate() {
421:
422:
423: $errors = array();
424: foreach ($this->getFields() as $pifaField) {
425: try {
426: $pifaField->validate();
427: } catch (PifaValidationException $e) {
428:
429: foreach ($e->getErrors() as $idfield => $error) {
430: $errors[$idfield] = $error;
431: }
432: }
433: }
434:
435:
436: if (0 < count($errors)) {
437:
438:
439: throw new PifaValidationException($errors);
440: }
441: }
442:
443: 444: 445: 446: 447: 448: 449: 450: 451: 452:
453: public function store() {
454: if (is_null($this->modifiedValues)) {
455: return true;
456: } else {
457: return parent::store();
458: }
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public function storeData() {
469: $cfg = cRegistry::getConfig();
470:
471:
472: $values = $this->getValues();
473:
474:
475: foreach ($values as $column => $value) {
476: if (is_array($value)) {
477: $values[$column] = implode(',', $value);
478: }
479: }
480:
481:
482: $db = cRegistry::getDb();
483:
484:
485: $sql = $db->buildInsert($this->get('data_table'), $values);
486:
487: if (NULL === $db->connect()) {
488: $msg = Pifa::i18n('DATABASE_CONNECT_ERROR');
489: throw new PifaDatabaseException($msg);
490: }
491: if (0 === cString::getStringLength(trim($sql))) {
492: $msg = Pifa::i18n('SQL_BUILD_ERROR');
493: throw new PifaDatabaseException($msg);
494: }
495:
496:
497: if (false === $db->query($sql)) {
498: $msg = Pifa::i18n('VALUE_STORE_ERROR');
499: throw new PifaDatabaseException($msg);
500: }
501:
502:
503: $lastInsertedId = $db->getLastInsertedId();
504:
505: $this->setLastInsertedId($lastInsertedId);
506:
507:
508: $files = $this->getFiles();
509: foreach ($this->getFiles() as $column => $file) {
510: if (!is_array($file)) {
511: continue;
512: }
513: $tmpName = $file['tmp_name'];
514:
515: if (0 === cString::getStringLength($tmpName)) {
516: continue;
517: }
518: $destPath = $cfg['path']['contenido_cache'] . 'form_assistant/';
519:
520: if (!cDirHandler::create($destPath)) {
521: $msg = Pifa::i18n('FOLDER_CREATE_ERROR');
522: throw new PifaException($msg);
523: }
524: $destName = $this->get('data_table') . '_' . $lastInsertedId . '_' . $column;
525: $destName = preg_replace('/[^a-z0-9_]+/i', '_', $destName);
526: if (false === move_uploaded_file($tmpName, $destPath . $destName)) {
527: $msg = Pifa::i18n('FILE_STORE_ERROR');
528: throw new PifaException($msg);
529: }
530: }
531: }
532:
533: 534: 535: 536:
537: public function toMailRecipient(array $opt) {
538: if (0 == cString::getStringLength(trim($opt['from']))) {
539: $msg = Pifa::i18n('MISSING_SENDER_ADDRESS');
540: throw new PifaMailException($msg);
541: }
542: if (0 == cString::getStringLength(trim($opt['fromName']))) {
543: $msg = Pifa::i18n('MISSING_SENDER_NAME');
544: throw new PifaMailException($msg);
545: }
546: if (0 == cString::getStringLength(trim($opt['to']))) {
547: $msg = Pifa::i18n('MISSING_RECIPIENT_ADDRESS');
548: throw new PifaMailException($msg);
549: }
550: if (0 == cString::getStringLength(trim($opt['subject']))) {
551: $msg = Pifa::i18n('MISSING_SUBJECT');
552: throw new PifaMailException($msg);
553: }
554: if (0 == cString::getStringLength(trim($opt['body']))) {
555: $msg = Pifa::i18n('MISSING_EMAIL_BODY');
556: throw new PifaMailException($msg);
557: }
558:
559:
560:
561: try {
562: $mailer = new cMailer();
563: $message = Swift_Message::newInstance($opt['subject'], $opt['body'], 'text/plain', $opt['charSet']);
564:
565:
566: if (array_key_exists('attachmentNames', $opt)) {
567: if (is_array($opt['attachmentNames'])) {
568: $values = $this->getValues();
569: foreach ($opt['attachmentNames'] as $column => $path) {
570: if (!file_exists($path)) {
571: continue;
572: }
573: $attachment = Swift_Attachment::fromPath($path);
574: $filename = $values[$column];
575: $attachment->setFilename($filename);
576: $message->attach($attachment);
577: }
578: }
579: }
580:
581:
582: if (array_key_exists('attachmentStrings', $opt)) {
583: if (is_array($opt['attachmentStrings'])) {
584: foreach ($opt['attachmentStrings'] as $filename => $string) {
585:
586: $attachment = Swift_Attachment::newInstance($string, $filename, 'text/csv');
587: $message->attach($attachment);
588: }
589: }
590: }
591:
592:
593: $message->addFrom($opt['from'], $opt['fromName']);
594:
595:
596: $to = explode(',', $opt['to']);
597: $message->setTo(array_combine($to, $to));
598: } catch (Exception $e) {
599: throw new PifaException($e->getMessage());
600: }
601:
602: if (!$mailer->send($message)) {
603: $msg = mi18n("PIFA_MAIL_ERROR_SUFFIX");
604: throw new PifaMailException($msg);
605: }
606: }
607:
608: 609: 610: 611: 612: 613: 614:
615: public function getData() {
616: if (!$this->isLoaded()) {
617: $msg = Pifa::i18n('FORM_LOAD_ERROR');
618: throw new PifaException($msg);
619: }
620:
621: $db = cRegistry::getDb();
622:
623:
624: $tableName = $this->get('data_table');
625: if (!$this->existsTable($tableName, false)) {
626: $msg = Pifa::i18n('MISSING_TABLE_ERROR');
627: throw new PifaException($msg);
628: }
629:
630:
631: $sql = "-- PifaForm->getData()
632: SELECT
633: *
634: FROM
635: `$tableName`
636: ;";
637:
638: if (false === $db->query($sql)) {
639: return array();
640: }
641:
642: if (0 === $db->numRows()) {
643: return array();
644: }
645:
646: $data = array();
647: while ($db->nextRecord()) {
648: $data[] = $db->toArray();
649: }
650:
651: return $data;
652: }
653:
654: 655: 656: 657: 658: 659: 660: 661: 662:
663: public function getDataAsCsv($optionally = 'OPTIONALLY') {
664: $cfg = cRegistry::getConfig();
665:
666: if (in_array($cfg['db']['connection']['host'], array(
667: '127.0.0.1',
668: 'localhost'
669: ))) {
670:
671:
672:
673:
674:
675:
676: $out = $this->_getCsvFromRemoteDatabaseServer();
677: } else {
678: $out = $this->_getCsvFromRemoteDatabaseServer();
679: }
680:
681:
682: return $out;
683: }
684:
685: 686: 687: 688: 689: 690:
691: private function _getCsvFromLocalDatabaseServer($optionally = 'OPTIONALLY') {
692:
693:
694: if (!$this->isLoaded()) {
695: $msg = Pifa::i18n('FORM_LOAD_ERROR');
696: throw new PifaException($msg);
697: }
698:
699:
700: $tableName = $this->get('data_table');
701: if (!$this->existsTable($tableName, false)) {
702: $msg = Pifa::i18n('MISSING_TABLE_ERROR');
703: throw new PifaException($msg);
704: }
705:
706:
707: if ('OPTIONALLY' !== $optionally) {
708: $optionally = '';
709: }
710:
711:
712: $cfg = cRegistry::getConfig();
713: $filename = tempnam($cfg['path']['contenido_cache'], 'PIFA_');
714: unlink($filename);
715:
716:
717: $sql = "-- PifaForm->_getCsvFromLocalDatabaseServer()
718: SELECT
719: *
720: INTO OUTFILE
721: '$filename'
722: FIELDS TERMINATED BY
723: ','
724: $optionally ENCLOSED BY
725: '\"'
726: ESCAPED BY
727: '\\\\'
728: LINES TERMINATED BY
729: '\\n'
730: FROM
731: `$tableName`
732: ;";
733:
734:
735: cRegistry::getDb()->query($sql);
736:
737:
738: $out = cFileHandler::read($filename);
739:
740:
741: unlink($filename);
742:
743: return $out;
744: }
745:
746: 747: 748: 749: 750: 751:
752: private function _getCsvFromRemoteDatabaseServer() {
753:
754:
755: $columns = array();
756:
757: array_push($columns, 'id');
758:
759: if (true === (bool) $this->get('with_timestamp')) {
760: array_push($columns, 'pifa_timestamp');
761: }
762: foreach ($this->getFields() as $index => $pifaField) {
763:
764: if (cString::getStringLength(trim($pifaField->get('column_name'))) > 0) {
765: $columns[] = $pifaField->get('column_name');
766: }
767: }
768:
769: $out = '';
770:
771:
772: foreach ($columns as $index => $columnName) {
773: if (0 < $index) {
774: $out .= ';';
775: }
776: $out .= $columnName;
777: }
778:
779: function pifa_form_get_literal_line_endings($value) {
780: $value = str_replace("\n", '\n', $value);
781: $value = str_replace("\r", '\r', $value);
782: $value = "\"$value\"";
783: return $value;
784: }
785:
786:
787: foreach ($this->getData() as $row) {
788:
789: $row = array_map('pifa_form_get_literal_line_endings', $row);
790:
791: foreach ($columns as $index => $columnName) {
792: $out .= 0 === $index? "\n" : ';';
793: $out .= $row[$columnName];
794: }
795: }
796:
797: return $out;
798: }
799:
800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813:
814: public function getCsv($oneRowPerField = false, array $additionalFields = NULL) {
815:
816:
817: $data = $this->getValues();
818:
819:
820: if (NULL !== $additionalFields) {
821: $data = array_merge($data, $additionalFields);
822: }
823:
824:
825: $toCsv = '';
826:
827:
828: $data = array_map(function($in) {
829: return implode(',', $in);;
830: }, $data);
831:
832:
833: if (!$oneRowPerField) {
834: $data = array(
835: array_keys($data),
836: array_values($data)
837: );
838: }
839:
840:
841: $csv = '';
842:
843: $total = 0;
844: if (false !== $tmpfile = tmpfile()) {
845: foreach ($data as $line) {
846: $length = fputcsv($tmpfile, $data, ';', '"');
847: if (false !== $length) {
848: $total += $length;
849: }
850: }
851: }
852:
853: if (0 < $total) {
854: $csv = fread($tmpfile, $length);
855: fclose($tmpfile);
856: }
857:
858: return $csv;
859: }
860:
861: 862: 863: 864: 865:
866: public function existsTable($tableName, $bySchema = false) {
867: $cfg = cRegistry::getConfig();
868:
869:
870: if (true === $bySchema) {
871:
872: $sql = "-- PifaForm->existsTable()
873: SELECT
874: *
875: FROM
876: `information_schema.tables`
877: WHERE
878: table_schema = '" . $cfg['db']['connection']['database'] . "'
879: AND table_name = '$tableName'
880: ;";
881: } else {
882:
883: $sql = "-- PifaForm->existsTable()
884: SHOW TABLES
885: LIKE
886: '$tableName';
887: ;";
888: }
889:
890:
891: $db = cRegistry::getDb();
892: if (false === $db->query($sql)) {
893: $msg = Pifa::i18n('TABLE_CHECK_ERROR');
894: $msg = sprintf($msg, $db->getErrorMessage());
895: throw new PifaException($msg);
896: }
897:
898: return (bool) (0 !== $db->numRows());
899: }
900:
901: 902: 903: 904: 905: 906: 907: 908: 909: 910: 911: 912:
913: public function createTable($withTimestamp) {
914: if (!$this->isLoaded()) {
915: $msg = Pifa::i18n('FORM_LOAD_ERROR');
916: throw new PifaException($msg);
917: }
918:
919:
920: $tableName = $this->get('data_table');
921: if ($this->existsTable($tableName)) {
922: $msg = Pifa::i18n('TABLE_EXISTS_ERROR');
923: $msg = sprintf($msg, $tableName);
924: throw new PifaException($msg);
925: }
926:
927:
928: $createDefinitions = array();
929: array_push($createDefinitions, "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'primary key'");
930: if ($withTimestamp) {
931: array_push($createDefinitions, "pifa_timestamp TIMESTAMP NOT NULL COMMENT 'automatic PIFA timestamp'");
932: }
933:
934: if (NULL === $this->_fields) {
935: $this->loadFields();
936: }
937: foreach ($this->_fields as $pifaField) {
938: $columnName = $pifaField->get('column_name');
939:
940: if (0 === cString::getStringLength(trim($columnName))) {
941: continue;
942: }
943: $dataType = $pifaField->getDbDataType();
944: array_push($createDefinitions, "`$columnName` $dataType");
945: }
946: $createDefinitions = join(',', $createDefinitions);
947:
948:
949: $sql = "-- PifaForm->createTable()
950: CREATE TABLE
951: -- IF NOT EXISTS
952: `$tableName`
953: ($createDefinitions)
954: ENGINE=MyISAM
955: DEFAULT CHARSET=utf8
956: ;";
957:
958:
959: $db = cRegistry::getDb();
960: if (false === $db->query($sql)) {
961: $msg = Pifa::i18n('TABLE_CREATE_ERROR');
962: throw new PifaException($msg);
963: }
964: }
965:
966: 967: 968: 969: 970: 971: 972: 973: 974: 975: 976: 977:
978: public function alterTable($oldTableName, $oldWithTimestamp) {
979: if (!$this->isLoaded()) {
980: $msg = Pifa::i18n('FORM_LOAD_ERROR');
981: throw new PifaException($msg);
982: }
983:
984:
985: $tableName = $this->get('data_table');
986:
987:
988: if ($oldTableName !== $tableName) {
989: if ($this->existsTable($tableName)) {
990: $this->set('data_table', $oldTableName);
991: } else {
992: $sql = "-- PifaForm->alterTable()
993: RENAME TABLE
994: `$oldTableName`
995: TO
996: `$tableName`
997: ;";
998: cRegistry::getDb()->query($sql);
999: }
1000: }
1001:
1002:
1003: $withTimestamp = $this->get('with_timestamp');
1004: if ($oldWithTimestamp != $withTimestamp) {
1005: if ($withTimestamp) {
1006: $sql = "-- PifaForm->alterTable()
1007: ALTER TABLE
1008: `$tableName`
1009: ADD
1010: `pifa_timestamp`
1011: TIMESTAMP
1012: NOT NULL
1013: COMMENT
1014: 'automatic PIFA timestamp'
1015: AFTER id
1016: ;";
1017: } else {
1018: $sql = "-- PifaForm->alterTable()
1019: ALTER TABLE
1020: `$tableName`
1021: DROP
1022: `pifa_timestamp`
1023: ;";
1024: }
1025: cRegistry::getDb()->query($sql);
1026: }
1027: }
1028:
1029: 1030: 1031: 1032: 1033: 1034: 1035:
1036: public function storeColumn(PifaField $pifaField, $oldColumnName) {
1037: if (!$this->isLoaded()) {
1038: $msg = Pifa::i18n('FORM_LOAD_ERROR');
1039: throw new PifaException($msg);
1040: }
1041: if (!$pifaField->isLoaded()) {
1042: $msg = Pifa::i18n('FIELD_LOAD_ERROR');
1043: throw new PifaException($msg);
1044: }
1045:
1046: $columnName = $pifaField->get('column_name');
1047: $dataType = $pifaField->getDbDataType();
1048:
1049: if (0 === cString::getStringLength(trim($oldColumnName))) {
1050: if (0 === cString::getStringLength(trim($columnName))) {
1051:
1052: } else {
1053: $this->addColumn($columnName, $dataType);
1054: }
1055: } else {
1056: if (0 === cString::getStringLength(trim($columnName))) {
1057: $this->dropColumn($oldColumnName);
1058: } else {
1059: $this->changeColumn($columnName, $dataType, $oldColumnName);
1060: }
1061: }
1062: }
1063:
1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072:
1073: public function changeColumn($columnName, $dataType, $oldColumnName) {
1074: $tableName = $this->get('data_table');
1075:
1076: if ($oldColumnName === $columnName) {
1077: return;
1078: }
1079: if (true === $this->_existsColumn($columnName)) {
1080: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1081: $msg = sprintf($msg, $columnName);
1082: throw new PifaException($msg);
1083: }
1084: if (NULL === $dataType) {
1085: return;
1086: }
1087:
1088: $sql = "-- PifaForm->changeColumn()
1089: ALTER TABLE
1090: `$tableName`
1091: CHANGE
1092: `$oldColumnName`
1093: `$columnName` $dataType
1094: ;";
1095:
1096: $db = cRegistry::getDb();
1097: if (false === $db->query($sql)) {
1098: $msg = Pifa::i18n('COLUMN_ALTER_ERROR');
1099: throw new PifaException($msg);
1100: }
1101: }
1102:
1103: 1104: 1105: 1106: 1107: 1108:
1109: public function dropColumn($columnName) {
1110: $tableName = $this->get('data_table');
1111: if (false === $this->_existsColumn($columnName)) {
1112: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1113: $msg = sprintf($msg, $columnName);
1114: throw new PifaException($msg);
1115: }
1116:
1117: $sql = "-- PifaForm->dropColumn()
1118: ALTER TABLE
1119: `$tableName`
1120: DROP
1121: `$columnName`
1122: ;";
1123:
1124: $db = cRegistry::getDb();
1125: if (false === $db->query($sql)) {
1126: $msg = Pifa::i18n('COLUMN_DROP_ERROR');
1127: throw new PifaException($msg);
1128: }
1129: }
1130:
1131: 1132: 1133: 1134: 1135: 1136: 1137:
1138: public function addColumn($columnName, $dataType) {
1139: $tableName = $this->get('data_table');
1140: if (true === $this->_existsColumn($columnName)) {
1141: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1142: $msg = sprintf($msg, $columnName);
1143: throw new PifaException($msg);
1144: }
1145: if (NULL === $dataType) {
1146: return;
1147: }
1148:
1149: $sql = "-- PifaForm->addColumn()
1150: ALTER TABLE
1151: `$tableName`
1152: ADD
1153: `$columnName` $dataType
1154: ;";
1155:
1156: $db = cRegistry::getDb();
1157: if (false === $db->query($sql)) {
1158: $msg = Pifa::i18n('COLUMN_ADD_ERROR');
1159: throw new PifaException($msg);
1160: }
1161: }
1162:
1163: 1164: 1165: 1166: 1167: 1168:
1169: protected function _existsColumn($columnName) {
1170: $tableName = $this->get('data_table');
1171: $sql = "-- PifaForm->_existsColumn()
1172: SHOW FIELDS FROM
1173: `$tableName`
1174: ;";
1175:
1176: $db = cRegistry::getDb();
1177: if (false === $db->query($sql)) {
1178: $msg = Pifa::i18n('COLUMNS_LOAD_ERROR');
1179: throw new PifaException($msg);
1180: }
1181:
1182:
1183: while (false !== $db->nextRecord()) {
1184: $field = $db->toArray();
1185: if (cString::toLowerCase($field['Field']) == cString::toLowerCase($columnName)) {
1186: return true;
1187: }
1188: }
1189:
1190: return false;
1191: }
1192:
1193: 1194: 1195: 1196:
1197: public function delete() {
1198: $cfg = cRegistry::getConfig();
1199: $db = cRegistry::getDb();
1200:
1201: if (!$this->isLoaded()) {
1202: $msg = Pifa::i18n('FORM_LOAD_ERROR');
1203: throw new PifaException($msg);
1204: }
1205:
1206:
1207: $sql = "-- PifaForm->delete()
1208: DELETE FROM
1209: `" . cRegistry::getDbTableName('pifa_form') . "`
1210: WHERE
1211: idform = " . cSecurity::toInteger($this->get('idform')) . "
1212: ;";
1213: if (false === $db->query($sql)) {
1214: $msg = Pifa::i18n('FORM_DELETE_ERROR');
1215: throw new PifaException($msg);
1216: }
1217:
1218:
1219: $sql = "-- PifaForm->delete()
1220: DELETE FROM
1221: `" . cRegistry::getDbTableName('pifa_field') . "`
1222: WHERE
1223: idform = " . cSecurity::toInteger($this->get('idform')) . "
1224: ;";
1225: if (false === $db->query($sql)) {
1226: $msg = Pifa::i18n('FIELDS_DELETE_ERROR');
1227: throw new PifaException($msg);
1228: }
1229:
1230:
1231: if (0 < cString::getStringLength(trim($this->get('data_table')))) {
1232: $sql = "-- PifaForm->delete()
1233: DROP TABLE IF EXISTS
1234: `" . cSecurity::toString($this->get('data_table')) . "`
1235: ;";
1236: if (false === $db->query($sql)) {
1237: $msg = Pifa::i18n('TABLE_DROP_ERROR');
1238: throw new PifaException($msg);
1239: }
1240: }
1241: }
1242:
1243: 1244: 1245: 1246:
1247: public function getTableName() {
1248: return $this->get('data_table');
1249: }
1250:
1251: }
1252: