(NFC) (dev/core#878) Simplify copyright header (CRM/*)
[civicrm-core.git] / CRM / Contact / Form / Edit / Individual.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 * Auxiliary class to provide support to the Contact Form class.
20 *
21 * Does this by implementing a small set of static methods.
22 */
23 class CRM_Contact_Form_Edit_Individual {
24
25 /**
26 * This function provides the HTML form elements that are specific to the Individual Contact Type.
27 *
28 * @param CRM_Core_Form $form
29 * Form object.
30 * @param int $inlineEditMode
31 * ( 1 for contact summary.
32 * top bar form and 2 for display name edit )
33 */
34 public static function buildQuickForm(&$form, $inlineEditMode = NULL) {
35 $form->applyFilter('__ALL__', 'trim');
36
37 if (!$inlineEditMode || $inlineEditMode == 1) {
38 $nameFields = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
39 'contact_edit_options', TRUE, NULL,
40 FALSE, 'name', TRUE, 'AND v.filter = 2'
41 );
42
43 // Use names instead of labels to build form.
44 $nameFields = array_keys($nameFields);
45
46 // Fixme: dear god why? these come out in a format that is NOT the name of the fields.
47 foreach ($nameFields as &$fix) {
48 $fix = str_replace(' ', '_', strtolower($fix));
49 if ($fix == 'prefix' || $fix == 'suffix') {
50 // God, why god?
51 $fix .= '_id';
52 }
53 }
54
55 foreach ($nameFields as $name) {
56 $props = [];
57 if ($name == 'prefix_id' || $name == 'suffix_id') {
58 //override prefix/suffix label name as Prefix/Suffix respectively and adjust select size
59 $props = ['class' => 'eight', 'placeholder' => ' ', 'label' => $name == 'prefix_id' ? ts('Prefix') : ts('Suffix')];
60 }
61 $form->addField($name, $props);
62 }
63 }
64
65 if (!$inlineEditMode || $inlineEditMode == 2) {
66 // nick_name
67 $form->addField('nick_name');
68
69 // job title
70 // override the size for UI to look better
71 $form->addField('job_title', ['size' => '30']);
72
73 //Current Employer Element
74 $props = [
75 'api' => ['params' => ['contact_type' => 'Organization']],
76 'create' => TRUE,
77 ];
78 $form->addField('employer_id', $props);
79 $form->addField('contact_source', ['class' => 'big']);
80 }
81
82 if (!$inlineEditMode) {
83 //External Identifier Element
84 $form->addField('external_identifier', ['label' => 'External ID']);
85
86 $form->addRule('external_identifier',
87 ts('External ID already exists in Database.'),
88 'objectExists',
89 ['CRM_Contact_DAO_Contact', $form->_contactId, 'external_identifier']
90 );
91 CRM_Core_ShowHideBlocks::links($form, 'demographics', '', '');
92 }
93 }
94
95 /**
96 * Global form rule.
97 *
98 * @param array $fields
99 * The input form values.
100 * @param array $files
101 * The uploaded files if any.
102 * @param int $contactID
103 *
104 * @return bool
105 * TRUE if no errors, else array of errors.
106 */
107 public static function formRule($fields, $files, $contactID = NULL) {
108 $errors = [];
109 $primaryID = CRM_Contact_Form_Contact::formRule($fields, $errors, $contactID, 'Individual');
110
111 // make sure that firstName and lastName or a primary OpenID is set
112 if (!$primaryID && (empty($fields['first_name']) || empty($fields['last_name']))) {
113 $errors['_qf_default'] = ts('First Name and Last Name OR an email OR an OpenID in the Primary Location should be set.');
114 }
115
116 return empty($errors) ? TRUE : $errors;
117 }
118
119 }