PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/include/SugarFields/Parsers/Rules/AddressRule.php

https://github.com/yinhm/sugarcrm
PHP | 153 lines | 69 code | 20 blank | 64 comment | 24 complexity | 0d3673afc45a5c813779facc44b88771 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2010 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. * AddressRule.php
  39. *
  40. * This is a utility base class to provide further refinement when converting
  41. * pre 5.x files to the new meta-data rules. Address fields defined in the
  42. * address panel will be removed as the new metadata definition will be merged later.
  43. * If the address fields are outside the address panel, we will keep them as is, but
  44. * ensure that they are not defined as Arrays.
  45. * @author Collin Lee
  46. */
  47. require_once('include/SugarFields/Parsers/Rules/BaseRule.php');
  48. class AddressRule extends BaseRule {
  49. function AddressRule() {
  50. }
  51. function parsePanels($panels, $view) {
  52. $searchedAddressPanel = array();
  53. foreach($panels as $name=>$panel) {
  54. $isAddressPanel = $name == 'lbl_address_information';
  55. foreach($panel as $rowCount=>$row) {
  56. foreach($row as $key=>$column) {
  57. if($this->matches($column, '/_address_(city|state|country|postalcode)$/si')) {
  58. if($view == 'DetailView' && !is_array($column)) {
  59. $panels[$name][$rowCount][$key] = '';
  60. } else if($view == 'DetailView' && $this->matches($column, '/_address_country$/') && is_array($column)) {
  61. $match = $this->getMatch($column, '/(.*?)_address_country$/');
  62. $panels[$name][$rowCount][$key]['name'] = $match[1] . '_address_street';
  63. $panels[$name][$rowCount][$key]['label'] = 'LBL_' . strtoupper($match[1]) . '_ADDRESS';
  64. } else if($view == 'EditView' && $isAddressPanel) {
  65. $field = is_array($column) ? $column['name'] : $column;
  66. preg_match('/^(.*?)_address_/si', $field, $matches);
  67. if(empty($searchedAddressPanel[$matches[1]])) {
  68. $intact = $this->hasAddressFieldsIntact($panel, $matches[1]);
  69. //now we actually have to go back in and replace the street field
  70. if(!$intact) {
  71. $panels = $this->removeStreetFieldOverride($panels, $matches[1]);
  72. }
  73. $addressFieldsIntact[$matches[1]] = $intact;
  74. $searchedAddressPanel[$matches[1]] = true;
  75. }
  76. //Only remove in address panel if the street field is in there by itself
  77. if($addressFieldsIntact[$matches[1]]) {
  78. $panels[$name][$rowCount][$key] = '';
  79. }
  80. }
  81. } else if($this->matches($column, '/^push_.*?_(shipping|billing)$/si')) {
  82. $panels[$name][$rowCount][$key] = '';
  83. }
  84. } //foreach
  85. } //foreach
  86. } //foreach
  87. return $panels;
  88. }
  89. /**
  90. * hasAddressFieldsIntact
  91. * This function checks to see if the address fields for the given street key is
  92. * intact. This means that all five fields (street, city, state, country and postalcode)
  93. * have not been moved from the address panel
  94. *
  95. * @param $addressPanel Array of address panel contents
  96. * @param $suffix The address suffix (billing, shipping, primary, alternate) to check for
  97. * @return boolean
  98. */
  99. function hasAddressFieldsIntact($addressPanel, $suffix) {
  100. $expression = '/^' . $suffix . '_address_(street|city|state|country|postalcode)$/si';
  101. $count = 0;
  102. foreach($addressPanel as $rowCount=>$row) {
  103. foreach($row as $key=>$column) {
  104. if($this->matches($column, $expression)) {
  105. $count++;
  106. }
  107. }
  108. }
  109. return $count == 5;
  110. }
  111. /**
  112. * removeStreetFieldOverride
  113. * This function scans the panels and locates the street address field for the given key
  114. * and replaces the Array definition (from the merging process) with a String value.
  115. * @param $panels Array of the view's panels
  116. * @param $street String key value of the street to search for
  117. * @returns $panels Array of view's panels with street value substituted
  118. */
  119. function removeStreetFieldOverride($panels, $street) {
  120. $expression = '/^' . $street . '_address_street$/si';
  121. foreach($panels as $name=>$panel) {
  122. foreach($panel as $rowCount=>$row) {
  123. foreach($row as $key=>$column) {
  124. if($this->matches($column, $expression)) {
  125. $panels[$name][$rowCount][$key] = $street . '_address_street';
  126. }
  127. }
  128. }
  129. }
  130. return $panels;
  131. }
  132. }
  133. ?>