2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once('include/SugarFields/Fields/Collection/SugarFieldCollection.php');
40 class ViewSugarFieldCollection{
41 var $ss; // Sugar Smarty Object
46 var $displayParams; // DisplayParams for the collection field (defined in the metadata)
47 var $vardef; // vardef of the collection field.
48 var $related_module; // module name of the related module
49 var $module_dir; // name of the module where the collection field is.
54 var $skipModuleQuickSearch = false;
55 var $field_to_name_array; //mapping of fields for the return of the select popup
56 var $showSelectButton = true;
57 var $hideShowHideButton = false;
61 function ViewSugarFieldCollection($fill_data = true){
62 $this->json = getJSONobj();
64 $this->displayParams = $this->json->decode(html_entity_decode($_REQUEST['displayParams']));
65 $this->vardef = $this->json->decode(html_entity_decode($_REQUEST['vardef']));
66 $this->module_dir = $_REQUEST['module_dir'];
67 $this->action_type = $_REQUEST['action_type'];
68 $this->name = $this->vardef['name'];
69 $this->value_name = $this->name . '_values';
71 $this->ss = new Sugar_Smarty();
72 $this->edit_tpl_path = $this->findTemplate('CollectionEditView');
73 $this->detail_tpl_path = $this->findTemplate('CollectionDetailView');
74 $this->extra_var = array();
75 $this->field_to_name_array = array();
79 * Retrieve the related module and load the bean and the relationship
80 * call retrieve values()
83 if(!class_exists('Relationship')){
86 $rel = new Relationship();
87 if(!empty($this->vardef['relationship'])){
88 $rel->retrieve_by_name($this->vardef['relationship']);
90 if($rel->relationship_type == 'many-to-many'){
91 if($rel->lhs_module == $this->module_dir){
92 $this->related_module = $rel->rhs_module;
93 $module_dir = $rel->lhs_module;
94 }else if($rel->rhs_module == $this->module_dir){
95 $this->related_module = $rel->lhs_module;
96 $module_dir = $rel->rhs_module;
98 die("this field has no relationships mapped with this module");
100 if($module_dir != $this->module_dir){
101 die('These modules do not match : '. $this->module_dir . ' and ' . $module_dir);
103 if(isset($GLOBALS['beanList'][$this->module_dir])){
104 $class = $GLOBALS['beanList'][$this->module_dir];
105 if(file_exists($GLOBALS['beanFiles'][$class])){
106 $this->bean = loadBean($this->module_dir);
107 $this->bean->retrieve($_REQUEST['bean_id']);
108 if($this->bean->load_relationship($this->vardef['name'])){
109 $this->retrieve_values();
111 die('failed to load the relationship');
114 die('class file do not exist');
117 die($this->module_dir . ' is not in the beanList.');
121 die("the relationship is not a many-to-many");
125 * Retrieve the values from the DB using the get method of the link class
126 * Organize and save the value into the bean
128 function retrieve_values(){
129 if(empty($this->bean->{$this->value_name}) && isset($this->bean->{$this->name})){
131 $values = $this->bean->{$this->name}->get(true);
132 $role_field = $this->bean->{$this->name}->_get_link_table_role_field($this->bean->{$this->name}->_relationship_name);
133 foreach($values as $v){
135 foreach($v as $kk=>$vv){
136 if($kk == $role_field){
140 if($role == 'primary'){
141 $primary_id = $v['id'];
143 $secondary_ids[] = array('id'=>$v['id'], 'role'=>$role);
146 $this->bean->{$this->value_name} = array('role_field'=>$role_field);
147 if(isset($primary_id) || isset($secondary_ids)){
148 if(!isset($primary_id)){
149 $primary_id = $secondary_ids[0]['id'];
150 unset($secondary_ids[0]);
152 if(isset($GLOBALS['beanList'][ $this->related_module])){
153 $class = $GLOBALS['beanList'][$this->related_module];
154 if(file_exists($GLOBALS['beanFiles'][$class])){
155 $mod = loadBean($this->module_dir);
156 $mod->relDepth = $this->bean->relDepth + 1;
157 $mod->retrieve($primary_id);
158 if (isset($mod->name)) {
159 $this->bean->{$this->value_name}=array_merge($this->bean->{$this->value_name}, array('primary'=>array('id'=>$primary_id, 'name'=>$mod->name)));
161 $secondaries = array();
162 if(isset($secondary_ids)){
163 foreach($secondary_ids as $v){
164 if($mod->retrieve($v['id'])){
165 if (isset($mod->name)){
166 $secondaries['secondaries'][]=array('id'=>$v['id'], 'name'=>$mod->name);
171 $this->bean->{$this->value_name}=array_merge($this->bean->{$this->value_name}, $secondaries);
172 if(isset($field['additionalFields'])){
173 foreach($field['additionalFields'] as $field=>$to){
174 if(isset($mod->$field)){
175 $this->bean->$to = $mod->$field;
185 * redirect to the good process method.
188 if($this->action_type == 'editview'){
189 $this->process_editview();
190 }else if($this->action_type == 'detailview'){
191 $this->process_detailview();
194 function process_detailview(){
198 * Build the DisplayParams array
200 function process_editview(){
201 if(isset($this->bean->{$this->value_name}['secondaries'])){
202 $this->numFields=count($this->bean->{$this->value_name}['secondaries'])+1;
204 if(!isset($this->displayParams['readOnly'])) {
205 $this->displayParams['readOnly'] = '';
207 $this->displayParams['readOnly'] = $this->displayParams['readOnly'] == false ? '' : 'READONLY';
209 // If there is extra field to show.
210 if(isset($this->displayParams['collection_field_list'])){
212 require_once('include/SugarFields/SugarFieldHandler.php');
213 $sfh = new SugarFieldHandler();
214 $relatedObject = BeanFactory::getObjectName($this->related_module);
215 vardefmanager::loadVardef($this->related_module, $relatedObject);
216 foreach($this->displayParams['collection_field_list'] as $k=>$v){
218 $collection_field_vardef = $GLOBALS['dictionary'][$relatedObject]['fields'][$v['name']];
220 // For each extra field the params which are not displayParams will be consider as params to override the vardefs values.
221 foreach($v as $k_override=>$v_override){
222 if($k_override != 'displayParams'){
223 $collection_field_vardef[$k_override] = $v_override;
227 // If relate field : enable quick search by creating the sqs_object array.
228 if($collection_field_vardef['type'] == 'relate'){
229 require_once('include/TemplateHandler/TemplateHandler.php');
230 $tph = new TemplateHandler();
231 $javascript = $tph->createQuickSearchCode(array($collection_field_vardef['name']=>$collection_field_vardef), array($v), $this->form_name);
232 $javascript = str_replace('<script language="javascript">'."if(typeof sqs_objects == 'undefined'){var sqs_objects = new Array;}sqs_objects['{$collection_field_vardef['name']}']=","",$javascript);
233 $javascript = substr($javascript, 0, -10);//remove ";</script>"
234 $javascriptPHP = $this->json->decode($javascript);
235 foreach($javascriptPHP['populate_list'] as $kk=>$vv){
236 $javascriptPHP['populate_list'][$kk] .= "_" . $this->vardef['name'] . "_collection_extra_0";
238 foreach($javascriptPHP['required_list'] as $kk=>$vv){
239 $javascriptPHP['required_list'][$kk] .= "_" . $this->vardef['name'] . "_collection_extra_0";
241 foreach($javascriptPHP['field_list'] as $kk=>$vv){
243 $javascriptPHP['populate_list'][$kk];
246 $javascript = $this->json->encode($javascriptPHP);
247 $javascript = "<script language='javascript'>if(typeof sqs_objects == 'undefined'){var sqs_objects = new Array;}sqs_objects['{$collection_field_vardef['name']}_" . $this->vardef['name'] . "_collection_extra_0']=".$javascript.';</script>';
250 $collection_field_vardef['name'] .= "_" . $this->vardef['name'] . "_collection_extra_0";
251 if(isset($collection_field_vardef['id_name'])){
252 $collection_field_vardef['id_name'] .= "_" . $this->vardef['name'] . "_collection_extra_0";
254 if(isset($this->displayParams['allow_update']) && ($this->displayParams['allow_update'] === false || $this->displayParams['allow_update'] === 'false')){
255 $this->displayParams['allow_update']='false';
256 $v['displayParams']['field']['disabled']='';
258 $this->displayParams['allow_update']='true';
259 if(!isset($v['displayParams'])){
260 $v['displayParams']=array();
263 $viewtype='EditView';
264 $name = $collection_field_vardef['name'];
265 // Rearranging the array with name as key instaead of number. This is required for displaySmarty() to assign the good variable.
266 $this->displayParams['collection_field_list'][$name]['vardefName'] = $this->displayParams['collection_field_list'][$k]['name'];
267 $this->displayParams['collection_field_list'][$name]['name'] = $name;
268 if($collection_field_vardef['type'] == 'relate'){
269 $this->displayParams['collection_field_list'][$name]['id_name'] = $collection_field_vardef['id_name'];
270 $this->displayParams['collection_field_list'][$name]['module'] = $collection_field_vardef['module'];
272 $this->displayParams['collection_field_list'][$name]['label'] = "{sugar_translate label='{$collection_field_vardef['vname']}' module='{$this->related_module}'}";//translate($collection_field_vardef['vname'], $this->related_module);
273 $this->displayParams['collection_field_list'][$name]['field'] = $sfh->displaySmarty('displayParams.collection_field_list', $collection_field_vardef, $viewtype, $v['displayParams'], 1);
274 $this->displayParams['collection_field_list'][$name]['field'] .= '{literal}'.$javascript;
275 // Handle update_field array ONCHANGE
276 $this->displayParams['collection_field_list'][$name]['field'] .= <<<FRA
277 <script language='javascript'>
278 var oldonchange = '';
279 if(typeof(document.getElementById('{$collection_field_vardef['name']}').attributes.onchange) != 'undefined')
281 oldonchange=document.getElementById('{$collection_field_vardef['name']}').attributes.onchange.value;
284 $this->displayParams['collection_field_list'][$name]['field'] .= "eval(\"document.getElementById('{$collection_field_vardef['name']}').onchange = function onchange(event){collection['{$this->vardef['name']}'].update_fields.{$collection_field_vardef['name']}=true;";
285 if($collection_field_vardef['type'] == 'relate'){
286 // If relate add the ID field to the array
287 $this->displayParams['collection_field_list'][$name]['field'] .= "collection['{$this->vardef['name']}'].update_fields.{$collection_field_vardef['id_name']}=true;";
289 $this->displayParams['collection_field_list'][$name]['field'] .= "document.getElementById('update_fields_{$this->vardef['name']}_collection').value = YAHOO.lang.JSON.stringify(collection['{$this->vardef['name']}'].update_fields);\" + oldonchange + \"};\");</script>{/literal}";
290 //we need to get rid of the old value;
291 unset($this->displayParams['collection_field_list'][$k]);
294 if(!isset($this->displayParams['class'])) $this->displayParams['class']='';
295 if(isset($this->displayParams['allow_new']) && ($this->displayParams['allow_new'] === false || $this->displayParams['allow_new'] === 'false')){
296 $this->displayParams['allow_new']='false';
297 $this->displayParams['class']=str_replace('sqsNoAutofill','',$this->displayParams['class']);
299 $this->displayParams['allow_new']='true';
300 $this->displayParams['class'].=' sqsNoAutofill';
302 if(isset($this->displayParams['new_on_update']) && ($this->displayParams['new_on_update'] !== false || $this->displayParams['new_on_update'] !== 'false' || $this->displayParams['new_on_update'] !== 'FALSE' || $this->displayParams['new_on_update'] !== '0')){
303 $this->displayParams['new_on_update']='true';
305 $this->displayParams['new_on_update']='false';
310 * Init the template with the variables
313 foreach($this->extra_var as $k=>$v){
314 $this->ss->assign($k,$v);
316 if($this->action_type == 'editview'){
317 $this->ss->assign('quickSearchCode',$this->createQuickSearchCode());
318 $this->createPopupCode();// this code populate $this->displayParams with popupdata.
319 $this->tpl_path = $this->edit_tpl_path;
320 }else if($this->action_type == 'detailview'){
321 $this->tpl_path = $this->detail_tpl_path;
324 $this->ss->assign('displayParams',$this->displayParams);
325 $this->ss->assign('vardef',$this->vardef);
326 $this->ss->assign('module',$this->related_module);
327 $this->ss->assign('values',$this->bean->{$this->value_name});
328 $this->ss->assign('showSelectButton',$this->showSelectButton);
329 $this->ss->assign('hideShowHideButton',$this->hideShowHideButton);
330 $this->ss->assign('APP',$GLOBALS['app_strings']);
333 * Display the collection field after retrieving the cached row.
336 $cacheRowFile = $GLOBALS['sugar_config']['cache_dir'] . 'modules/'. $this->module_dir . '/collections/'. $this->name . '.tpl';
337 if(!$this->checkTemplate($cacheRowFile)){
338 $dir = dirname($cacheRowFile);
339 if(!file_exists($dir)) {
341 mkdir_recursive($dir, null, true);
343 $cacheRow = $this->ss->fetch($this->findTemplate('CollectionEditViewRow'));
344 file_put_contents($cacheRowFile, $cacheRow);
346 $this->ss->assign('cacheRowFile', $cacheRowFile);
347 return $this->ss->fetch($this->tpl_path);
350 * Check if the template is cached
353 function checkTemplate($cacheRowFile){
354 if(!empty($GLOBALS['sugar_config']['developerMode']) || !empty($_SESSION['developerMode'])){
357 return file_exists($cacheRowFile);
362 * Create the quickSearch code for the collection field.
363 * return the javascript code which define sqs_objects.
365 function createQuickSearchCode($returnAsJavascript = true){
366 $sqs_objects = array();
367 require_once('include/QuickSearchDefaults.php');
368 $qsd = new QuickSearchDefaults();
369 $qsd->setFormName($this->form_name);
370 for($i=0; $i<$this->numFields; $i++){
371 $name1 = "{$this->form_name}_{$this->name}_collection_{$i}";
372 if(!$this->skipModuleQuickSearch && preg_match('/(Campaigns|Teams|Users|Accounts)/si', $this->related_module, $matches)) {
373 if($matches[0] == 'Users'){
374 $sqs_objects[$name1] = $qsd->getQSUser();
375 } else if($matches[0] == 'Campaigns') {
376 $sqs_objects[$name1] = $qsd->getQSCampaigns();
378 } else if($matches[0] == 'Users'){
379 $sqs_objects[$name1] = $qsd->getQSUser();
381 } else if($matches[0] == 'Accounts') {
382 $nameKey = "{$this->name}_collection_{$i}";
383 $idKey = "id_{$this->name}_collection_{$i}";
385 //There are billingKey, shippingKey and additionalFields entries you can define in editviewdefs.php
386 //entry to allow quick search to autocomplete fields with a suffix value of the
387 //billing/shippingKey value (i.e. 'billingKey' => 'primary' in Contacts will populate
388 //primary_XXX fields with the Account's billing address values).
389 //addtionalFields are key/value pair of fields to fill from Accounts(key) to Contacts(value)
390 $billingKey = isset($this->displayParams['billingKey']) ? $this->displayParams['billingKey'] : null;
391 $shippingKey = isset($this->displayParams['shippingKey']) ? $this->displayParams['shippingKey'] : null;
392 $additionalFields = isset($this->displayParams['additionalFields']) ? $this->displayParams['additionalFields'] : null;
393 $sqs_objects[$name1] = $qsd->getQSAccount($nameKey, $idKey, $billingKey, $shippingKey, $additionalFields);
394 } else if($matches[0] == 'Contacts'){
395 $sqs_objects[$name1] = $qsd->getQSContact($name1, "id_".$name1);
399 $temp_array = array('field_list'=>array(),'populate_list'=>array());
400 foreach($sqs_objects[$name1]['field_list'] as $k=>$v){
401 if(!in_array($v, array('name','id'))){
402 $sqs_objects[$name1]['primary_field_list'][]=$v;
403 $sqs_objects[$name1]['primary_populate_list'][]=$sqs_objects[$name1]['populate_list'][$k];
405 $temp_array['field_list'][]=$v;
406 $temp_array['populate_list'][]=$sqs_objects[$name1]['populate_list'][$k];
409 $sqs_objects[$name1]['field_list'] = $temp_array['field_list'];
410 $sqs_objects[$name1]['populate_list'] = $temp_array['populate_list'];
411 if(isset($this->displayParams['collection_field_list'])){
412 foreach($this->displayParams['collection_field_list'] as $v){
413 $sqs_objects[$name1]['populate_list'][]= $v['vardefName']."_".$this->name."_collection_extra_".$i;
414 $sqs_objects[$name1]['field_list'][] = $v['vardefName'];
418 $sqs_objects[$name1] = $qsd->getQSParent($this->related_module);
419 $sqs_objects[$name1]['populate_list'] = array("{$this->vardef['name']}_collection_{$i}", "id_{$this->vardef['name']}_collection_{$i}");
420 $sqs_objects[$name1]['field_list'] = array('name', 'id');
421 if(isset($this->displayParams['collection_field_list'])){
422 foreach($this->displayParams['collection_field_list'] as $v){
423 $sqs_objects[$name1]['populate_list'][] = $v['vardefName']."_".$this->name."_collection_extra_".$i;
424 $sqs_objects[$name1]['field_list'][] = $v['vardefName'];
427 if(isset($this->displayParams['field_to_name_array'])){
428 foreach($this->displayParams['field_to_name_array'] as $k=>$v){
430 * "primary_populate_list" and "primary_field_list" are used when the field is selected as a primary.
431 * At this time the JS function changePrimary() will copy "primary_populate_list" and "primary_field_list"
432 * into "populate_list" and "field_list" and remove the values from all the others which are secondaries.
433 * "primary_populate_list" and "primary_field_list" contain the fields which has to be populated outside of
434 * the collection field. For example the "Address Information" are populated with the "billing address" of the
435 * selected account in a contact editview.
437 $sqs_objects[$name1]['primary_populate_list'][] = $v;
438 $sqs_objects[$name1]['primary_field_list'][] = $k;
440 }else if(isset($field['field_list']) && isset($field['populate_list'])){
441 $sqs_objects[$name1]['primary_populate_list'] = array_merge($sqs_objects[$name1]['populate_list'], $field['field_list']);
442 $sqs_objects[$name1]['primary_field_list'] = array_merge($sqs_objects[$name1]['field_list'], $field['populate_list']);
444 $sqs_objects[$name1]['primary_populate_list'] = array();
445 $sqs_objects[$name1]['primary_field_list'] = array();
450 $id = "{$this->form_name}_{$this->name}_collection_0";
452 if(!empty($sqs_objects) && count($sqs_objects) > 0) {
453 foreach($sqs_objects[$id]['field_list'] as $k=>$v){
454 $this->field_to_name_array[$v] = $sqs_objects[$id]['populate_list'][$k];
456 if($returnAsJavascript){
457 $quicksearch_js = '<script language="javascript">';
458 $quicksearch_js.= "if(typeof sqs_objects == 'undefined'){var sqs_objects = new Array;}";
460 foreach($sqs_objects as $sqsfield=>$sqsfieldArray){
461 $quicksearch_js .= "sqs_objects['$sqsfield']={$this->json->encode($sqsfieldArray)};";
464 return $quicksearch_js .= '</script>';
472 * Always call createQuickSearchCode() before createPopupCode() to define field_to_name_array
474 function createPopupCode(){
475 // TODO the 'select' button is not fully working. We should use the sqs_objects in open_popup instead of the parameter.
476 if(isset($this->field_to_name_array) && !empty($this->field_to_name_array)){
477 $call_back_function = 'set_return';
479 if(isset($this->displayParams['formName'])) {
480 $form = $this->displayParams['formName'];
481 } else if($this->action_type == 'editview'){
483 } else if($this->action_type == 'quickcreate'){
484 $form = "QuickCreate_{$this->module_dir}";
487 if(isset($this->displayParams['call_back_function'])) {
488 $call_back_function = $this->displayParams['call_back_function'];
491 $popup_request_data= array(
492 'call_back_function' => $call_back_function,
493 'form_name' => $form,
494 'field_to_name_array' => $this->field_to_name_array,
497 //Make sure to replace {{ and }} with spacing in between because Smarty template parsing will treat {{ or }} specially
498 $this->displayParams['popupData'] = '{literal}'. str_replace(array('{{', '}}'), array('{ {', '} }'), $this->json->encode($popup_request_data)) . '{/literal}';
504 function findTemplate($view){
505 static $tplCache = array();
507 if ( isset($tplCache[$this->type][$view]) ) {
508 return $tplCache[$this->type][$view];
511 $lastClass = get_class($this);
512 $classList = array($this->type,str_replace('ViewSugarField','',$lastClass));
513 while ( $lastClass = get_parent_class($lastClass) ) {
514 $classList[] = str_replace('ViewSugarField','',$lastClass);
518 foreach ( $classList as $className ) {
519 global $current_language;
520 if(isset($current_language)) {
521 $tplName = 'include/SugarFields/Fields/'. $className .'/'. $current_language . '.' . $view .'.tpl';
522 if ( file_exists('custom/'.$tplName) ) {
523 $tplName = 'custom/'.$tplName;
526 if ( file_exists($tplName) ) {
530 $tplName = 'include/SugarFields/Fields/'. $className .'/'. $view .'.tpl';
531 if ( file_exists('custom/'.$tplName) ) {
532 $tplName = 'custom/'.$tplName;
535 if ( file_exists($tplName) ) {
540 $tplCache[$this->type][$view] = $tplName;