31603 progress bar update not clearing widget cache
[com.zyxware.civiwci.git] / CRM / Wci / Form / NewEmbedCode.php
CommitLineData
13e70378 1<?php
bccdda02
J
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM Widget Creation Interface (WCI) Version 1.0 |
5 +--------------------------------------------------------------------+
6 | Copyright Zyxware Technologies (c) 2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM WCI. |
9 | |
10 | CiviCRM WCI is free software; you can copy, modify, and distribute |
11 | it under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM WCI is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License along with this program; if not, contact Zyxware |
21 | Technologies at info[AT]zyxware[DOT]com. |
22 +--------------------------------------------------------------------+
23*/
13e70378
J
24require_once 'CRM/Core/Form.php';
25require_once 'wci-helper-functions.php';
26require_once 'CRM/Wci/DAO/NewEmbedCode.php';
27
28/**
29 * Form controller class
30 *
31 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
32 */
33class CRM_Wci_Form_NewEmbedCode extends CRM_Core_Form {
34 protected $_id;
35
36 function preProcess() {
bccdda02 37 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
13e70378
J
38 FALSE, NULL, 'REQUEST' );
39 }
bccdda02 40
13e70378 41 function buildQuickForm() {
d6f51b17 42 $this->add('text', 'title', ts('Title'),true, true)->setSize(45);
13e70378
J
43 // add form elements
44 $this->add(
45 'select', // field type
46 'widget', // field name
47 'Widget', // field label
48 $this->getWidgets(), // list of options
d6f51b17 49 true // is required
13e70378
J
50 );
51 $this->addButtons(array(
52 array(
53 'type' => 'submit',
54 'name' => ts('Save'),
55 'isDefault' => TRUE,
56 ),
57 array(
58 'type' => 'next',
59 'name' => ts('Save & Preview'),
60 ),
61 ));
62
63 if (isset($this->_id)) {
b3e303a2
J
64 $query = "SELECT * FROM civicrm_wci_embed_code WHERE id= %1";
65 $params = array(1 => array($this->_id, 'Integer'));
bccdda02 66
13e70378
J
67 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_EmbedCode');
68
69 while ($dao->fetch()) {
70
71 $emb_code[$dao->id] = array();
72 CRM_Core_DAO::storeValues($dao, $emb_code[$dao->id]);
73
74 $this->setDefaults(array(
75 'title' => $emb_code[$dao->id]['name']));
76 $this->setDefaults(array(
77 'widget' => $emb_code[$dao->id]['widget_id']));
78 }
79 CRM_Utils_System::setTitle(ts('Edit embed code'));
80 }
81 else {
82 CRM_Utils_System::setTitle(ts('New embed code'));
bccdda02 83 }
13e70378
J
84 // export form elements
85 $this->assign('elementNames', $this->getRenderableElementNames());
86 parent::buildQuickForm();
87 }
88
89 function postProcess() {
90 $values = $this->exportValues();
91
92 $title = str_replace("'", "''", $values['title']);
bccdda02
J
93 $params = array(1 => array($title, 'String'),
94 2 => array($values['widget'], 'Integer'),
95 );
13e70378 96 if (isset($this->_id)) {
bccdda02
J
97 $params += array(3 => array($this->_id, 'Integer'),);
98 $sql = "UPDATE civicrm_wci_embed_code SET name = %1, widget_id = %2 where id = %3" ;
13e70378
J
99 }
100 else {
bccdda02 101 $sql = "INSERT INTO civicrm_wci_embed_code (name, widget_id)VALUES (%1, %2)";
13e70378
J
102 }
103 $errorScope = CRM_Core_TemporaryErrorScope::useException();
104 try {
105 $transaction = new CRM_Core_Transaction();
bccdda02 106 CRM_Core_DAO::executeQuery($sql, $params);
13e70378
J
107 $transaction->commit();
108 CRM_Core_Session::setStatus(ts('Embed code created successfuly'), '', 'success');
109 if(isset($_REQUEST['_qf_NewEmbedCode_next'])) {
bccdda02 110 (isset($this->_id)) ? $embed_id = $this->_id :
13e70378
J
111 $embed_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
112 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $embed_id);
113 } else {
114 CRM_Utils_System::redirect('embed-code?reset=1');
115 }
bccdda02 116 }
13e70378
J
117 catch (Exception $e) {
118 CRM_Core_Session::setStatus(ts('Failed to create embed code'), '', 'error');
119 $transaction->rollback();
120 }
121 parent::postProcess();
122 }
bccdda02 123
13e70378
J
124 function getWidgets() {
125 $options = array(
126 '' => ts('- select -'),
127 );
128 $widgList = CRM_Wci_BAO_Widget::getWidgetList();
129 foreach ($widgList as $widg) {
130 $options[$widg['id']] = $widg['title'];
131 }
132
133 return $options;
134 }
135
136 /**
137 * Get the fields/elements defined in this form.
138 *
139 * @return array (string)
140 */
141 function getRenderableElementNames() {
142 // The _elements list includes some items which should not be
143 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
144 // items don't have labels. We'll identify renderable by filtering on
145 // the 'label'.
146 $elementNames = array();
147 foreach ($this->_elements as $element) {
148 $label = $element->getLabel();
149 if (!empty($label)) {
150 $elementNames[] = $element->getName();
151 }
152 }
153 return $elementNames;
154 }
155}