PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/ModuleBuilder/parsers/parser.modifylistview.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 333 lines | 245 code | 18 blank | 70 comment | 60 complexity | 3fb2dba58c841938c369682a72d9a788 MD5 | raw file
  1. <?php
  2. if (! defined ( 'sugarEntry' ) || ! sugarEntry)
  3. die ( 'Not A Valid Entry Point' ) ;
  4. /*********************************************************************************
  5. * SugarCRM Community Edition is a customer relationship management program developed by
  6. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  7. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  8. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it under
  11. * the terms of the GNU Affero General Public License version 3 as published by the
  12. * Free Software Foundation with the addition of the following permission added
  13. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  14. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  15. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  16. *
  17. * This program is distributed in the hope that it will be useful, but WITHOUT
  18. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  19. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License along with
  23. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  24. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  25. * 02110-1301 USA.
  26. *
  27. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  28. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  29. *
  30. * The interactive user interfaces in modified source and object code versions
  31. * of this program must display Appropriate Legal Notices, as required under
  32. * Section 5 of the GNU Affero General Public License version 3.
  33. *
  34. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  35. * these Appropriate Legal Notices must retain the display of the "Powered by
  36. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  37. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  38. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  39. ********************************************************************************/
  40. require_once ('modules/ModuleBuilder/parsers/ModuleBuilderParser.php') ;
  41. class ParserModifyListView extends ModuleBuilderParser
  42. {
  43. var $listViewDefs = false ;
  44. var $defaults = array ( ) ;
  45. var $additional = array ( ) ;
  46. var $available = array ( ) ;
  47. var $reserved = array(); // fields marked by 'studio'=>false in the listviewdefs; need to be preserved
  48. // var $language_module = '';
  49. var $columns = array ( 'LBL_DEFAULT' => 'getDefaultFields' , 'LBL_AVAILABLE' => 'getAdditionalFields' , 'LBL_HIDDEN' => 'getAvailableFields' ) ;
  50. function init ( $module_name , $submodule = '' )
  51. {
  52. global $app_list_strings ;
  53. $this->module_name = $module_name ;
  54. $mod_strings = return_module_language ( $GLOBALS [ 'current_language' ], $this->module_name ) ; // needed solely so that listviewdefs that reference this can be included without error
  55. $class = $GLOBALS [ 'beanList' ] [ $this->module_name ] ;
  56. require_once ($GLOBALS [ 'beanFiles' ] [ $class ]) ;
  57. $this->module = new $class ( ) ;
  58. $loaded = $this->_loadFromFile('ListView','modules/' . $this->module_name . '/metadata/listviewdefs.php',$this->module_name);
  59. $this->originalListViewDefs = $loaded['viewdefs'] [ $this->module_name ] ;
  60. $this->_variables = $loaded['variables'];
  61. // _pp($loaded);
  62. $this->customFile = 'custom/modules/' . $this->module_name . '/metadata/listviewdefs.php' ;
  63. if (file_exists ( $this->customFile ))
  64. {
  65. $loaded = $this->_loadFromFile('ListView',$this->customFile,$this->module_name);
  66. $this->listViewDefs = $loaded['viewdefs'] [ $this->module_name ] ;
  67. $this->_variables = $loaded['variables'];
  68. } else
  69. {
  70. $this->listViewDefs = & $this->originalListViewDefs ;
  71. }
  72. $this->fixKeys ( $this->originalListViewDefs ) ;
  73. $this->fixKeys ( $this->listViewDefs ) ;
  74. $this->language_module = $this->module_name ;
  75. }
  76. function getLanguage()
  77. {
  78. return $this->language_module;
  79. }
  80. // re-key array so that every entry has a key=name and all keys are lowercase - makes it easier in handleSave() later...
  81. function fixKeys ( &$defs )
  82. {
  83. $temp = array ( ) ;
  84. foreach ( $defs as $key => $value )
  85. {
  86. if (! is_array ( $value ))
  87. {
  88. $key = $value ;
  89. $def = array ( ) ;
  90. $def [ 'name' ] = (isset ( $this->module->field_defs [ $key ] )) ? $this->module->field_defs [ $key ] [ 'name' ] : $key ;
  91. $value = $def ;
  92. }
  93. if (isset ( $value [ 'name' ] ))
  94. {
  95. $key = $value [ 'name' ] ; // override key with name, needed when the entry lacks a key
  96. }
  97. $temp [ strtolower ( $key ) ] = $value ;
  98. }
  99. $defs = $temp ;
  100. }
  101. /**
  102. * returns the default fields for a listview
  103. * Called only when displaying the listview for editing; not called when saving
  104. */
  105. function getDefaultFields ()
  106. {
  107. $this->defaults = array ( ) ;
  108. foreach ( $this->listViewDefs as $key => $def )
  109. {
  110. // add in the default fields from the listviewdefs, stripping out any field with 'studio' set to a value other than true
  111. // Important: the 'studio' fields must be added back into the layout on save, as they're not editable rather than hidden
  112. if (! empty ( $def [ 'default' ] ))
  113. {
  114. if (! isset($def['studio']) || $def['studio'] === true)
  115. {
  116. $this->defaults [ $key ] = $def ;
  117. }
  118. else
  119. // anything which doesn't go into the defaults is a reserved field - this makes sure we don't miss anything
  120. {
  121. $this->reserved [ $key ] = $def;
  122. }
  123. }
  124. }
  125. return $this->defaults ;
  126. }
  127. /**
  128. * returns additional fields available for users to create fields
  129. */
  130. function getAdditionalFields ()
  131. {
  132. $this->additional = array ( ) ;
  133. foreach ( $this->listViewDefs as $key => $def )
  134. {
  135. if (empty ( $def [ 'default' ] ))
  136. {
  137. $key = strtolower ( $key ) ;
  138. $this->additional [ $key ] = $def ;
  139. }
  140. }
  141. return $this->additional ;
  142. }
  143. /**
  144. * returns unused fields that are available for using in either default or additional list views
  145. */
  146. function getAvailableFields ()
  147. {
  148. $this->availableFields = array ( ) ;
  149. $lowerFieldList = array_change_key_case ( $this->listViewDefs ) ;
  150. foreach ( $this->originalListViewDefs as $key => $def )
  151. {
  152. $key = strtolower ( $key ) ;
  153. if (! isset ( $lowerFieldList [ $key ] ))
  154. {
  155. $this->availableFields [ $key ] = $def ;
  156. }
  157. }
  158. $GLOBALS['log']->debug('parser.modifylistview.php->getAvailableFields(): field_defs='.print_r($this->availableFields,true));
  159. $modFields = !empty($this->module->field_name_map) ? $this->module->field_name_map : $this->module->field_defs;
  160. foreach ( $modFields as $key => $def )
  161. {
  162. $fieldName = strtolower ( $key ) ;
  163. if ($fieldName == 'currency_id')
  164. continue;
  165. if (!isset ( $lowerFieldList [ $fieldName ] )) // bug 16728 - check this first, so that other conditions (e.g., studio == visible) can't override and add duplicate entries
  166. {
  167. // bug 19656: this test changed after 5.0.0b - we now remove all ID type fields - whether set as type, or dbtype, from the fielddefs
  168. if ($this->isValidField($key, $def)){
  169. $label = (isset ( $def [ 'vname' ] )) ? $def [ 'vname' ] : (isset($def [ 'label' ]) ? $def['label'] : $def['name']) ;
  170. $this->availableFields [ $fieldName ] = array ( 'width' => '10' , 'label' => $label ) ;
  171. }
  172. }
  173. }
  174. return $this->availableFields ;
  175. }
  176. function getFieldDefs()
  177. {
  178. return $this->module->field_defs;
  179. }
  180. function isValidField($key, $def) {
  181. //Allow fields that are studio visible
  182. if (! empty ( $def [ 'studio' ] ) && $def [ 'studio' ] == 'visible')
  183. return true;
  184. //No ID fields
  185. if ((!empty ( $def [ 'dbType' ] ) && $def [ 'dbType' ] == 'id') || (!empty ( $def [ 'type' ] ) && $def [ 'type' ] == 'id'))
  186. return false;
  187. //only allow DB and custom fields (if a source is specified)
  188. if (!empty($def [ 'source' ]) && $def [ 'source' ] != 'db' && $def [ 'source' ] != 'custom_fields')
  189. return false;
  190. //Dont ever show the "deleted" fields or "_name" fields
  191. if (strcmp ( $key, 'deleted' ) == 0 || (isset ( $def [ 'name' ] ) && strpos ( $def [ 'name' ], "_name" ) !== false))
  192. return false;
  193. //If none of the "ifs" are true, the field is valid
  194. return true;
  195. }
  196. function getField ( $fieldName )
  197. {
  198. $fieldName = strtolower ( $fieldName ) ;
  199. foreach ( $this->listViewDefs as $key => $def )
  200. {
  201. $key = strtolower ( $key ) ;
  202. if ($key == $fieldName)
  203. {
  204. return $def ;
  205. }
  206. }
  207. foreach ( $this->module->field_defs as $key => $def )
  208. {
  209. $key = strtolower ( $key ) ;
  210. if ($key == $fieldName)
  211. {
  212. return $def ;
  213. }
  214. }
  215. return array ( ) ;
  216. }
  217. function addRelateData($fieldname, $listfielddef) {
  218. $modFieldDef = $this->module->field_defs [ strtolower ( $fieldname ) ];
  219. if (!empty($modFieldDef['module']) && !empty($modFieldDef['id_name'])) {
  220. $listfielddef['module'] = $modFieldDef['module'];
  221. $listfielddef['id'] = strtoupper($modFieldDef['id_name']);
  222. $listfielddef['link'] = true;
  223. $listfielddef['related_fields'] = array (strtolower($modFieldDef['id_name']));
  224. }
  225. return $listfielddef;
  226. }
  227. function _loadLayoutFromRequest ()
  228. {
  229. $GLOBALS['log']->debug("ParserModifyListView->_loadLayoutFromRequest()");
  230. $fields = array ( ) ;
  231. $rejectTypes = array ( 'html' , 'enum' , 'text' ) ;
  232. for ( $i = 0 ; isset ( $_POST [ 'group_' . $i ] ) && $i < 2 ; $i ++ )
  233. {
  234. //echo "\n***group-$i Size:".sizeof($_POST['group_' . $i])."\n";
  235. foreach ( $_POST [ 'group_' . $i ] as $field )
  236. {
  237. $fieldname = strtoupper ( $field ) ;
  238. //originalListViewDefs are all lower case
  239. $lowerFieldName = strtolower ( $field ) ;
  240. if (isset ( $this->originalListViewDefs [ $lowerFieldName ] ))
  241. {
  242. $fields [ $fieldname ] = $this->originalListViewDefs [ $lowerFieldName ] ;
  243. } else
  244. {
  245. //check if we have the case wrong for custom fields
  246. if (! isset ( $this->module->field_defs [ $fieldname ] ))
  247. {
  248. foreach ( $this->module->field_defs as $key => $value )
  249. {
  250. if (strtoupper ( $key ) == $fieldname)
  251. {
  252. $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $key ] [ 'vname' ] ) ;
  253. break ;
  254. }
  255. }
  256. } else
  257. {
  258. $fields [ $fieldname ] = array ( 'width' => 10 , 'label' => $this->module->field_defs [ $fieldname ] [ 'vname' ] ) ;
  259. }
  260. // sorting fields of certain types will cause a database engine problems
  261. // we only check this for custom fields, as we assume that OOB fields have been independently confirmed as ok
  262. if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] ) && (in_array ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ], $rejectTypes ) || isset($this->module->field_defs [ strtolower ( $fieldname ) ]['custom_module'])))
  263. {
  264. $fields [ $fieldname ] [ 'sortable' ] = false ;
  265. }
  266. // Bug 23728 - Make adding a currency type field default to setting the 'currency_format' to true
  267. if (isset ( $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type ' ] ) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'currency')
  268. {
  269. $fields [ $fieldname ] [ 'currency_format' ] = true;
  270. }
  271. }
  272. if (isset ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ] ))
  273. {
  274. $width = substr ( $_REQUEST [ strtolower ( $fieldname ) . 'width' ], 6, 3 ) ;
  275. if (strpos ( $width, "%" ) !== false)
  276. {
  277. $width = substr ( $width, 0, 2 ) ;
  278. }
  279. if ($width < 101 && $width > 0)
  280. {
  281. $fields [ $fieldname ] [ 'width' ] = $width ;
  282. }
  283. } else if (isset ( $this->listViewDefs [ $fieldname ] [ 'width' ] ))
  284. {
  285. $fields [ $fieldname ] [ 'width' ] = $this->listViewDefs [ $fieldname ] [ 'width' ] ;
  286. }
  287. //Get additional Data for relate fields
  288. if (isset($this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ]) && $this->module->field_defs [ strtolower ( $fieldname ) ] [ 'type' ] == 'relate') {
  289. $fields [ $fieldname ] = $this->addRelateData($field, $fields [ $fieldname ]);
  290. }
  291. $fields [ $fieldname ] [ 'default' ] = ($i == 0) ;
  292. }
  293. }
  294. // Add the reserved fields back in to the end of the default fields in the layout
  295. // ASSUMPTION: reserved fields go back at the end
  296. // First, load the reserved fields - we cannot assume that getDefaultFields has been called earlier when saving
  297. $this->getDefaultFields();
  298. foreach ( $this->reserved as $key => $def)
  299. {
  300. $fields[ $key ] = $def;
  301. }
  302. return $fields ;
  303. }
  304. function handleSave ()
  305. {
  306. $fields = $this->_loadLayoutFromRequest();
  307. $this->_writeToFile($this->customFile,'ListView',$this->module_name,$fields,$this->_variables);
  308. $GLOBALS [ "listViewDefs" ] [ $this->module_name ] = $fields ;
  309. // now clear the cache so that the results are immediately visible
  310. include_once ('include/TemplateHandler/TemplateHandler.php') ;
  311. TemplateHandler::clearCache ( $this->module_name, "ListView.tpl" ) ; // not currently cached, but here for the future
  312. }
  313. }
  314. ?>