Merge pull request #22886 from demeritcowboy/contributionview-notice3
[civicrm-core.git] / CRM / Contact / Form / Inline / Website.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form helper class for an Website object,
20 */
21 class CRM_Contact_Form_Inline_Website extends CRM_Contact_Form_Inline {
22
23 /**
24 * Websitess of the contact that is been viewed.
25 * @var array
26 */
27 private $_websites = [];
28
29 /**
30 * No of website blocks for inline edit.
31 * @var int
32 */
33 private $_blockCount = 26;
34
35 /**
36 * Call preprocess.
37 */
38 public function preProcess() {
39 parent::preProcess();
40
41 //get all the existing websites
42 $params = ['contact_id' => $this->_contactId];
43 $values = [];
44 $this->_websites = CRM_Core_BAO_Website::getValues($params, $values);
45 }
46
47 /**
48 * Build the form object elements for website object.
49 */
50 public function buildQuickForm() {
51 parent::buildQuickForm();
52
53 $totalBlocks = $this->_blockCount;
54 $actualBlockCount = 1;
55 if (count($this->_websites) > 1) {
56 $actualBlockCount = $totalBlocks = count($this->_websites);
57 if ($totalBlocks < $this->_blockCount) {
58 $additionalBlocks = $this->_blockCount - $totalBlocks;
59 $totalBlocks += $additionalBlocks;
60 }
61 else {
62 $actualBlockCount++;
63 $totalBlocks++;
64 }
65 }
66
67 $this->assign('actualBlockCount', $actualBlockCount);
68 $this->assign('totalBlocks', $totalBlocks);
69
70 $this->applyFilter('__ALL__', 'trim');
71
72 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
73 CRM_Contact_Form_Edit_Website::buildQuickForm($this, $blockId, TRUE);
74 }
75
76 $this->addFormRule(['CRM_Contact_Form_Inline_Website', 'formRule'], $this);
77
78 }
79
80 /**
81 * Set defaults for the form.
82 *
83 * @return array
84 */
85 public function setDefaultValues() {
86 $defaults = [];
87 if (!empty($this->_websites)) {
88 foreach ($this->_websites as $id => $value) {
89 $defaults['website'][$id] = $value;
90 }
91 }
92 else {
93 // set the default website type
94 $defaults['website'][1]['website_type_id'] = key(CRM_Core_OptionGroup::values('website_type',
95 FALSE, FALSE, FALSE, ' AND is_default = 1'
96 ));
97 }
98 return $defaults;
99 }
100
101 /**
102 * Process the form.
103 */
104 public function postProcess() {
105 $params = $this->exportValues();
106
107 foreach ($this->_websites as $count => $value) {
108 if (!empty($value['id']) && isset($params['website'][$count])) {
109 $params['website'][$count]['id'] = $value['id'];
110 }
111 }
112 // Process / save websites
113 CRM_Core_BAO_Website::process($params['website'], $this->_contactId, TRUE);
114
115 $this->log();
116 $this->response();
117 }
118
119 /**
120 * Global validation rules for the form.
121 *
122 * @param array $fields
123 * Posted values of the form.
124 * @param array $errors
125 * List of errors to be posted back to the form.
126 * @param CRM_Contact_Form_Inline_Website $form
127 *
128 * @return array
129 */
130 public static function formRule($fields, $errors, $form) {
131 $hasData = $errors = [];
132 if (!empty($fields['website']) && is_array($fields['website'])) {
133 $types = [];
134 foreach ($fields['website'] as $instance => $blockValues) {
135 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
136
137 if ($dataExists) {
138 $hasData[] = $instance;
139 if (!empty($blockValues['website_type_id'])) {
140 if (empty($types[$blockValues['website_type_id']])) {
141 $types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
142 }
143 else {
144 $errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');
145 }
146 }
147 }
148 }
149 }
150 return $errors;
151 }
152
153 }