Merge pull request #4897 from totten/master-cleanup2
[civicrm-core.git] / CRM / Contact / Form / Location.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Location {
36
37 /**
38 * Set variables up before form is built
39 *
40 * @param CRM_Core_Form $form
41 *
42 * @return void
43 */
44 public static function preProcess(&$form) {
45 $form->_addBlockName = CRM_Utils_Request::retrieve('block', 'String', CRM_Core_DAO::$_nullObject);
46 $additionalblockCount = CRM_Utils_Request::retrieve('count', 'Positive', CRM_Core_DAO::$_nullObject);
47
48 $form->assign('addBlock', FALSE);
49 if ($form->_addBlockName && $additionalblockCount) {
50 $form->assign('addBlock', TRUE);
51 $form->assign('blockName', $form->_addBlockName);
52 $form->assign('blockId', $additionalblockCount);
53 $form->set($form->_addBlockName . '_Block_Count', $additionalblockCount);
54 }
55
56 $className = CRM_Utils_System::getClassName($form);
57 if (in_array($className, array(
58 'CRM_Event_Form_ManageEvent_Location',
59 'CRM_Contact_Form_Domain'
60 ))) {
61 $form->_blocks = array(
62 'Address' => ts('Address'),
63 'Email' => ts('Email'),
64 'Phone' => ts('Phone'),
65 );
66 }
67
68 $form->assign('blocks', $form->_blocks);
69 $form->assign('className', $className);
70
71 // get address sequence.
72 if (!$addressSequence = $form->get('addressSequence')) {
73 $addressSequence = CRM_Core_BAO_Address::addressSequence();
74 $form->set('addressSequence', $addressSequence);
75 }
76 $form->assign('addressSequence', $addressSequence);
77 }
78
79 /**
80 * Build the form object
81 *
82 * @param CRM_Core_Form $form
83 *
84 * @return void
85 */
86 public static function buildQuickForm(&$form) {
87 // required for subsequent AJAX requests.
88 $ajaxRequestBlocks = array();
89 $generateAjaxRequest = 0;
90
91 //build 1 instance of all blocks, without using ajax ...
92 foreach ($form->_blocks as $blockName => $label) {
93 require_once str_replace('_', DIRECTORY_SEPARATOR, 'CRM_Contact_Form_Edit_' . $blockName) . '.php';
94 $name = strtolower($blockName);
95
96 $instances = array(1);
97 if (!empty($_POST[$name]) && is_array($_POST[$name])) {
98 $instances = array_keys($_POST[$name]);
99 }
100 elseif (property_exists($form, '_values') && !empty($form->_values[$name]) && is_array($form->_values[$name])) {
101 $instances = array_keys($form->_values[$name]);
102 }
103
104 foreach ($instances as $instance) {
105 if ($instance == 1) {
106 $form->assign('addBlock', FALSE);
107 $form->assign('blockId', $instance);
108 }
109 else {
110 //we are going to build other block instances w/ AJAX
111 $generateAjaxRequest++;
112 $ajaxRequestBlocks[$blockName][$instance] = TRUE;
113 }
114
115 $form->set($blockName . '_Block_Count', $instance);
116 $formName = 'CRM_Contact_Form_Edit_' . $blockName;
117 $formName::buildQuickForm($form);
118 }
119 }
120
121 //assign to generate AJAX request for building extra blocks.
122 $form->assign('generateAjaxRequest', $generateAjaxRequest);
123 $form->assign('ajaxRequestBlocks', $ajaxRequestBlocks);
124 }
125 }