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