/custom/include/SubPanel/SubPanelDefinitions.php

https://gitlab.com/tjaafar/SuiteCRM · PHP · 205 lines · 150 code · 24 blank · 31 comment · 34 complexity · 5b03132f56fa9e1d1ba2f60a0e1ea40a MD5 · raw file

  1. <?php
  2. /**
  3. * SubPanelDefinitions.php
  4. * @author SalesAgility <support@salesagility.com>
  5. * Date: 28/01/14
  6. */
  7. require_once('include/SubPanel/SubPanelDefinitions.php');
  8. class CustomaSubPanel extends aSubPanel
  9. {
  10. var $search_query;
  11. var $base_collection_list = array();
  12. function CustomaSubPanel($name , $instance_properties , $parent_bean , $reload = false , $original_only = false, $search_query = '', $collections = array() ){
  13. $this->_instance_properties = $instance_properties ;
  14. if(isset($instance_properties['collection_list' ])) $this->base_collection_list = $instance_properties['collection_list' ];
  15. if(!empty($collections) && isset($instance_properties['collection_list' ])){
  16. foreach($instance_properties['collection_list' ] as $cname => $value){
  17. if(!in_array($value['module'], $collections)){
  18. unset($instance_properties['collection_list'][$cname]);
  19. }
  20. }
  21. }
  22. if (!$this->isCollection()){
  23. $table = strtolower($instance_properties['module']);
  24. $search_query = str_replace('meetings',$table,$search_query);
  25. }
  26. $this->search_query = $search_query;
  27. parent::aSubPanel($name , $instance_properties , $parent_bean , $reload , $original_only);
  28. }
  29. //returns the where clause for the query.
  30. function get_where ()
  31. {
  32. if($this->get_def_prop_value ( 'where' ) != '' && $this->search_query != ''){
  33. return $this->get_def_prop_value ( 'where' ).' AND '.$this->search_query;
  34. } else if($this->search_query != ''){
  35. return $this->search_query;
  36. }
  37. return $this->get_def_prop_value ( 'where' ) ;
  38. }
  39. /**
  40. * Load the Sub-Panel objects if it can from the metadata files.
  41. *
  42. * call this function for sub-panels that have unions.
  43. *
  44. * @return bool True by default if the subpanel was loaded. Will return false if none in the collection are
  45. * allowed by the current user.
  46. */
  47. function load_sub_subpanels ()
  48. {
  49. global $modListHeader ;
  50. // added a check for security of tabs to see if an user has access to them
  51. // this prevents passing an "unseen" tab to the query string and pulling up its contents
  52. if (! isset ( $modListHeader ))
  53. {
  54. global $current_user ;
  55. if (isset ( $current_user ))
  56. {
  57. $modListHeader = query_module_access_list ( $current_user ) ;
  58. }
  59. }
  60. //by default all the activities modules are exempt, so hiding them won't affect their appearance unless the 'activity' subpanel itself is hidden.
  61. //add email to the list temporarily so it is not affected in activities subpanel
  62. global $modules_exempt_from_availability_check ;
  63. $modules_exempt_from_availability_check['Emails'] = 'Emails';
  64. $listFieldMap = array();
  65. if (empty ( $this->sub_subpanels ))
  66. {
  67. $panels = $this->get_inst_prop_value ( 'collection_list' ) ;
  68. foreach ( $panels as $panel => $properties )
  69. {
  70. if (array_key_exists ( $properties [ 'module' ], $modListHeader ) or array_key_exists ( $properties [ 'module' ], $modules_exempt_from_availability_check ))
  71. {
  72. $this->sub_subpanels [ $panel ] = new CustomaSubPanel ( $panel, $properties, $this->parent_bean, false, false, $this->search_query ) ;
  73. }
  74. }
  75. // if it's empty just dump out as there is nothing to process.
  76. if(empty($this->sub_subpanels)) return false;
  77. //Sync displayed list fields across the subpanels
  78. $display_fields = $this->getDisplayFieldsFromCollection($this->sub_subpanels);
  79. $query_fields = array();
  80. foreach ( $this->sub_subpanels as $key => $subpanel )
  81. {
  82. $list_fields = $subpanel->get_list_fields();
  83. $listFieldMap[$key] = array();
  84. $index = 0;
  85. foreach($list_fields as $field => $def)
  86. {
  87. if (isset($def['vname']) && isset($def['width']))
  88. {
  89. $index++;
  90. if(!empty($def['alias']))
  91. $listFieldMap[$key][$def['alias']] = $field;
  92. else
  93. $listFieldMap[$key][$field] = $field;
  94. if (!isset($display_fields[$def['vname']]))
  95. {
  96. if(sizeof($display_fields) > $index)
  97. {
  98. //Try to insert the new field in an order that makes sense
  99. $start = array_slice($display_fields, 0, $index);
  100. $end = array_slice($display_fields, $index);
  101. $display_fields = array_merge(
  102. $start,
  103. array($def['vname'] => array('name' => $field, 'vname' => $def['vname'], 'width' => $def['width'] )),
  104. $end
  105. );
  106. } else
  107. {
  108. $display_fields[$def['vname']] = array(
  109. 'name' => empty($def['alias']) ? $field : $def['alias'],
  110. 'vname' => $def['vname'],
  111. 'width' => $def['width'],
  112. );
  113. }
  114. }
  115. } else {
  116. $query_fields[$field] = $def;
  117. }
  118. }
  119. }
  120. foreach ( $this->sub_subpanels as $key => $subpanel )
  121. {
  122. $list_fields = array();
  123. foreach($display_fields as $vname => $def)
  124. {
  125. $field = $def['name'];
  126. $list_key = isset($listFieldMap[$key][$field]) ? $listFieldMap[$key][$field] : $field;
  127. if (isset($subpanel->panel_definition['list_fields'][$field]))
  128. {
  129. $list_fields[$field] = $subpanel->panel_definition['list_fields'][$field];
  130. }
  131. else if ($list_key != $field && isset($subpanel->panel_definition['list_fields'][$list_key]))
  132. {
  133. $list_fields[$list_key] = $subpanel->panel_definition['list_fields'][$list_key];
  134. }
  135. else {
  136. $list_fields[$field] = $display_fields[$vname];
  137. }
  138. }
  139. foreach($query_fields as $field => $def)
  140. {
  141. if (isset($subpanel->panel_definition['list_fields'][$field]))
  142. {
  143. $list_fields[$field] = $subpanel->panel_definition['list_fields'][$field];
  144. }
  145. else {
  146. $list_fields[$field] = $def;
  147. }
  148. }
  149. $subpanel->panel_definition['list_fields'] = $list_fields;
  150. }
  151. }
  152. return true;
  153. }
  154. }
  155. class CustomSubPanelDefinitions extends SubPanelDefinitions
  156. {
  157. /**
  158. * Enter description here...
  159. *
  160. * @param BEAN $focus - this is the bean you want to get the data from
  161. * @param STRING $layout_def_key - if you wish to use a layout_def defined in the default metadata/subpaneldefs.php that is not keyed off of $bean->module_dir pass in the key here
  162. * @param ARRAY $layout_def_override - if you wish to override the default loaded layout defs you pass them in here.
  163. * @return SubPanelDefinitions
  164. */
  165. function CustomSubPanelDefinitions ( $focus , $layout_def_key = '' , $layout_def_override = '' )
  166. {
  167. parent::SubPanelDefinitions($focus , $layout_def_key, $layout_def_override);
  168. }
  169. function load_subpanel ( $name , $reload = false , $original_only = false, $search_query = '', $collections = array() )
  170. {
  171. if (!is_dir('modules/' . $this->layout_defs [ 'subpanel_setup' ][ strtolower ( $name ) ] [ 'module' ]))
  172. return false;
  173. $subpanel = new CustomaSubPanel ( $name, $this->layout_defs [ 'subpanel_setup' ] [ strtolower ( $name ) ], $this->_focus, $reload, $original_only, $search_query, $collections ) ;
  174. // only return the subpanel object if we can display it.
  175. if($subpanel->canDisplay == true) {
  176. return $subpanel;
  177. }
  178. // by default return false so we don't show anything if it's not required.
  179. return false;
  180. }
  181. }