a18348f2b4ae2f90c7649d76cf6e4d976ab48bf4
[com.zyxware.civiwci.git] / CRM / Wci / Form / WCISettings.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4
5 /**
6 * Form controller class
7 *
8 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
9 */
10 class CRM_Wci_Form_WCISettings extends CRM_Core_Form {
11 function buildQuickForm() {
12
13 // add form elements
14 /*$this->add(
15 'select', // field type
16 'default_profile', // field name
17 'Default profile', // field label
18 $this->getProfiles(), // list of options
19 true // is required
20 );*/
21
22 $this->add(
23 'select', // field type
24 'default_widget', // field name
25 'Default widget', // field label
26 $this->getWidgets(), // list of options
27 false // is required
28 );
29 $this->add('text', 'default_profile', ts('Default profile'),true)->setSize(45);
30 $this->addButtons(array(
31 array(
32 'type' => 'submit',
33 'name' => ts('Save'),
34 'isDefault' => TRUE,
35 ),
36 ));
37
38 $widgetId = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_widget'));
39 $defProf = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_profile'));
40
41 $this->setDefaults(array(
42 'default_widget' => $widgetId));
43 $this->setDefaults(array(
44 'default_profile' => $defProf));
45 // export form elements
46 $this->assign('elementNames', $this->getRenderableElementNames());
47
48 CRM_Utils_System::setTitle(ts('Widget Settings'));
49
50 parent::buildQuickForm();
51 }
52
53 function postProcess() {
54 $values = $this->exportValues();
55 civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_widget' => $values['default_widget'],));
56 civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_profile' => $values['default_profile'],));
57 CRM_Core_Session::setStatus(ts('Widget settings are saved to database'), '', 'success');
58 parent::postProcess();
59 }
60
61 function getProfiles() {
62 /*$params = array();
63 $result = civicrm_api3('Profile', 'get', $params);
64 $result = civicrm_api('Profile', 'getcount', $params);
65 print_r($result);*/
66 }
67 function getWidgets() {
68 $query = "SELECT * FROM civicrm_wci_widget";
69 $params = array();
70
71 $widgetlist = array(
72 '' => ts('- select -'),
73 );
74
75 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
76
77 while ($dao->fetch()) {
78 $widgetlist[$dao->id] = $dao->title;
79 }
80 return $widgetlist;
81 }
82
83
84 /**
85 * Get the fields/elements defined in this form.
86 *
87 * @return array (string)
88 */
89 function getRenderableElementNames() {
90 // The _elements list includes some items which should not be
91 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
92 // items don't have labels. We'll identify renderable by filtering on
93 // the 'label'.
94 $elementNames = array();
95 foreach ($this->_elements as $element) {
96 $label = $element->getLabel();
97 if (!empty($label)) {
98 $elementNames[] = $element->getName();
99 }
100 }
101 return $elementNames;
102 }
103 }