]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DynamicFields/templates/Fields/TemplateRelatedTextField.php
Release 6.5.14
[Github/sugarcrm.git] / modules / DynamicFields / templates / Fields / TemplateRelatedTextField.php
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-2013 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  
39 require_once('modules/DynamicFields/templates/Fields/TemplateField.php');
40 require_once 'modules/ModuleBuilder/parsers/parser.label.php';
41 require_once 'modules/ModuleBuilder/MB/ModuleBuilder.php';
42
43 class TemplateRelatedTextField extends TemplateText{
44     var $type = 'relate';
45     //ext1 is the name field
46     //ext2 is the related module
47     
48     function get_html_edit(){
49         $this->prepare();
50         $name = $this->name .'_name';
51         $value_name = strtoupper('{'.$name.'}');
52         $id = $this->name ;
53         $value_id = strtoupper('{'.$id .'}');
54         return "<input type='text' name='$name' id='$name' size='".$this->size."' readonly value='$value_name'><input type='button' onclick='open_popup(\"{". strtoupper($this->name). "_MODULE}\", 600, 400,\" \", true, false, {ENCODED_". strtoupper($this->name). "_POPUP_REQUEST_DATA})' type='button'  class='button' value='{APP.LBL_SELECT_BUTTON_LABEL}' ><input type='hidden' name='$id' value='$value_id'>";
55     }
56
57     function get_html_detail(){
58         $name = $this->name .'_name';
59         $value_name = strtoupper('{'.$name.'}');
60         $id = $this->name ;
61         $value_id = strtoupper('{'.$id .'}');
62         
63         return "<a href='index.php?module=$this->ext2&action=DetailView&record={$value_id}'>{$value_name}</a>" ;    
64     }
65     
66     function get_html_list(){
67         if(isset($this->bean)){
68             $name = $this->bean->object_name . '.'. $this->ext1;
69         }else{
70             $name = $this->ext1;    
71         }
72         return '{'. strtoupper($name) . '}';    
73     }
74
75     function get_html_search(){
76         $searchable=array();
77         $def = $this->bean->field_name_map[$this->name];
78         if(!empty($def['id_name']) && in_array($def['id_name'], $searchable)){
79             $name = $def['id_name'];
80             return "<select size='3' name='{$name}[]' tabindex='1' multiple='multiple'>{".strtoupper($name). "_FILTER}</select>";   
81         }
82         //return 'NOT AVAILABLE';
83         return $this->get_html_edit();
84     }   
85
86     function get_xtpl_search(){
87         $searchable=array();
88         $def = $this->bean->field_name_map[$this->name];
89         $returnXTPL = array();
90         if(!empty($def['id_name']) && in_array($def['id_name'], $searchable)){
91             $name = $def['id_name'];
92             $team_list = '';
93             foreach(get_team_array() as $id=>$team){
94                 $selected = '';
95                 
96                 if(!empty($_REQUEST[$name]) && is_array($_REQUEST[$name]) && in_array($id, $_REQUEST[$name])){
97                     $selected = 'selected';
98                 }
99                 $team_list .= "<option  $selected value='$id'>$team</option>";
100             }
101             $returnXTPL[strtoupper($name). '_FILTER'] = $team_list;
102         } else {
103             $id = $this->name;
104             $name = $this->name .'_name';
105             $module = $this->ext2;
106             $popup_request_data = array(
107                                         'call_back_function' => 'set_return',
108                                         'form_name' => 'search_form',
109                                         'field_to_name_array' => array(
110                                         'id' => $this->name,
111                                         $this->ext1 => $name,
112                                     ),
113             );
114         
115             $json = getJSONobj();
116             $encoded_popup_request_data = $json->encode($popup_request_data);
117             $returnXTPL['ENCODED_'.strtoupper($id).'_POPUP_REQUEST_DATA'] = $encoded_popup_request_data;
118             $returnXTPL[strtoupper($id).'_MODULE'] = $module;
119
120             if(isset( $_REQUEST[$name])){
121                $returnXTPL[strtoupper($name)] =  $_REQUEST[$name];
122             }
123             if(isset( $_REQUEST[$id])){
124                $returnXTPL[strtoupper($id)] =  $_REQUEST[$id];
125             }
126         }
127         return $returnXTPL; 
128     }
129
130
131     function get_xtpl_edit(){
132     global $beanList;
133         
134         $name = $this->name .'_name';
135         $id = $this->name;
136         $module = $this->ext2;
137         $returnXTPL = array();
138         $popup_request_data = array(
139             'call_back_function' => 'set_return',
140             'form_name' => 'EditView',
141             'field_to_name_array' => array(
142             'id' => $this->name,
143             $this->ext1 => $name,
144         ),
145         );
146         
147         //$GLOBALS['log']->fatal($this->bean);
148       
149         $json = getJSONobj();
150         $encoded_contact_popup_request_data = $json->encode($popup_request_data);
151         $returnXTPL['ENCODED_'.strtoupper($id).'_POPUP_REQUEST_DATA'] = $encoded_contact_popup_request_data;
152         $returnXTPL[strtoupper($id).'_MODULE'] = $module;
153
154         if(isset($beanList[$module]) && isset($this->bean->$id)){
155             if(!isset($this->bean->$name)){
156                 $mod_field = $this->ext1;
157                 global $beanFiles;
158                 
159                 $class = $beanList[$module];
160             
161                 require_once($beanFiles[$class]);
162                 $mod = new $class();
163                 $mod->retrieve($this->bean->$id);
164                 if(isset($mod->$mod_field)){
165                     $this->bean->$name = $mod->$mod_field;  
166                 }   
167             }
168             
169             
170             $returnXTPL[strtoupper($id)] = $this->bean->$id;
171         }
172         if(isset($this->bean->$name)){
173             $returnXTPL[strtoupper($name)] = $this->bean->$name;
174         }
175         if(isset($this->bean->$id)) {
176             $returnXTPL[strtoupper($id)] = $this->bean->$id;
177         }
178         
179         
180         return $returnXTPL; 
181     }
182     
183     function get_xtpl_detail(){
184         return $this->get_xtpl_edit();
185     }
186     
187     function get_related_info(){
188             
189     }
190     
191      function get_field_def(){
192         $def = parent::get_field_def();
193         $def['id_name'] = $this->ext3;
194         $def['ext2'] = $this->ext2;
195         $def['module'] = $def['ext2'];
196         //Special case for documents, which use a document_name rather than name
197         if ($def['module'] == "Documents") {
198                 $def['rname'] = 'document_name';
199         } else {
200                 $def['rname'] = 'name';
201         }
202         $def['quicksearch'] = 'enabled';
203         $def['studio'] = 'visible';
204         $def['source'] = 'non-db';
205         return $def;    
206     }
207     
208     /**
209      * Delete field
210      *
211      * @param DynamicField $df
212      */
213     public function delete($df)
214     {
215         if ($df instanceof DynamicField) {
216             $fieldId = $df->getFieldWidget($df->module, $this->id_name);
217         } elseif ($df instanceof MBModule) {
218             $fieldId = $df->getField($this->id_name);
219         } else {
220             $GLOBALS['log']->fatal('Unsupported DynamicField type');
221         }
222
223         $this->deleteIdLabel($fieldId, $df);
224         $fieldId->delete($df);
225         parent::delete($df);
226     }
227         
228     /**
229      * Delete label of id field
230      * @param TemplateField $fieldId
231      * @param $df
232      */
233     protected function deleteIdLabel(TemplateField $fieldId, $df)
234     {
235         if ($df instanceof DynamicField) {
236             require_once 'modules/ModuleBuilder/parsers/parser.label.php';
237             foreach (array_keys($GLOBALS['sugar_config']['languages']) AS $language) {
238                 foreach (ModuleBuilder::getModuleAliases($df->module) AS $module) {
239                     $mod_strings = return_module_language($language, $module);
240                     if (isset($mod_strings[$fieldId->vname])) {
241                         ParserLabel::removeLabel($language, $fieldId->vname, $mod_strings[$fieldId->vname], $module);
242                     }
243                 }
244             }
245     
246         } elseif ($df instanceof MBModule) {
247             foreach (array_keys($GLOBALS['sugar_config']['languages']) AS $language) {
248                 $df->deleteLabel($language, $fieldId->vname);
249                 $df->save();
250             }
251         }
252     }
253
254     function save($df){
255         // create the new ID field associated with this relate field - this will hold the id of the related record
256         // this field must have a unique name as the name is used when constructing quicksearches and when saving the field
257         //Check if we have not saved this field so we don't create two ID fields.
258         //Users should not be able to switch the module after having saved it once.
259         if (!$df->fieldExists($this->name)) {
260                 $id = new TemplateId();
261                 $id->len = 36;
262             $id->label = strtoupper("LBL_{$this->name}_".BeanFactory::getBeanName($this->ext2)."_ID");
263             $id->vname = $id->label;
264             $this->saveIdLabel($id->label, $df);
265
266                 $count = 0;
267                 $basename = strtolower(get_singular_bean_name($this->ext2)).'_id' ;
268                 $idName = $basename.'_c' ;
269                 
270                 while ( $df->fieldExists($idName, 'id') )
271                 {
272                     $idName = $basename.++$count.'_c' ;
273                 }
274                 $id->name = $idName ;
275                         $id->reportable = false;                                
276                 $id->save($df);
277                 
278                 // record the id field's name, and save
279                 $this->ext3 = $id->name;
280             $this->id_name = $id->name;
281         }
282         
283         parent::save($df);
284     }
285
286     /**
287      * Save label for id field
288      *
289      * @param string $idLabelName
290      * @param DynamicField $df
291      */
292     protected function saveIdLabel($idLabelName, $df)
293     {
294         if ($df instanceof DynamicField) {
295             $module = $df->module;
296         } elseif ($df instanceof MBModule) {
297             $module = $df->name;
298         }else{
299             $GLOBALS['log']->fatal('Unsupported DynamicField type');
300         }
301         $viewPackage = isset($df->package)?$df->package:null;
302
303         $idLabelValue = string_format(
304             $GLOBALS['mod_strings']['LBL_RELATED_FIELD_ID_NAME_LABEL'],
305             array($this->label_value, $GLOBALS['app_list_strings']['moduleListSingular'][$this->ext2])
306         );
307
308         $idFieldLabelArr = array(
309             "label_{$idLabelName}" => $idLabelValue
310         );
311
312         foreach(ModuleBuilder::getModuleAliases($module) AS  $moduleName) {
313             if ($df instanceof DynamicField) {
314                 $parser = new ParserLabel($moduleName, $viewPackage);
315                 $parser->handleSave($idFieldLabelArr, $GLOBALS['current_language']);
316             } elseif ($df instanceof MBModule) {
317                 $df->setLabel($GLOBALS ['current_language'], $idLabelName, $idLabelValue);
318                 $df->save();
319             }
320         }
321
322     }
323     
324     function get_db_add_alter_table($table){
325         return "";
326     }
327     
328     function get_db_delete_alter_table($table) {
329         return "";
330     }
331     
332     function get_db_modify_alter_table($table){
333         return "";
334     }
335     
336 }
337
338
339 ?>