aa5005b2f2baef812c22f1774b8044ae6927d68d
[civicrm-core.git] / CRM / Badge / Form / Layout.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | 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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for name badge layout
38 *
39 */
40 class CRM_Badge_Form_Layout extends CRM_Admin_Form {
41
42 /**
43 * Function to build the form
44 *
45 * @return None
46 * @access public
47 */
48 public function buildQuickForm() {
49 parent::buildQuickForm();
50
51 if ($this->_action & CRM_Core_Action::DELETE) {
52 return;
53 }
54
55 $config = CRM_Core_Config::singleton();
56 $resources = CRM_Core_Resources::singleton();
57 $resources->addSetting(
58 array(
59 'kcfinderPath' => $config->userFrameworkResourceURL .'packages' .DIRECTORY_SEPARATOR
60 )
61 );
62 $resources->addScriptFile('civicrm', 'templates/CRM/Badge/Form/Layout.js');
63
64 $this->applyFilter('__ALL__', 'trim');
65
66 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'), true);
67
68 $labelStyle = CRM_Core_BAO_LabelFormat::getList(TRUE, 'name_badge');
69 $this->add('select', 'label_format_name', ts('Label Style'), array('' => ts('- select -')) + $labelStyle, TRUE);
70
71 $this->add('text', 'description', ts('Description'),
72 CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
73
74 // get the tokens
75 $contactTokens = CRM_Core_SelectValues::contactTokens();
76 $eventTokens = CRM_Core_SelectValues::eventTokens();
77 $tokens = array_merge($contactTokens, $eventTokens);
78 asort($tokens);
79
80 $fontSizes = CRM_Core_BAO_LabelFormat::getFontSizes();
81 $fontNames = CRM_Core_BAO_LabelFormat::getFontNames('name_badge');
82 $textAlignment = CRM_Core_BAO_LabelFormat::getTextAlignments();
83
84 $rowCount = 4;
85 for ( $i =1; $i <= $rowCount; $i++ ) {
86 $this->add('select', "token[$i]", ts('Token'), array('' => ts('- none -')) + $tokens);
87 $this->add('select', "font_name[$i]", ts('Font Name'), $fontNames);
88 $this->add('select', "font_size[$i]", ts('Font Size'), $fontSizes);
89 $this->add('select', "text_alignment[$i]", ts('Alignment'), $textAlignment);
90 }
91 $rowCount++;
92 $this->assign('rowCount', $rowCount);
93
94 $this->add('checkbox', 'add_barcode', ts('Barcode?'));
95 unset($textAlignment['J']);
96 $this->add('select', "barcode_alignment", ts('Alignment'), $textAlignment);
97
98 $attributes = array(
99 'readonly'=> true,
100 'value' => ts('click here and select a file double clicking on it'),
101 );
102 $this->add('text', 'image_1', ts('Image 1'), $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
103 $this->add('text', 'image_2', ts('Image 2'), $attributes + CRM_Core_DAO::getAttribute('CRM_Core_DAO_PrintLabel', 'title'));
104
105 $this->add('checkbox', 'is_default', ts('Default?'));
106 $this->add('checkbox', 'is_active', ts('Enabled?'));
107 $this->add('checkbox', 'is_reserved', ts('Reserved?'));
108 }
109
110 /**
111 * This function sets the default values for the form. MobileProvider that in edit/view mode
112 * the default values are retrieved from the database
113 *
114 * @access public
115 *
116 * @return None
117 */
118 function setDefaultValues() {
119 if (isset($this->_id) && empty($this->_values)) {
120 $this->_values = array();
121 $params = array('id' => $this->_id);
122 CRM_Badge_BAO_Layout::retrieve($params, $this->_values);
123
124
125 $defaults = array_merge($this->_values,
126 CRM_Badge_BAO_Layout::getDecodedData($this->_values['data']));
127 }
128
129 if ($this->_action == CRM_Core_Action::DELETE && isset($defaults['title'])) {
130 $this->assign('delName', $defaults['title']);
131 }
132
133 // its ok if there is no element called is_active
134 $defaults['is_active'] = ($this->_id) ? CRM_Utils_Array::value('is_active', $defaults) : 1;
135
136 return $defaults;
137 }
138
139 /**
140 * Function to process the form
141 *
142 * @access public
143 *
144 * @return None
145 */
146 public function postProcess() {
147 if ($this->_action & CRM_Core_Action::DELETE) {
148 CRM_Badge_BAO_Layout::del($this->_id);
149 CRM_Core_Session::setStatus(ts('Selected badge layout has been deleted.'), ts('Record Deleted'), 'success');
150 return;
151 }
152
153 $params = $data = $this->controller->exportValues($this->_name);
154
155 unset($data['qfKey']);
156 $params['data'] = json_encode($data);
157
158 if ($this->_id) {
159 $params['id'] = $this->_id;
160 }
161
162 // store the submitted values in an array
163 CRM_Badge_BAO_Layout::create($params);
164
165 CRM_Core_Session::setStatus(ts("The badge layout '%1' has been saved.",
166 array(1 => $params['title'])
167 ), ts('Saved'), 'success');
168 }
169 }