3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Form helper class for an Website object,
37 class CRM_Contact_Form_Inline_Website
extends CRM_Contact_Form_Inline
{
40 * Websitess of the contact that is been viewed.
42 private $_websites = [];
45 * No of website blocks for inline edit.
47 private $_blockCount = 6;
52 public function preProcess() {
55 //get all the existing websites
56 $params = ['contact_id' => $this->_contactId
];
58 $this->_websites
= CRM_Core_BAO_Website
::getValues($params, $values);
62 * Build the form object elements for website object.
64 public function buildQuickForm() {
65 parent
::buildQuickForm();
67 $totalBlocks = $this->_blockCount
;
68 $actualBlockCount = 1;
69 if (count($this->_websites
) > 1) {
70 $actualBlockCount = $totalBlocks = count($this->_websites
);
71 if ($totalBlocks < $this->_blockCount
) {
72 $additionalBlocks = $this->_blockCount
- $totalBlocks;
73 $totalBlocks +
= $additionalBlocks;
81 $this->assign('actualBlockCount', $actualBlockCount);
82 $this->assign('totalBlocks', $totalBlocks);
84 $this->applyFilter('__ALL__', 'trim');
86 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
87 CRM_Contact_Form_Edit_Website
::buildQuickForm($this, $blockId, TRUE);
90 $this->addFormRule(['CRM_Contact_Form_Inline_Website', 'formRule'], $this);
95 * Set defaults for the form.
99 public function setDefaultValues() {
101 if (!empty($this->_websites
)) {
102 foreach ($this->_websites
as $id => $value) {
103 $defaults['website'][$id] = $value;
107 // set the default website type
108 $defaults['website'][1]['website_type_id'] = key(CRM_Core_OptionGroup
::values('website_type',
109 FALSE, FALSE, FALSE, ' AND is_default = 1'
118 public function postProcess() {
119 $params = $this->exportValues();
121 foreach ($this->_websites
as $count => $value) {
122 if (!empty($value['id']) && isset($params['website'][$count])) {
123 $params['website'][$count]['id'] = $value['id'];
126 // Process / save websites
127 CRM_Core_BAO_Website
::process($params['website'], $this->_contactId
, TRUE);
134 * Global validation rules for the form.
136 * @param array $fields
137 * Posted values of the form.
138 * @param array $errors
139 * List of errors to be posted back to the form.
140 * @param CRM_Contact_Form_Inline_Website $form
144 public static function formRule($fields, $errors, $form) {
145 $hasData = $errors = [];
146 if (!empty($fields['website']) && is_array($fields['website'])) {
148 foreach ($fields['website'] as $instance => $blockValues) {
149 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
152 $hasData[] = $instance;
153 if (!empty($blockValues['website_type_id'])) {
154 if (empty($types[$blockValues['website_type_id']])) {
155 $types[$blockValues['website_type_id']] = $blockValues['website_type_id'];
158 $errors["website[" . $instance . "][website_type_id]"] = ts('Contacts may only have one website of each type at most.');