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 (strtoupper($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:
383:
384: $htmlForm = new cHTMLForm($opt['name'], $opt['action'], $opt['method'], $opt['class']);
385:
386:
387: $htmlForm->removeAttribute('id')->setID('pifa-form-' . $idform);
388:
389:
390:
391: $htmlForm->appendContent("<input type=\"hidden\" name=\"idform\" value=\"$idform\">");
392:
393:
394: foreach ($this->getFields() as $pifaField) {
395:
396: if (PifaField::INPUTFILE === cSecurity::toInteger($pifaField->get('field_type'))) {
397: $htmlForm->setAttribute('enctype', 'multipart/form-data');
398: }
399: $errors = $this->getErrors();
400: $htmlField = $pifaField->toHtml($errors);
401: if (NULL !== $htmlField) {
402: $htmlForm->appendContent($htmlField);
403: }
404: }
405: $htmlForm->appendContent("\n");
406:
407: return $htmlForm->render();
408: }
409:
410: 411: 412: 413: 414: 415:
416: public function validate() {
417:
418:
419: $errors = array();
420: foreach ($this->getFields() as $pifaField) {
421: try {
422: $pifaField->validate();
423: } catch (PifaValidationException $e) {
424:
425: foreach ($e->getErrors() as $idfield => $error) {
426: $errors[$idfield] = $error;
427: }
428: }
429: }
430:
431:
432: if (0 < count($errors)) {
433:
434:
435: throw new PifaValidationException($errors);
436: }
437: }
438:
439: 440: 441: 442: 443: 444: 445: 446: 447: 448:
449: public function store() {
450: if (is_null($this->modifiedValues)) {
451: return true;
452: } else {
453: return parent::store();
454: }
455: }
456:
457: 458: 459: 460: 461: 462: 463:
464: public function storeData() {
465: $cfg = cRegistry::getConfig();
466:
467:
468: $values = $this->getValues();
469:
470:
471: foreach ($values as $column => $value) {
472: if (is_array($value)) {
473: $values[$column] = implode(',', $value);
474: }
475: }
476:
477:
478: $db = cRegistry::getDb();
479:
480:
481: $sql = $db->buildInsert($this->get('data_table'), $values);
482:
483: if (NULL === $db->connect()) {
484: $msg = Pifa::i18n('DATABASE_CONNECT_ERROR');
485: throw new PifaDatabaseException($msg);
486: }
487: if (0 === strlen(trim($sql))) {
488: $msg = Pifa::i18n('SQL_BUILD_ERROR');
489: throw new PifaDatabaseException($msg);
490: }
491:
492:
493: if (false === $db->query($sql)) {
494: $msg = Pifa::i18n('VALUE_STORE_ERROR');
495: throw new PifaDatabaseException($msg);
496: }
497:
498:
499: $lastInsertedId = $db->getLastInsertedId();
500:
501: $this->setLastInsertedId($lastInsertedId);
502:
503:
504: $files = $this->getFiles();
505: foreach ($this->getFiles() as $column => $file) {
506: if (!is_array($file)) {
507: continue;
508: }
509: $tmpName = $file['tmp_name'];
510:
511: if (0 === strlen($tmpName)) {
512: continue;
513: }
514: $destPath = $cfg['path']['contenido_cache'] . 'form_assistant/';
515:
516: if (!cDirHandler::create($destPath)) {
517: $msg = Pifa::i18n('FOLDER_CREATE_ERROR');
518: throw new PifaException($msg);
519: }
520: $destName = $this->get('data_table') . '_' . $lastInsertedId . '_' . $column;
521: $destName = preg_replace('/[^a-z0-9_]+/i', '_', $destName);
522: if (false === move_uploaded_file($tmpName, $destPath . $destName)) {
523: $msg = Pifa::i18n('FILE_STORE_ERROR');
524: throw new PifaException($msg);
525: }
526: }
527: }
528:
529: 530: 531: 532:
533: public function toMailRecipient(array $opt) {
534: if (0 == strlen(trim($opt['from']))) {
535: $msg = Pifa::i18n('MISSING_SENDER_ADDRESS');
536: throw new PifaMailException($msg);
537: }
538: if (0 == strlen(trim($opt['fromName']))) {
539: $msg = Pifa::i18n('MISSING_SENDER_NAME');
540: throw new PifaMailException($msg);
541: }
542: if (0 == strlen(trim($opt['to']))) {
543: $msg = Pifa::i18n('MISSING_RECIPIENT_ADDRESS');
544: throw new PifaMailException($msg);
545: }
546: if (0 == strlen(trim($opt['subject']))) {
547: $msg = Pifa::i18n('MISSING_SUBJECT');
548: throw new PifaMailException($msg);
549: }
550: if (0 == strlen(trim($opt['body']))) {
551: $msg = Pifa::i18n('MISSING_EMAIL_BODY');
552: throw new PifaMailException($msg);
553: }
554:
555:
556:
557: try {
558: $mailer = new cMailer();
559: $message = Swift_Message::newInstance($opt['subject'], $opt['body'], 'text/plain', $opt['charSet']);
560:
561:
562: if (array_key_exists('attachmentNames', $opt)) {
563: if (is_array($opt['attachmentNames'])) {
564: $values = $this->getValues();
565: foreach ($opt['attachmentNames'] as $column => $path) {
566: if (!file_exists($path)) {
567: continue;
568: }
569: $attachment = Swift_Attachment::fromPath($path);
570: $filename = $values[$column];
571: $attachment->setFilename($filename);
572: $message->attach($attachment);
573: }
574: }
575: }
576:
577:
578: if (array_key_exists('attachmentStrings', $opt)) {
579: if (is_array($opt['attachmentStrings'])) {
580: foreach ($opt['attachmentStrings'] as $filename => $string) {
581:
582: $attachment = Swift_Attachment::newInstance($string, $filename, 'text/csv');
583: $message->attach($attachment);
584: }
585: }
586: }
587:
588:
589: $message->addFrom($opt['from'], $opt['fromName']);
590:
591:
592: $to = explode(',', $opt['to']);
593: $message->setTo(array_combine($to, $to));
594: } catch (Exception $e) {
595: throw new PifaException($e->getMessage());
596: }
597:
598: if (!$mailer->send($message)) {
599: $msg = mi18n("PIFA_MAIL_ERROR_SUFFIX");
600: throw new PifaMailException($msg);
601: }
602: }
603:
604: 605: 606: 607: 608: 609: 610:
611: public function getData() {
612: if (!$this->isLoaded()) {
613: $msg = Pifa::i18n('FORM_LOAD_ERROR');
614: throw new PifaException($msg);
615: }
616:
617: $db = cRegistry::getDb();
618:
619:
620: $tableName = $this->get('data_table');
621: if (!$this->existsTable($tableName, false)) {
622: $msg = Pifa::i18n('MISSING_TABLE_ERROR');
623: throw new PifaException($msg);
624: }
625:
626:
627: $sql = "-- PifaForm->getData()
628: SELECT
629: *
630: FROM
631: `$tableName`
632: ;";
633:
634: if (false === $db->query($sql)) {
635: return array();
636: }
637:
638: if (0 === $db->numRows()) {
639: return array();
640: }
641:
642: $data = array();
643: while ($db->nextRecord()) {
644: $data[] = $db->toArray();
645: }
646:
647: return $data;
648: }
649:
650: 651: 652: 653: 654: 655: 656: 657: 658:
659: public function getDataAsCsv($optionally = 'OPTIONALLY') {
660: $cfg = cRegistry::getConfig();
661:
662: if (in_array($cfg['db']['connection']['host'], array(
663: '127.0.0.1',
664: 'localhost'
665: ))) {
666:
667:
668:
669:
670:
671:
672: $out = $this->_getCsvFromRemoteDatabaseServer();
673: } else {
674: $out = $this->_getCsvFromRemoteDatabaseServer();
675: }
676:
677:
678: return $out;
679: }
680:
681: 682: 683: 684: 685: 686:
687: private function _getCsvFromLocalDatabaseServer($optionally = 'OPTIONALLY') {
688:
689:
690: if (!$this->isLoaded()) {
691: $msg = Pifa::i18n('FORM_LOAD_ERROR');
692: throw new PifaException($msg);
693: }
694:
695:
696: $tableName = $this->get('data_table');
697: if (!$this->existsTable($tableName, false)) {
698: $msg = Pifa::i18n('MISSING_TABLE_ERROR');
699: throw new PifaException($msg);
700: }
701:
702:
703: if ('OPTIONALLY' !== $optionally) {
704: $optionally = '';
705: }
706:
707:
708: $cfg = cRegistry::getConfig();
709: $filename = tempnam($cfg['path']['contenido_cache'], 'PIFA_');
710: unlink($filename);
711:
712:
713: $sql = "-- PifaForm->_getCsvFromLocalDatabaseServer()
714: SELECT
715: *
716: INTO OUTFILE
717: '$filename'
718: FIELDS TERMINATED BY
719: ','
720: $optionally ENCLOSED BY
721: '\"'
722: ESCAPED BY
723: '\\\\'
724: LINES TERMINATED BY
725: '\\n'
726: FROM
727: `$tableName`
728: ;";
729:
730:
731: cRegistry::getDb()->query($sql);
732:
733:
734: $out = cFileHandler::read($filename);
735:
736:
737: unlink($filename);
738:
739: return $out;
740: }
741:
742: 743: 744: 745: 746: 747:
748: private function _getCsvFromRemoteDatabaseServer() {
749:
750:
751: $columns = array();
752:
753: array_push($columns, 'id');
754:
755: if (true === (bool) $this->get('with_timestamp')) {
756: array_push($columns, 'pifa_timestamp');
757: }
758: foreach ($this->getFields() as $index => $pifaField) {
759:
760: if (strlen(trim($pifaField->get('column_name'))) > 0) {
761: $columns[] = $pifaField->get('column_name');
762: }
763: }
764:
765: $out = '';
766:
767:
768: foreach ($columns as $index => $columnName) {
769: if (0 < $index) {
770: $out .= ';';
771: }
772: $out .= $columnName;
773: }
774:
775: function pifa_form_get_literal_line_endings($value) {
776: $value = str_replace("\n", '\n', $value);
777: $value = str_replace("\r", '\r', $value);
778: $value = "\"$value\"";
779: return $value;
780: }
781:
782:
783: foreach ($this->getData() as $row) {
784:
785: $row = array_map('pifa_form_get_literal_line_endings', $row);
786:
787: foreach ($columns as $index => $columnName) {
788: $out .= 0 === $index? "\n" : ';';
789: $out .= $row[$columnName];
790: }
791: }
792:
793: return $out;
794: }
795:
796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809:
810: public function getCsv($oneRowPerField = false, array $additionalFields = NULL) {
811:
812:
813: $data = $this->getValues();
814:
815:
816: if (NULL !== $additionalFields) {
817: $data = array_merge($data, $additionalFields);
818: }
819:
820:
821: $toCsv = '';
822:
823:
824: $implode = 'return implode(\',\', $in);';
825: $implode = create_function('$in', $toCsv);
826: $data = array_map($implode, $data);
827:
828:
829: if (!$oneRowPerField) {
830: $data = array(
831: array_keys($data),
832: array_values($data)
833: );
834: }
835:
836:
837: $csv = '';
838:
839: $total = 0;
840: if (false !== $tmpfile = tmpfile()) {
841: foreach ($data as $line) {
842: $length = fputcsv($tmpfile, $data, ';', '"');
843: if (false !== $length) {
844: $total += $length;
845: }
846: }
847: }
848:
849: if (0 < $total) {
850: $csv = fread($tmpfile, $length);
851: fclose($tmpfile);
852: }
853:
854: return $csv;
855: }
856:
857: 858: 859: 860: 861:
862: public function existsTable($tableName, $bySchema = false) {
863: $cfg = cRegistry::getConfig();
864:
865:
866: if (true === $bySchema) {
867:
868: $sql = "-- PifaForm->existsTable()
869: SELECT
870: *
871: FROM
872: `information_schema.tables`
873: WHERE
874: table_schema = '" . $cfg['db']['connection']['database'] . "'
875: AND table_name = '$tableName'
876: ;";
877: } else {
878:
879: $sql = "-- PifaForm->existsTable()
880: SHOW TABLES
881: LIKE
882: '$tableName';
883: ;";
884: }
885:
886:
887: $db = cRegistry::getDb();
888: if (false === $db->query($sql)) {
889: $msg = Pifa::i18n('TABLE_CHECK_ERROR');
890: $msg = sprintf($msg, $db->getErrorMessage());
891: throw new PifaException($msg);
892: }
893:
894: return (bool) (0 !== $db->numRows());
895: }
896:
897: 898: 899: 900: 901: 902: 903: 904: 905: 906: 907: 908:
909: public function createTable($withTimestamp) {
910: if (!$this->isLoaded()) {
911: $msg = Pifa::i18n('FORM_LOAD_ERROR');
912: throw new PifaException($msg);
913: }
914:
915:
916: $tableName = $this->get('data_table');
917: if ($this->existsTable($tableName)) {
918: $msg = Pifa::i18n('TABLE_EXISTS_ERROR');
919: $msg = sprintf($msg, $tableName);
920: throw new PifaException($msg);
921: }
922:
923:
924: $createDefinitions = array();
925: array_push($createDefinitions, "id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'primary key'");
926: if ($withTimestamp) {
927: array_push($createDefinitions, "pifa_timestamp TIMESTAMP NOT NULL COMMENT 'automatic PIFA timestamp'");
928: }
929:
930: if (NULL === $this->_fields) {
931: $this->loadFields();
932: }
933: foreach ($this->_fields as $pifaField) {
934: $columnName = $pifaField->get('column_name');
935:
936: if (0 === strlen(trim($columnName))) {
937: continue;
938: }
939: $dataType = $pifaField->getDbDataType();
940: array_push($createDefinitions, "`$columnName` $dataType");
941: }
942: $createDefinitions = join(',', $createDefinitions);
943:
944:
945: $sql = "-- PifaForm->createTable()
946: CREATE TABLE
947: -- IF NOT EXISTS
948: `$tableName`
949: ($createDefinitions)
950: ENGINE=MyISAM
951: DEFAULT CHARSET=utf8
952: ;";
953:
954:
955: $db = cRegistry::getDb();
956: if (false === $db->query($sql)) {
957: $msg = Pifa::i18n('TABLE_CREATE_ERROR');
958: throw new PifaException($msg);
959: }
960: }
961:
962: 963: 964: 965: 966: 967: 968: 969: 970: 971: 972: 973:
974: public function alterTable($oldTableName, $oldWithTimestamp) {
975: if (!$this->isLoaded()) {
976: $msg = Pifa::i18n('FORM_LOAD_ERROR');
977: throw new PifaException($msg);
978: }
979:
980:
981: $tableName = $this->get('data_table');
982:
983:
984: if ($oldTableName !== $tableName) {
985: if ($this->existsTable($tableName)) {
986: $this->set('data_table', $oldTableName);
987: } else {
988: $sql = "-- PifaForm->alterTable()
989: RENAME TABLE
990: `$oldTableName`
991: TO
992: `$tableName`
993: ;";
994: cRegistry::getDb()->query($sql);
995: }
996: }
997:
998:
999: $withTimestamp = $this->get('with_timestamp');
1000: if ($oldWithTimestamp != $withTimestamp) {
1001: if ($withTimestamp) {
1002: $sql = "-- PifaForm->alterTable()
1003: ALTER TABLE
1004: `$tableName`
1005: ADD
1006: `pifa_timestamp`
1007: TIMESTAMP
1008: NOT NULL
1009: COMMENT
1010: 'automatic PIFA timestamp'
1011: AFTER id
1012: ;";
1013: } else {
1014: $sql = "-- PifaForm->alterTable()
1015: ALTER TABLE
1016: `$tableName`
1017: DROP
1018: `pifa_timestamp`
1019: ;";
1020: }
1021: cRegistry::getDb()->query($sql);
1022: }
1023: }
1024:
1025: 1026: 1027: 1028: 1029: 1030: 1031:
1032: public function storeColumn(PifaField $pifaField, $oldColumnName) {
1033: if (!$this->isLoaded()) {
1034: $msg = Pifa::i18n('FORM_LOAD_ERROR');
1035: throw new PifaException($msg);
1036: }
1037: if (!$pifaField->isLoaded()) {
1038: $msg = Pifa::i18n('FIELD_LOAD_ERROR');
1039: throw new PifaException($msg);
1040: }
1041:
1042: $columnName = $pifaField->get('column_name');
1043: $dataType = $pifaField->getDbDataType();
1044:
1045: if (0 === strlen(trim($oldColumnName))) {
1046: if (0 === strlen(trim($columnName))) {
1047:
1048: } else {
1049: $this->addColumn($columnName, $dataType);
1050: }
1051: } else {
1052: if (0 === strlen(trim($columnName))) {
1053: $this->dropColumn($oldColumnName);
1054: } else {
1055: $this->changeColumn($columnName, $dataType, $oldColumnName);
1056: }
1057: }
1058: }
1059:
1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068:
1069: public function changeColumn($columnName, $dataType, $oldColumnName) {
1070: $tableName = $this->get('data_table');
1071:
1072: if ($oldColumnName === $columnName) {
1073: return;
1074: }
1075: if (true === $this->_existsColumn($columnName)) {
1076: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1077: $msg = sprintf($msg, $columnName);
1078: throw new PifaException($msg);
1079: }
1080: if (NULL === $dataType) {
1081: return;
1082: }
1083:
1084: $sql = "-- PifaForm->changeColumn()
1085: ALTER TABLE
1086: `$tableName`
1087: CHANGE
1088: `$oldColumnName`
1089: `$columnName` $dataType
1090: ;";
1091:
1092: $db = cRegistry::getDb();
1093: if (false === $db->query($sql)) {
1094: $msg = Pifa::i18n('COLUMN_ALTER_ERROR');
1095: throw new PifaException($msg);
1096: }
1097: }
1098:
1099: 1100: 1101: 1102: 1103: 1104:
1105: public function dropColumn($columnName) {
1106: $tableName = $this->get('data_table');
1107: if (false === $this->_existsColumn($columnName)) {
1108: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1109: $msg = sprintf($msg, $columnName);
1110: throw new PifaException($msg);
1111: }
1112:
1113: $sql = "-- PifaForm->dropColumn()
1114: ALTER TABLE
1115: `$tableName`
1116: DROP
1117: `$columnName`
1118: ;";
1119:
1120: $db = cRegistry::getDb();
1121: if (false === $db->query($sql)) {
1122: $msg = Pifa::i18n('COLUMN_DROP_ERROR');
1123: throw new PifaException($msg);
1124: }
1125: }
1126:
1127: 1128: 1129: 1130: 1131: 1132: 1133:
1134: public function addColumn($columnName, $dataType) {
1135: $tableName = $this->get('data_table');
1136: if (true === $this->_existsColumn($columnName)) {
1137: $msg = Pifa::i18n('COLUMN_EXISTS_ERROR');
1138: $msg = sprintf($msg, $columnName);
1139: throw new PifaException($msg);
1140: }
1141: if (NULL === $dataType) {
1142: return;
1143: }
1144:
1145: $sql = "-- PifaForm->addColumn()
1146: ALTER TABLE
1147: `$tableName`
1148: ADD
1149: `$columnName` $dataType
1150: ;";
1151:
1152: $db = cRegistry::getDb();
1153: if (false === $db->query($sql)) {
1154: $msg = Pifa::i18n('COLUMN_ADD_ERROR');
1155: throw new PifaException($msg);
1156: }
1157: }
1158:
1159: 1160: 1161: 1162: 1163: 1164:
1165: protected function _existsColumn($columnName) {
1166: $tableName = $this->get('data_table');
1167: $sql = "-- PifaForm->_existsColumn()
1168: SHOW FIELDS FROM
1169: `$tableName`
1170: ;";
1171:
1172: $db = cRegistry::getDb();
1173: if (false === $db->query($sql)) {
1174: $msg = Pifa::i18n('COLUMNS_LOAD_ERROR');
1175: throw new PifaException($msg);
1176: }
1177:
1178:
1179: while (false !== $db->nextRecord()) {
1180: $field = $db->toArray();
1181: if (strtolower($field['Field']) == strtolower($columnName)) {
1182: return true;
1183: }
1184: }
1185:
1186: return false;
1187: }
1188:
1189: 1190: 1191: 1192:
1193: public function delete() {
1194: $cfg = cRegistry::getConfig();
1195: $db = cRegistry::getDb();
1196:
1197: if (!$this->isLoaded()) {
1198: $msg = Pifa::i18n('FORM_LOAD_ERROR');
1199: throw new PifaException($msg);
1200: }
1201:
1202:
1203: $sql = "-- PifaForm->delete()
1204: DELETE FROM
1205: `" . cRegistry::getDbTableName('pifa_form') . "`
1206: WHERE
1207: idform = " . cSecurity::toInteger($this->get('idform')) . "
1208: ;";
1209: if (false === $db->query($sql)) {
1210: $msg = Pifa::i18n('FORM_DELETE_ERROR');
1211: throw new PifaException($msg);
1212: }
1213:
1214:
1215: $sql = "-- PifaForm->delete()
1216: DELETE FROM
1217: `" . cRegistry::getDbTableName('pifa_field') . "`
1218: WHERE
1219: idform = " . cSecurity::toInteger($this->get('idform')) . "
1220: ;";
1221: if (false === $db->query($sql)) {
1222: $msg = Pifa::i18n('FIELDS_DELETE_ERROR');
1223: throw new PifaException($msg);
1224: }
1225:
1226:
1227: if (0 < strlen(trim($this->get('data_table')))) {
1228: $sql = "-- PifaForm->delete()
1229: DROP TABLE IF EXISTS
1230: `" . cSecurity::toString($this->get('data_table')) . "`
1231: ;";
1232: if (false === $db->query($sql)) {
1233: $msg = Pifa::i18n('TABLE_DROP_ERROR');
1234: throw new PifaException($msg);
1235: }
1236: }
1237: }
1238:
1239: 1240: 1241: 1242:
1243: public function getTableName() {
1244: return $this->get('data_table');
1245: }
1246:
1247: }
1248: