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.
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.
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
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
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.
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.
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 ********************************************************************************/
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';
43 class TemplateRelatedTextField extends TemplateText{
45 //ext1 is the name field
46 //ext2 is the related module
48 function get_html_edit(){
50 $name = $this->name .'_name';
51 $value_name = strtoupper('{'.$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'>";
57 function get_html_detail(){
58 $name = $this->name .'_name';
59 $value_name = strtoupper('{'.$name.'}');
61 $value_id = strtoupper('{'.$id .'}');
63 return "<a href='index.php?module=$this->ext2&action=DetailView&record={$value_id}'>{$value_name}</a>" ;
66 function get_html_list(){
67 if(isset($this->bean)){
68 $name = $this->bean->object_name . '.'. $this->ext1;
72 return '{'. strtoupper($name) . '}';
75 function get_html_search(){
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>";
82 //return 'NOT AVAILABLE';
83 return $this->get_html_edit();
86 function get_xtpl_search(){
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'];
93 foreach(get_team_array() as $id=>$team){
96 if(!empty($_REQUEST[$name]) && is_array($_REQUEST[$name]) && in_array($id, $_REQUEST[$name])){
97 $selected = 'selected';
99 $team_list .= "<option $selected value='$id'>$team</option>";
101 $returnXTPL[strtoupper($name). '_FILTER'] = $team_list;
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(
111 $this->ext1 => $name,
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;
120 if(isset( $_REQUEST[$name])){
121 $returnXTPL[strtoupper($name)] = $_REQUEST[$name];
123 if(isset( $_REQUEST[$id])){
124 $returnXTPL[strtoupper($id)] = $_REQUEST[$id];
131 function get_xtpl_edit(){
134 $name = $this->name .'_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(
143 $this->ext1 => $name,
147 //$GLOBALS['log']->fatal($this->bean);
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;
154 if(isset($beanList[$module]) && isset($this->bean->$id)){
155 if(!isset($this->bean->$name)){
156 $mod_field = $this->ext1;
159 $class = $beanList[$module];
161 require_once($beanFiles[$class]);
163 $mod->retrieve($this->bean->$id);
164 if(isset($mod->$mod_field)){
165 $this->bean->$name = $mod->$mod_field;
170 $returnXTPL[strtoupper($id)] = $this->bean->$id;
172 if(isset($this->bean->$name)){
173 $returnXTPL[strtoupper($name)] = $this->bean->$name;
175 if(isset($this->bean->$id)) {
176 $returnXTPL[strtoupper($id)] = $this->bean->$id;
183 function get_xtpl_detail(){
184 return $this->get_xtpl_edit();
187 function get_related_info(){
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';
200 $def['rname'] = 'name';
202 $def['quicksearch'] = 'enabled';
203 $def['studio'] = 'visible';
204 $def['source'] = 'non-db';
211 * @param DynamicField $df
213 public function delete($df)
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);
220 $GLOBALS['log']->fatal('Unsupported DynamicField type');
223 $this->deleteIdLabel($fieldId, $df);
224 $fieldId->delete($df);
229 * Delete label of id field
230 * @param TemplateField $fieldId
233 protected function deleteIdLabel(TemplateField $fieldId, $df)
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);
246 } elseif ($df instanceof MBModule) {
247 foreach (array_keys($GLOBALS['sugar_config']['languages']) AS $language) {
248 $df->deleteLabel($language, $fieldId->vname);
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();
262 $id->label = strtoupper("LBL_{$this->name}_".BeanFactory::getBeanName($this->ext2)."_ID");
263 $id->vname = $id->label;
264 $this->saveIdLabel($id->label, $df);
267 $basename = strtolower(get_singular_bean_name($this->ext2)).'_id' ;
268 $idName = $basename.'_c' ;
270 while ( $df->fieldExists($idName, 'id') )
272 $idName = $basename.++$count.'_c' ;
274 $id->name = $idName ;
275 $id->reportable = false;
278 // record the id field's name, and save
279 $this->ext3 = $id->name;
280 $this->id_name = $id->name;
287 * Save label for id field
289 * @param string $idLabelName
290 * @param DynamicField $df
292 protected function saveIdLabel($idLabelName, $df)
294 if ($df instanceof DynamicField) {
295 $module = $df->module;
296 } elseif ($df instanceof MBModule) {
299 $GLOBALS['log']->fatal('Unsupported DynamicField type');
301 $viewPackage = isset($df->package)?$df->package:null;
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])
308 $idFieldLabelArr = array(
309 "label_{$idLabelName}" => $idLabelValue
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);
324 function get_db_add_alter_table($table){
328 function get_db_delete_alter_table($table) {
332 function get_db_modify_alter_table($table){