]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Parsers/Rules/AddressRule.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Parsers / Rules / AddressRule.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 /**
40  * AddressRule.php
41  * 
42  * This is a utility base class to provide further refinement when converting 
43  * pre 5.x files to the new meta-data rules.  Address fields defined in the
44  * address panel will be removed as the new metadata definition will be merged later.
45  * If the address fields are outside the address panel, we will keep them as is, but
46  * ensure that they are not defined as Arrays.
47  * @author Collin Lee
48  */
49  
50 require_once('include/SugarFields/Parsers/Rules/BaseRule.php');
51  
52 class AddressRule extends BaseRule {
53
54 function AddressRule() {
55   
56 }
57
58 function parsePanels($panels, $view) {
59    $searchedAddressPanel = array();
60    
61    foreach($panels as $name=>$panel) {
62         
63           $isAddressPanel = $name == 'lbl_address_information';
64           
65           foreach($panel as $rowCount=>$row) {
66                  foreach($row as $key=>$column) {
67
68                         if($this->matches($column, '/_address_(city|state|country|postalcode)$/si')) {
69                            if($view == 'DetailView' && !is_array($column)) { 
70                                   $panels[$name][$rowCount][$key] = '';
71                            } else if($view == 'DetailView' && $this->matches($column, '/_address_country$/') && is_array($column)) {
72                               $match = $this->getMatch($column, '/(.*?)_address_country$/');
73                   $panels[$name][$rowCount][$key]['name'] = $match[1] . '_address_street';
74                   $panels[$name][$rowCount][$key]['label'] = 'LBL_' . strtoupper($match[1]) . '_ADDRESS';
75                            } else if($view == 'EditView' && $isAddressPanel) {
76           
77                       $field = is_array($column) ? $column['name'] : $column;
78                       preg_match('/^(.*?)_address_/si', $field, $matches);
79                        
80                                   if(empty($searchedAddressPanel[$matches[1]])) {    
81                                      $intact = $this->hasAddressFieldsIntact($panel, $matches[1]);
82                                      
83                                      //now we actually have to go back in and replace the street field
84                                      if(!$intact) {
85                                         $panels = $this->removeStreetFieldOverride($panels, $matches[1]);
86                                      }
87                                      
88                                      $addressFieldsIntact[$matches[1]] = $intact;
89                                      $searchedAddressPanel[$matches[1]] = true;
90                                   }
91                                   
92                                   //Only remove in address panel if the street field is in there by itself
93                                   if($addressFieldsIntact[$matches[1]]) {
94                                          $panels[$name][$rowCount][$key] = '';
95                                   }
96                            }
97                         } else if($this->matches($column, '/^push_.*?_(shipping|billing)$/si')) {
98                            $panels[$name][$rowCount][$key] = '';
99                         } 
100                  } //foreach
101           } //foreach
102    } //foreach
103    return $panels;
104 }
105
106 /**
107  * hasAddressFieldsIntact
108  * This function checks to see if the address fields for the given street key is
109  * intact.  This means that all five fields (street, city, state, country and postalcode)
110  * have not been moved from the address panel
111  * 
112  * @param $addressPanel Array of address panel contents
113  * @param $suffix The address suffix (billing, shipping, primary, alternate) to check for
114  * @return boolean
115  */
116 function hasAddressFieldsIntact($addressPanel, $suffix) {
117    $expression = '/^' . $suffix . '_address_(street|city|state|country|postalcode)$/si';
118    $count = 0;
119    foreach($addressPanel as $rowCount=>$row) {
120           foreach($row as $key=>$column) {
121              if($this->matches($column, $expression)) {
122                 $count++;       
123              }
124           }
125    }
126    return $count == 5;
127 }
128
129
130 /**
131  * removeStreetFieldOverride
132  * This function scans the panels and locates the street address field for the given key
133  * and replaces the Array definition (from the merging process) with a String value.
134  * @param $panels Array of the view's panels
135  * @param $street String key value of the street to search for
136  * @returns $panels Array of view's panels with street value substituted
137  */
138 function removeStreetFieldOverride($panels, $street) {
139    $expression = '/^' . $street . '_address_street$/si';
140    foreach($panels as $name=>$panel) {
141         foreach($panel as $rowCount=>$row) {
142           foreach($row as $key=>$column) {
143              if($this->matches($column, $expression)) {
144                  $panels[$name][$rowCount][$key] = $street . '_address_street';
145              }
146           }
147         }
148    }
149    return $panels;
150 }
151
152 }
153
154 ?>