3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Form helper class for an Website object,
21 class CRM_Contact_Form_Inline_Website
extends CRM_Contact_Form_Inline
{
24 * Websitess of the contact that is been viewed.
27 private $_websites = [];
30 * No of website blocks for inline edit.
33 private $_blockCount = 6;
38 public function preProcess() {
41 //get all the existing websites
42 $params = ['contact_id' => $this->_contactId
];
44 $this->_websites
= CRM_Core_BAO_Website
::getValues($params, $values);
48 * Build the form object elements for website object.
50 public function buildQuickForm() {
51 parent
::buildQuickForm();
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;
67 $this->assign('actualBlockCount', $actualBlockCount);
68 $this->assign('totalBlocks', $totalBlocks);
70 $this->applyFilter('__ALL__', 'trim');
72 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
73 CRM_Contact_Form_Edit_Website
::buildQuickForm($this, $blockId, TRUE);
76 $this->addFormRule(['CRM_Contact_Form_Inline_Website', 'formRule'], $this);
81 * Set defaults for the form.
85 public function setDefaultValues() {
87 if (!empty($this->_websites
)) {
88 foreach ($this->_websites
as $id => $value) {
89 $defaults['website'][$id] = $value;
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'
104 public function postProcess() {
105 $params = $this->exportValues();
107 foreach ($this->_websites
as $count => $value) {
108 if (!empty($value['id']) && isset($params['website'][$count])) {
109 $params['website'][$count]['id'] = $value['id'];
112 // Process / save websites
113 CRM_Core_BAO_Website
::process($params['website'], $this->_contactId
, TRUE);
120 * Global validation rules for the form.
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
130 public static function formRule($fields, $errors, $form) {
131 $hasData = $errors = [];
132 if (!empty($fields['website']) && is_array($fields['website'])) {
134 foreach ($fields['website'] as $instance => $blockValues) {
135 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
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'];
144 $errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');