Merge pull request #17380 from seamuslee001/symfony_4_4
[civicrm-core.git] / CRM / Contact / Form / Inline / ContactName.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 contact name section.
20 */
21 class CRM_Contact_Form_Inline_ContactName extends CRM_Contact_Form_Inline {
22
23 /**
24 * Build the form object elements.
25 */
26 public function buildQuickForm() {
27 parent::buildQuickForm();
28
29 // Build contact type specific fields
30 $class = 'CRM_Contact_Form_Edit_' . $this->_contactType;
31 $class::buildQuickForm($this, 1);
32 $this->addFormRule(['CRM_Contact_Form_Inline_ContactName', 'formRule'], $this);
33 }
34
35 /**
36 * Global validation rules for the form.
37 *
38 * @param array $fields
39 * Posted values of the form.
40 * @param array $errors
41 * List of errors to be posted back to the form.
42 * @param CRM_Contact_Form_Inline_ContactName $form
43 *
44 * @return array
45 */
46 public static function formRule($fields, $errors, $form) {
47 if (empty($fields['first_name']) && empty($fields['last_name'])
48 && empty($fields['organization_name'])
49 && empty($fields['household_name'])) {
50 $emails = civicrm_api3('Email', 'getcount', ['contact_id' => $form->_contactId]);
51 if (!$emails) {
52 $errorField = $form->_contactType == 'Individual' ? 'last' : strtolower($form->_contactType);
53 $errors[$errorField . '_name'] = ts('Contact with no email must have a name.');
54 }
55 }
56 return $errors;
57 }
58
59 /**
60 * Process the form.
61 */
62 public function postProcess() {
63 $params = $this->exportValues();
64
65 // Process / save contact info
66 $params['contact_type'] = $this->_contactType;
67 $params['contact_id'] = $this->_contactId;
68
69 if (!empty($this->_contactSubType)) {
70 $params['contact_sub_type'] = $this->_contactSubType;
71 }
72
73 CRM_Contact_BAO_Contact::create($params);
74
75 $this->response();
76 }
77
78 }