Merge pull request #5729 from mlutfy/47-crm14588
[civicrm-core.git] / CRM / Contact / Form / Location.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_Form_Location {
36
37 /**
fe482240 38 * Set variables up before form is built.
6a488035 39 *
c490a46a 40 * @param CRM_Core_Form $form
77b97be7 41 *
6a488035
TO
42 * @return void
43 */
00be9182 44 public static function preProcess(&$form) {
6a488035
TO
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
1f7c9105
TM
56 if (is_a($form, 'CRM_Event_Form_ManageEvent_Location')
57 || is_a($form, 'CRM_Contact_Form_Domain')) {
6ea503d4
TO
58 $form->_blocks = array(
59 'Address' => ts('Address'),
6a488035
TO
60 'Email' => ts('Email'),
61 'Phone' => ts('Phone'),
62 );
63 }
64
65 $form->assign('blocks', $form->_blocks);
9b01cb89 66 $form->assign('className', CRM_Utils_System::getClassName($form));
6a488035
TO
67
68 // get address sequence.
69 if (!$addressSequence = $form->get('addressSequence')) {
70 $addressSequence = CRM_Core_BAO_Address::addressSequence();
71 $form->set('addressSequence', $addressSequence);
72 }
73 $form->assign('addressSequence', $addressSequence);
74 }
75
76 /**
fe482240 77 * Build the form object.
6a488035 78 *
c490a46a 79 * @param CRM_Core_Form $form
6c8f6e67 80 *
355ba699 81 * @return void
6a488035 82 */
00be9182 83 public static function buildQuickForm(&$form) {
6a488035
TO
84 // required for subsequent AJAX requests.
85 $ajaxRequestBlocks = array();
86 $generateAjaxRequest = 0;
87
88 //build 1 instance of all blocks, without using ajax ...
89 foreach ($form->_blocks as $blockName => $label) {
d3e86119 90 require_once str_replace('_', DIRECTORY_SEPARATOR, 'CRM_Contact_Form_Edit_' . $blockName) . '.php';
6a488035
TO
91 $name = strtolower($blockName);
92
93 $instances = array(1);
a7488080 94 if (!empty($_POST[$name]) && is_array($_POST[$name])) {
6a488035
TO
95 $instances = array_keys($_POST[$name]);
96 }
8cc574cf 97 elseif (property_exists($form, '_values') && !empty($form->_values[$name]) && is_array($form->_values[$name])) {
6a488035
TO
98 $instances = array_keys($form->_values[$name]);
99 }
100
101 foreach ($instances as $instance) {
102 if ($instance == 1) {
103 $form->assign('addBlock', FALSE);
104 $form->assign('blockId', $instance);
105 }
106 else {
107 //we are going to build other block instances w/ AJAX
108 $generateAjaxRequest++;
109 $ajaxRequestBlocks[$blockName][$instance] = TRUE;
110 }
111
112 $form->set($blockName . '_Block_Count', $instance);
e52506b0 113 $formName = 'CRM_Contact_Form_Edit_' . $blockName;
481a74f4 114 $formName::buildQuickForm($form);
6a488035
TO
115 }
116 }
117
118 //assign to generate AJAX request for building extra blocks.
119 $form->assign('generateAjaxRequest', $generateAjaxRequest);
120 $form->assign('ajaxRequestBlocks', $ajaxRequestBlocks);
121 }
96025800 122
6a488035 123}