2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 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/Base/SugarFieldBase.php');
38 class SugarFieldCollection extends SugarFieldBase {
41 function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
42 $nolink = array('Users');
43 if(in_array($vardef['module'], $nolink)){
44 $displayParams['nolink']=true;
46 $displayParams['nolink']=false;
49 $displayParamsJSON = $json->encode($displayParams);
50 $vardefJSON = $json->encode($vardef);
51 $this->ss->assign('displayParamsJSON', '{literal}'.$displayParamsJSON.'{/literal}');
52 $this->ss->assign('vardefJSON', '{literal}'.$vardefJSON.'{/literal}');
53 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
54 if(empty($this->tpl_path)){
55 $this->tpl_path = $this->findTemplate('DetailView');
57 return $this->fetch($this->tpl_path);
60 function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, $searchView = false) {
62 $form_name = 'search_form';
64 $form_name = 'EditView';
67 $displayParamsJSON = $json->encode($displayParams);
68 $vardefJSON = $json->encode($vardef);
69 $this->ss->assign('required', !empty($vardef['required']));
70 $this->ss->assign('displayParamsJSON', '{literal}'.$displayParamsJSON.'{/literal}');
71 $this->ss->assign('vardefJSON', '{literal}'.$vardefJSON.'{/literal}');
73 $keys = $this->getAccessKey($vardef,'COLLECTION',$vardef['module']);
74 $displayParams['accessKeySelect'] = $keys['accessKeySelect'];
75 $displayParams['accessKeySelectLabel'] = $keys['accessKeySelectLabel'];
76 $displayParams['accessKeySelectTitle'] = $keys['accessKeySelectTitle'];
77 $displayParams['accessKeyClear'] = $keys['accessKeyClear'];
78 $displayParams['accessKeyClearLabel'] = $keys['accessKeyClearLabel'];
79 $displayParams['accessKeyClearTitle'] = $keys['accessKeyClearTitle'];
81 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
83 if(empty($this->tpl_path)){
84 $this->tpl_path = $this->findTemplate('EditView');
86 return $this->fetch($this->tpl_path);
90 function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
91 $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, true);
94 * This should be called when the bean is saved. The bean itself will be passed by reference
95 * @param SugarBean bean - the bean performing the save
96 * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
98 public function save(&$bean, $params, $field, $properties, $prefix = ''){
99 if(isset($_POST["primary_" . $field . "_collection"])){
101 $value_name = $field . "_values";
102 $link_field = array();
103 // populate $link_field from POST
104 foreach($_POST as $name=>$value){
105 if(strpos($name, $field . "_collection_") !== false){
106 $num = substr($name, -1);
107 if(is_numeric($num)){
108 settype($num, 'int');
109 if(strpos($name, $field . "_collection_extra_") !== false){
110 $extra_field = substr($name, $field . "_collection_extra_" . $num);
111 $link_field[$num]['extra_field'][$extra_field]=$value;
112 }else if ($name == $field . "_collection_" . $num){
113 $link_field[$num]['name']=$value;
114 }else if ($name == "id_" . $field . "_collection_" . $num){
115 $link_field[$num]['id']=$value;
121 if(isset($_POST["primary_" . $field . "_collection"])){
122 $primary = $_POST["primary_" . $field . "_collection"];
123 settype($primary, 'int');
124 $link_field[$primary]['primary']=true;
126 // Create or update record and take care of the extra_field
127 require('include/modules.php');
128 require_once('data/Link.php');
129 $class = load_link_class($bean->field_defs[$field]);
131 $link_obj = new $class($bean->field_defs[$field]['relationship'], $bean, $bean->field_defs[$field]);
132 $module = $link_obj->getRelatedModuleName();
133 $beanName = $beanList[$module];
134 require_once($beanFiles[$beanName]);
135 foreach($link_field as $k=>$v){
137 $update_fields = array();
138 $obj = new $beanName();
139 if(!isset($link_field[$k]['name']) || empty($link_field[$k]['name'])){
140 // There is no name so it is an empty record -> ignore it!
141 unset($link_field[$k]);
144 if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id']) || (isset($_POST[$field . "_new_on_update"]) && $_POST[$field . "_new_on_update"] === 'true')){
145 // Create a new record
146 if(isset($_POST[$field . "_allow_new"]) && ($_POST[$field . "_allow_new"] === 'false' || $_POST[$field . "_allow_new"] === false)){
147 // Not allow to create a new record so remove from $link_field
148 unset($link_field[$k]);
151 if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id'])){
152 // There is no ID so it is a new record
154 $obj->name=$link_field[$k]['name'];
156 // We duplicate an existing record because new_on_update is set
157 $obj->retrieve($link_field[$k]['id']);
159 $obj->name = $obj->name . '_DUP';
162 // id exist so retrieve the data
163 $obj->retrieve($link_field[$k]['id']);
165 // Update the extra field for the new or the existing record
166 if(isset($v['extra_field']) && is_array($v['extra_field'])){
167 // Retrieve the changed fields
168 if(isset($_POST["update_fields_{$field}_collection"]) && !empty($_POST["update_fields_{$field}_collection"])){
169 $JSON = getJSONobj();
170 $update_fields = $JSON->decode(html_entity_decode($_POST["update_fields_{$field}_collection"]));
172 // Update the changed fields
173 foreach($update_fields as $kk=>$vv){
174 if(!isset($_POST[$field . "_allow_update"]) || ($_POST[$field . "_allow_update"] !== 'false' && $_POST[$field . "_allow_update"] !== false)){
175 //allow to update the extra_field in the record
176 if(isset($v['extra_field'][$kk]) && $vv == true){
177 $extra_field_name = str_replace("_".$field."_collection_extra_".$k,"",$kk);
178 if($obj->$extra_field_name != $v['extra_field'][$kk]){
180 $obj->$extra_field_name=$v['extra_field'][$kk];
186 // Save the new or updated record
188 if(!$obj->ACLAccess('save')){
189 ACLController::displayNoAccess(true);
193 $link_field[$k]['id']=$obj->id;
196 // Save new relationship or delete deleted relationship
197 if(!empty($link_field)){
198 if($bean->load_relationship($field)){
199 $oldvalues = $bean->$field->get(true);
200 $role_field = $bean->$field->_get_link_table_role_field($bean->$field->_relationship_name);
201 foreach($link_field as $new_v){
202 if(!empty($new_v['id'])){
203 if(!empty($role_field)){
204 if(isset($new_v['primary']) && $new_v['primary']){
205 $bean->$field->add($new_v['id'], array($role_field=>'primary'));
207 $bean->$field->add($new_v['id'], array($role_field=>'NULL'));
210 $bean->$field->add($new_v['id'], array());
214 foreach($oldvalues as $old_v){
216 foreach($link_field as $new_v){
217 if($new_v['id'] == $old_v['id']){
222 $bean->$field->delete($bean->id, $old_v['id']);