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