c39820d0113684f1f46a3ea70d1989e7f0e65064
[com.zyxware.civiwci.git] / CRM / Wci / Form / NewEmbedCode.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/DAO/NewEmbedCode.php';
6
7 /**
8 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12 class CRM_Wci_Form_NewEmbedCode extends CRM_Core_Form {
13 protected $_id;
14
15 function preProcess() {
16 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
17 FALSE, NULL, 'REQUEST' );
18 }
19
20 function buildQuickForm() {
21 $this->add('text', 'title', ts('Title'),true, true)->setSize(45);
22 // add form elements
23 $this->add(
24 'select', // field type
25 'widget', // field name
26 'Widget', // field label
27 $this->getWidgets(), // list of options
28 true // is required
29 );
30 $this->addButtons(array(
31 array(
32 'type' => 'submit',
33 'name' => ts('Save'),
34 'isDefault' => TRUE,
35 ),
36 array(
37 'type' => 'next',
38 'name' => ts('Save & Preview'),
39 ),
40 ));
41
42 if (isset($this->_id)) {
43 $query = "SELECT * FROM civicrm_wci_embed_code WHERE id= %1";
44 $params = array(1 => array($this->_id, 'Integer'));
45
46 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_EmbedCode');
47
48 while ($dao->fetch()) {
49
50 $emb_code[$dao->id] = array();
51 CRM_Core_DAO::storeValues($dao, $emb_code[$dao->id]);
52
53 $this->setDefaults(array(
54 'title' => $emb_code[$dao->id]['name']));
55 $this->setDefaults(array(
56 'widget' => $emb_code[$dao->id]['widget_id']));
57 }
58 CRM_Utils_System::setTitle(ts('Edit embed code'));
59 }
60 else {
61 CRM_Utils_System::setTitle(ts('New embed code'));
62 }
63 // export form elements
64 $this->assign('elementNames', $this->getRenderableElementNames());
65 parent::buildQuickForm();
66 }
67
68 function postProcess() {
69 $values = $this->exportValues();
70
71 $title = str_replace("'", "''", $values['title']);
72
73 if (isset($this->_id)) {
74 $sql = "UPDATE civicrm_wci_embed_code SET name = '" . $title
75 . "', widget_id = '" . $values['widget'] . "' where id =" . $this->_id ;
76 }
77 else {
78 $sql = "INSERT INTO civicrm_wci_embed_code (name, widget_id)VALUES ('"
79 . $title . "','" . $values['widget'] . "')";
80 }
81 $errorScope = CRM_Core_TemporaryErrorScope::useException();
82 try {
83 $transaction = new CRM_Core_Transaction();
84 CRM_Core_DAO::executeQuery($sql);
85 $transaction->commit();
86 CRM_Core_Session::setStatus(ts('Embed code created successfuly'), '', 'success');
87 if(isset($_REQUEST['_qf_NewEmbedCode_next'])) {
88 (isset($this->_id)) ? $embed_id = $this->_id :
89 $embed_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
90 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $embed_id);
91 } else {
92 CRM_Utils_System::redirect('embed-code?reset=1');
93 }
94 }
95 catch (Exception $e) {
96 CRM_Core_Session::setStatus(ts('Failed to create embed code'), '', 'error');
97 $transaction->rollback();
98 }
99 parent::postProcess();
100 }
101 function getWidgets() {
102 $options = array(
103 '' => ts('- select -'),
104 );
105 $widgList = CRM_Wci_BAO_Widget::getWidgetList();
106 foreach ($widgList as $widg) {
107 $options[$widg['id']] = $widg['title'];
108 }
109
110 return $options;
111 }
112
113 /**
114 * Get the fields/elements defined in this form.
115 *
116 * @return array (string)
117 */
118 function getRenderableElementNames() {
119 // The _elements list includes some items which should not be
120 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
121 // items don't have labels. We'll identify renderable by filtering on
122 // the 'label'.
123 $elementNames = array();
124 foreach ($this->_elements as $element) {
125 $label = $element->getLabel();
126 if (!empty($label)) {
127 $elementNames[] = $element->getName();
128 }
129 }
130 return $elementNames;
131 }
132 }