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