3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright (C) 2011 Marty Wright |
7 | Licensed to CiviCRM under the Academic Free License version 3.0. |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
38 * This class contains functions for managing PDF Page Formats
40 class CRM_Core_BAO_PdfFormat
extends CRM_Core_DAO_OptionValue
{
43 * Static holder for the PDF Page Formats Option Group ID
45 private static $_gid = NULL;
48 * PDF Page Format fields stored in the 'value' field of the Option Value table.
50 private static $optionValueFields = array(
51 'paper_size' => array(
52 'name' => 'paper_size',
53 'type' => CRM_Utils_Type
::T_STRING
,
54 'default' => 'letter',
56 'stationery' => array(
57 'name' => 'stationery',
58 'type' => CRM_Utils_Type
::T_STRING
,
61 'orientation' => array(
62 'name' => 'orientation',
63 'type' => CRM_Utils_Type
::T_STRING
,
64 'default' => 'portrait',
68 'type' => CRM_Utils_Type
::T_STRING
,
71 'margin_top' => array(
72 'name' => 'margin_top',
73 'type' => CRM_Utils_Type
::T_FLOAT
,
77 'margin_bottom' => array(
78 'name' => 'margin_bottom',
79 'type' => CRM_Utils_Type
::T_FLOAT
,
83 'margin_left' => array(
84 'name' => 'margin_left',
85 'type' => CRM_Utils_Type
::T_FLOAT
,
89 'margin_right' => array(
90 'name' => 'margin_right',
91 'type' => CRM_Utils_Type
::T_FLOAT
,
98 * Get page orientations recognized by the DOMPDF package used to create PDF letters.
103 * array of page orientations
105 public static function getPageOrientations() {
107 'portrait' => ts('Portrait'),
108 'landscape' => ts('Landscape'),
113 * Get measurement units recognized by the DOMPDF package used to create PDF letters.
118 * array of measurement units
120 public static function getUnits() {
122 'in' => ts('Inches'),
123 'cm' => ts('Centimeters'),
124 'mm' => ts('Millimeters'),
125 'pt' => ts('Points'),
130 * Get Option Group ID for PDF Page Formats
135 * Group ID (null if Group ID doesn't exist)
137 private static function _getGid() {
139 self
::$_gid = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'pdf_format', 'id', 'name');
141 CRM_Core_Error
::fatal(ts('PDF Format Option Group not found in database.'));
148 * Add ordering fields to Page Format list
150 * @param array (reference) $list List of PDF Page Formats
151 * @param string $returnURL
152 * URL of page calling this function.
156 public static function addOrder(&$list, $returnURL) {
157 $filter = "option_group_id = " . self
::_getGid();
158 CRM_Utils_Weight
::addOrder($list, 'CRM_Core_DAO_OptionValue', 'id', $returnURL, $filter);
162 * Get list of PDF Page Formats.
164 * @param bool $namesOnly
165 * Return simple list of names.
168 * (reference) PDF Page Format list
170 public static function &getList($namesOnly = FALSE) {
171 static $list = array();
172 if (self
::_getGid()) {
173 // get saved PDF Page Formats from Option Value table
174 $dao = new CRM_Core_DAO_OptionValue();
175 $dao->option_group_id
= self
::_getGid();
177 $dao->orderBy('weight');
179 while ($dao->fetch()) {
181 $list[$dao->id
] = $dao->name
;
184 CRM_Core_DAO
::storeValues($dao, $list[$dao->id
]);
192 * Get the default PDF Page Format values
197 * Name/value pairs containing the default PDF Page Format values.
199 public static function &getDefaultValues() {
200 $params = array('is_active' => 1, 'is_default' => 1);
202 if (!self
::retrieve($params, $defaults)) {
203 foreach (self
::$optionValueFields as $name => $field) {
204 $defaults[$name] = $field['default'];
206 $filter = array('option_group_id' => self
::_getGid());
207 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $filter);
209 // also set the id to avoid NOTICES, CRM-8454
210 $defaults['id'] = NULL;
216 * Get PDF Page Format from the DB
218 * @param string $field
219 * Field name to search by.
221 * Field value to search for.
224 * (reference) associative array of name/value pairs
226 public static function &getPdfFormat($field, $val) {
227 $params = array('is_active' => 1, $field => $val);
228 $pdfFormat = array();
229 if (self
::retrieve($params, $pdfFormat)) {
233 return self
::getDefaultValues();
238 * Get PDF Page Format by Name
241 * PDF Page Format name. Empty = get default PDF Page Format.
244 * (reference) associative array of name/value pairs
246 public static function &getByName($name) {
247 return self
::getPdfFormat('name', $name);
251 * Get PDF Page Format by ID
254 * PDF Page Format id. 0 = get default PDF Page Format.
257 * (reference) associative array of name/value pairs
259 public static function &getById($id) {
260 return self
::getPdfFormat('id', $id);
264 * Get PDF Page Format field from associative array
266 * @param string $field
267 * Name of a PDF Page Format field.
268 * @param array (reference) $values associative array of name/value pairs containing
269 * PDF Page Format field selections
271 * @param null $default
275 public static function getValue($field, &$values, $default = NULL) {
276 if (array_key_exists($field, self
::$optionValueFields)) {
277 switch (self
::$optionValueFields[$field]['type']) {
278 case CRM_Utils_Type
::T_INT
:
279 return (int) CRM_Utils_Array
::value($field, $values, $default);
281 case CRM_Utils_Type
::T_FLOAT
:
282 // Round float values to three decimal places and trim trailing zeros.
283 // Add a leading zero to values less than 1.
284 $f = sprintf('%05.3f', $values[$field]);
287 return (float) (empty($f) ?
'0' : $f);
289 return CRM_Utils_Array
::value($field, $values, $default);
295 * Takes a bunch of params that are needed to match certain criteria and
296 * retrieves the relevant objects. Typically the valid params are only
297 * format id. It also stores all the retrieved values in the default array.
299 * @param array $params
300 * (reference ) an assoc array of name/value pairs.
301 * @param array $values
302 * (reference ) an assoc array to hold the flattened values.
304 * @return CRM_Core_DAO_OptionValue
306 public static function retrieve(&$params, &$values) {
307 $optionValue = new CRM_Core_DAO_OptionValue();
308 $optionValue->copyValues($params);
309 $optionValue->option_group_id
= self
::_getGid();
310 if ($optionValue->find(TRUE)) {
311 // Extract fields that have been serialized in the 'value' column of the Option Value table.
312 $values = json_decode($optionValue->value
, TRUE);
313 // Add any new fields that don't yet exist in the saved values.
314 foreach (self
::$optionValueFields as $name => $field) {
315 if (!isset($values[$name])) {
316 $values[$name] = $field['default'];
317 if ($field['metric']) {
318 $values[$name] = CRM_Utils_PDF_Utils
::convertMetric($field['default'],
319 self
::$optionValueFields['metric']['default'],
325 // Add fields from the OptionValue base class
326 CRM_Core_DAO
::storeValues($optionValue, $values);
333 * Save the PDF Page Format in the DB
335 * @param array (reference) $values associative array of name/value pairs
337 * Id of the database record (null = new record).
341 public function savePdfFormat(&$values, $id = NULL) {
342 // get the Option Group ID for PDF Page Formats (create one if it doesn't exist)
343 $group_id = self
::_getGid();
345 // clear other default if this is the new default PDF Page Format
346 if ($values['is_default']) {
347 $query = "UPDATE civicrm_option_value SET is_default = 0 WHERE option_group_id = $group_id";
348 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
351 // fetch existing record
357 // copy the supplied form values to the corresponding Option Value fields in the base class
358 foreach ($this->fields() as $name => $field) {
359 $this->$name = trim(CRM_Utils_Array
::value($name, $values, $this->$name));
360 if (empty($this->$name)) {
361 $this->$name = 'null';
365 $this->option_group_id
= $group_id;
366 $this->label
= $this->name
;
367 $this->is_active
= 1;
369 // serialize PDF Page Format fields into a single string to store in the 'value' column of the Option Value table
370 $v = json_decode($this->value
, TRUE);
371 foreach (self
::$optionValueFields as $name => $field) {
372 $v[$name] = self
::getValue($name, $values, CRM_Utils_Array
::value($name, $v));
374 $this->value
= json_encode($v);
376 // make sure serialized array will fit in the 'value' column
377 $attribute = CRM_Core_DAO
::getAttribute('CRM_Core_BAO_PdfFormat', 'value');
378 if (strlen($this->value
) > $attribute['maxlength']) {
379 CRM_Core_Error
::fatal(ts('PDF Page Format does not fit in database.'));
383 // fix duplicate weights
384 $filter = array('option_group_id' => self
::_getGid());
385 CRM_Utils_Weight
::correctDuplicateWeights('CRM_Core_DAO_OptionValue', $filter);
389 * Delete a PDF Page Format
392 * ID of the PDF Page Format to be deleted.
395 public static function del($id) {
397 $dao = new CRM_Core_DAO_OptionValue();
399 if ($dao->find(TRUE)) {
400 if ($dao->option_group_id
== self
::_getGid()) {
401 $filter = array('option_group_id' => self
::_getGid());
402 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_OptionValue', $id, $filter);
408 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));