3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/DAO/NewEmbedCode.php';
8 * Form controller class
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
12 class CRM_Wci_Form_NewEmbedCode
extends CRM_Core_Form
{
15 function preProcess() {
16 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this,
17 FALSE, NULL, 'REQUEST' );
20 function buildQuickForm() {
21 $this->add('text', 'title', ts('Title'),true, true)->setSize(45);
24 'select', // field type
25 'widget', // field name
26 'Widget', // field label
27 $this->getWidgets(), // list of options
30 $this->addButtons(array(
38 'name' => ts('Save & Preview'),
42 if (isset($this->_id
)) {
43 $query = "SELECT * FROM civicrm_wci_embed_code WHERE id= %1";
44 $params = array(1 => array($this->_id
, 'Integer'));
46 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_EmbedCode');
48 while ($dao->fetch()) {
50 $emb_code[$dao->id
] = array();
51 CRM_Core_DAO
::storeValues($dao, $emb_code[$dao->id
]);
53 $this->setDefaults(array(
54 'title' => $emb_code[$dao->id
]['name']));
55 $this->setDefaults(array(
56 'widget' => $emb_code[$dao->id
]['widget_id']));
58 CRM_Utils_System
::setTitle(ts('Edit embed code'));
61 CRM_Utils_System
::setTitle(ts('New embed code'));
63 // export form elements
64 $this->assign('elementNames', $this->getRenderableElementNames());
65 parent
::buildQuickForm();
68 function postProcess() {
69 $values = $this->exportValues();
71 $title = str_replace("'", "''", $values['title']);
73 if (isset($this->_id
)) {
74 $sql = "UPDATE civicrm_wci_embed_code SET name = '" . $title
75 . "', widget_id = '" . $values['widget'] . "' where id =" . $this->_id
;
78 $sql = "INSERT INTO civicrm_wci_embed_code (name, widget_id)VALUES ('"
79 . $title . "','" . $values['widget'] . "')";
81 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
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);
92 CRM_Utils_System
::redirect('embed-code?reset=1');
95 catch (Exception
$e) {
96 CRM_Core_Session
::setStatus(ts('Failed to create embed code'), '', 'error');
97 $transaction->rollback();
99 parent
::postProcess();
101 function getWidgets() {
103 '' => ts('- select -'),
105 $widgList = CRM_Wci_BAO_Widget
::getWidgetList();
106 foreach ($widgList as $widg) {
107 $options[$widg['id']] = $widg['title'];
114 * Get the fields/elements defined in this form.
116 * @return array (string)
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
123 $elementNames = array();
124 foreach ($this->_elements
as $element) {
125 $label = $element->getLabel();
126 if (!empty($label)) {
127 $elementNames[] = $element->getName();
130 return $elementNames;