Merge pull request #15115 from eileenmcnaughton/import_metadata
[civicrm-core.git] / CRM / Admin / Form / PdfFormats.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
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. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 */
34
35 /**
36 * This class generates form components for PDF Page Format Settings.
37 */
38 class CRM_Admin_Form_PdfFormats extends CRM_Admin_Form {
39
40 /**
41 * PDF Page Format ID.
42 * @var int
43 */
44 protected $_id = NULL;
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 parent::buildQuickForm();
51
52 if ($this->_action & CRM_Core_Action::DELETE) {
53 $formatName = CRM_Core_BAO_PdfFormat::getFieldValue('CRM_Core_BAO_PdfFormat', $this->_id, 'name');
54 $this->assign('formatName', $formatName);
55 return;
56 }
57
58 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_BAO_PdfFormat');
59 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
60 $this->add('text', 'description', ts('Description'), ['size' => CRM_Utils_Type::HUGE]);
61 $this->add('checkbox', 'is_default', ts('Is this PDF Page Format the default?'));
62
63 $this->add('select', 'paper_size', ts('Paper Size'),
64 [
65 0 => ts('- default -'),
66 ] + CRM_Core_BAO_PaperSize::getList(TRUE), FALSE,
67 ['onChange' => "selectPaper( this.value );"]
68 );
69
70 $this->add('static', 'paper_dimensions', NULL, ts('Width x Height'));
71 $this->add('select', 'orientation', ts('Orientation'), CRM_Core_BAO_PdfFormat::getPageOrientations(), FALSE,
72 ['onChange' => "updatePaperDimensions();"]
73 );
74 $this->add('select', 'metric', ts('Unit of Measure'), CRM_Core_BAO_PdfFormat::getUnits(), FALSE,
75 ['onChange' => "selectMetric( this.value );"]
76 );
77 $this->add('text', 'margin_left', ts('Left Margin'), ['size' => 8, 'maxlength' => 8], TRUE);
78 $this->add('text', 'margin_right', ts('Right Margin'), ['size' => 8, 'maxlength' => 8], TRUE);
79 $this->add('text', 'margin_top', ts('Top Margin'), ['size' => 8, 'maxlength' => 8], TRUE);
80 $this->add('text', 'margin_bottom', ts('Bottom Margin'), ['size' => 8, 'maxlength' => 8], TRUE);
81 $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_BAO_PdfFormat', 'weight'), TRUE);
82
83 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', [
84 'CRM_Core_BAO_PdfFormat',
85 $this->_id,
86 ]);
87 $this->addRule('margin_left', ts('Margin must be numeric'), 'numeric');
88 $this->addRule('margin_right', ts('Margin must be numeric'), 'numeric');
89 $this->addRule('margin_top', ts('Margin must be numeric'), 'numeric');
90 $this->addRule('margin_bottom', ts('Margin must be numeric'), 'numeric');
91 $this->addRule('weight', ts('Weight must be integer'), 'integer');
92 }
93
94 /**
95 * @return int
96 */
97 public function setDefaultValues() {
98 if ($this->_action & CRM_Core_Action::ADD) {
99 $defaults['weight'] = CRM_Utils_Array::value('weight', CRM_Core_BAO_PdfFormat::getDefaultValues(), 0);
100 }
101 else {
102 $defaults = $this->_values;
103 }
104 return $defaults;
105 }
106
107 /**
108 * Process the form submission.
109 */
110 public function postProcess() {
111 if ($this->_action & CRM_Core_Action::DELETE) {
112 // delete PDF Page Format
113 CRM_Core_BAO_PdfFormat::del($this->_id);
114 CRM_Core_Session::setStatus(ts('Selected PDF Page Format has been deleted.'), ts('Record Deleted'), 'success');
115 return;
116 }
117
118 $values = $this->controller->exportValues($this->getName());
119 $values['is_default'] = isset($values['is_default']);
120 $bao = new CRM_Core_BAO_PdfFormat();
121 $bao->savePdfFormat($values, $this->_id);
122
123 $status = ts('Your new PDF Page Format titled <strong>%1</strong> has been saved.', [1 => $values['name']], ts('Saved'), 'success');
124 if ($this->_action & CRM_Core_Action::UPDATE) {
125 $status = ts('Your PDF Page Format titled <strong>%1</strong> has been updated.', [1 => $values['name']], ts('Saved'), 'success');
126 }
127 CRM_Core_Session::setStatus($status);
128 }
129
130 }