PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/DynamicFields/templates/Fields/TemplateRange.php

https://github.com/jacknicole/sugarcrm_dev
PHP | 293 lines | 182 code | 46 blank | 65 comment | 59 complexity | 352d48a341a2e4f81992b694c3344f1b MD5 | raw file
  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-2011 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. class TemplateRange extends TemplateText
  38. {
  39. /**
  40. * __construct
  41. *
  42. * Constructor for class. This constructor ensures that TemplateRanage instances have the
  43. * enable_range_search vardef value.
  44. */
  45. function __construct()
  46. {
  47. $this->vardef_map['enable_range_search'] = 'enable_range_search';
  48. $this->vardef_map['options'] = 'options';
  49. }
  50. /**
  51. * populateFromPost
  52. *
  53. * @see parent::populateFromPost
  54. * This method checks to see if enable_range_search is set. If so, ensure that the
  55. * searchdefs for the module include the additional range fields.
  56. */
  57. function populateFromPost() {
  58. parent::populateFromPost();
  59. //If we are enabling range search, make sure we add the start and end range fields
  60. if (!empty($this->enable_range_search))
  61. {
  62. //If range search is enabled, set the options attribute for the dropdown choice selections
  63. $this->options = ($this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime') ? 'date_range_search_dom' : 'numeric_range_search_dom';
  64. if(isset($_REQUEST['view_module']))
  65. {
  66. $module = $_REQUEST['view_module'];
  67. if (file_exists('modules/'.$module.'/metadata/SearchFields.php'))
  68. {
  69. require('modules/'.$module.'/metadata/SearchFields.php');
  70. }
  71. if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  72. {
  73. require('custom/modules/'.$module.'/metadata/SearchFields.php');
  74. }
  75. $field_name = $this->get_field_name($module, $_REQUEST['name']);
  76. if(isset($searchFields[$module]))
  77. {
  78. $field_name_range = 'range_' . $field_name;
  79. $field_name_start = 'start_range_' . $field_name;
  80. $field_name_end = 'end_range_' . $field_name;
  81. $isDateField = $this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime';
  82. $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
  83. if($isDateField)
  84. {
  85. $searchFields[$module][$field_name_range]['is_date_field'] = true;
  86. }
  87. $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
  88. if($isDateField)
  89. {
  90. $searchFields[$module][$field_name_start]['is_date_field'] = true;
  91. }
  92. $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
  93. if($isDateField)
  94. {
  95. $searchFields[$module][$field_name_end]['is_date_field'] = true;
  96. }
  97. if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  98. {
  99. mkdir_recursive('custom/modules/'.$module.'/metadata');
  100. }
  101. write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
  102. }
  103. if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
  104. {
  105. unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
  106. }
  107. if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
  108. {
  109. unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
  110. }
  111. }
  112. } else {
  113. //Otherwise, try to restore the searchFields to their state prior to being enabled
  114. if(isset($_REQUEST['view_module']))
  115. {
  116. $module = $_REQUEST['view_module'];
  117. if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
  118. require('modules/'.$module.'/metadata/SearchFields.php');
  119. }
  120. if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  121. {
  122. require('custom/modules/'.$module.'/metadata/SearchFields.php');
  123. }
  124. $field_name = $this->get_field_name($module, $_REQUEST['name']);
  125. if(isset($searchFields[$module]))
  126. {
  127. $field_name_range = 'range_' . $field_name;
  128. $field_name_start = 'start_range_' . $field_name;
  129. $field_name_end = 'end_range_' . $field_name;
  130. if(isset($searchFields[$module][$field_name_range]))
  131. {
  132. unset($searchFields[$module][$field_name_range]);
  133. }
  134. if(isset($searchFields[$module][$field_name_start]))
  135. {
  136. unset($searchFields[$module][$field_name_start]);
  137. }
  138. if(isset($searchFields[$module][$field_name_end]))
  139. {
  140. unset($searchFields[$module][$field_name_end]);
  141. }
  142. if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  143. {
  144. mkdir_recursive('custom/modules/'.$module.'/metadata');
  145. }
  146. write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
  147. }
  148. if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
  149. {
  150. unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
  151. }
  152. if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
  153. {
  154. unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * get_field_def
  161. *
  162. * @see parent::get_field_def
  163. * This method checks to see if the enable_range_search key/value entry should be
  164. * added to the vardef entry representing the module
  165. */
  166. function get_field_def()
  167. {
  168. $vardef = parent::get_field_def();
  169. if(!empty($this->enable_range_search))
  170. {
  171. $vardef['enable_range_search'] = $this->enable_range_search;
  172. $vardef['options'] = ($this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime') ? 'date_range_search_dom' : 'numeric_range_search_dom';
  173. } else {
  174. $vardef['enable_range_search'] = false;
  175. }
  176. return $vardef;
  177. }
  178. /**
  179. * get_field_name
  180. * This function gets the correct name for the field name being submitted from studio
  181. *
  182. * @param String $module The name of the module
  183. * @param String $name The field name
  184. */
  185. private function get_field_name($module, $name)
  186. {
  187. $bean = loadBean($module);
  188. if(empty($bean) || is_null($bean))
  189. {
  190. return $name;
  191. }
  192. $field_defs = $bean->field_defs;
  193. $field_name = isset($field_defs[$_REQUEST['name']]) ? $_REQUEST['name'] : $_REQUEST['name'] . '_c';
  194. return $field_name;
  195. }
  196. public static function repairCustomSearchFields($vardefs, $module, $package='')
  197. {
  198. $fields = array();
  199. //Find any range search enabled fields
  200. foreach($vardefs as $key=>$field)
  201. {
  202. if(!empty($field['enable_range_search'])) {
  203. $fields[$field['name']] = $field;
  204. }
  205. }
  206. if(!empty($fields))
  207. {
  208. if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  209. {
  210. require('custom/modules/'.$module.'/metadata/SearchFields.php');
  211. } else if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
  212. require('modules/'.$module.'/metadata/SearchFields.php');
  213. } else if (file_exists('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php')) {
  214. require('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php');
  215. }
  216. foreach($fields as $field_name=>$field)
  217. {
  218. $field_name_range = 'range_' . $field_name;
  219. $field_name_start = 'start_range_' . $field_name;
  220. $field_name_end = 'end_range_' . $field_name;
  221. $type = $field['type'];
  222. $isDateField = $type == 'date' || $type == 'datetimecombo' || $type == 'datetime';
  223. $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
  224. if($isDateField)
  225. {
  226. $searchFields[$module][$field_name_range]['is_date_field'] = true;
  227. }
  228. $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
  229. if($isDateField)
  230. {
  231. $searchFields[$module][$field_name_start]['is_date_field'] = true;
  232. }
  233. $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
  234. if($isDateField)
  235. {
  236. $searchFields[$module][$field_name_end]['is_date_field'] = true;
  237. }
  238. }
  239. if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
  240. {
  241. mkdir_recursive('custom/modules/'.$module.'/metadata');
  242. }
  243. write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
  244. }
  245. }
  246. }
  247. ?>