]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Collection/SugarFieldCollection.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Fields / Collection / SugarFieldCollection.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37 require_once('include/SugarFields/Fields/Base/SugarFieldBase.php');
38 class SugarFieldCollection extends SugarFieldBase {
39         var $tpl_path;
40         
41         function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
42                 $nolink = array('Users');
43                 if(in_array($vardef['module'], $nolink)){
44                         $displayParams['nolink']=true;
45                 }else{
46                         $displayParams['nolink']=false;
47                 }
48                 $json = getJSONobj();
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');
56         }
57         return $this->fetch($this->tpl_path);
58     }
59
60     function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, $searchView = false) {
61         if($searchView){
62                 $form_name = 'search_form';
63         }else{
64                 $form_name = 'EditView';
65         }
66         $json = getJSONobj();
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}');
72
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'];
80
81         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
82             if(!$searchView) {
83                 if(empty($this->tpl_path)){
84                         $this->tpl_path = $this->findTemplate('EditView');
85                 }
86                 return $this->fetch($this->tpl_path);
87             }
88     }
89
90         function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
91                 $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, true);
92     }
93      /**
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
97      */
98         public function save(&$bean, $params, $field, $properties, $prefix = ''){
99         if(isset($_POST["primary_" . $field . "_collection"])){
100             $save = false;
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;
116                         }
117                     }
118                 }
119             }
120             // Set Primary
121             if(isset($_POST["primary_" . $field . "_collection"])){
122                 $primary = $_POST["primary_" . $field . "_collection"];
123                 settype($primary, 'int');
124                 $link_field[$primary]['primary']=true;
125             }
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]);
130                 
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){
136                 $save = false;
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]);
142                     break;
143                 }
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]);
149                         break;
150                     }
151                     if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id'])){
152                         // There is no ID so it is a new record
153                         $save = true;
154                         $obj->name=$link_field[$k]['name'];
155                     }else{
156                         // We duplicate an existing record because new_on_update is set
157                         $obj->retrieve($link_field[$k]['id']);
158                         $obj->id='';
159                         $obj->name = $obj->name . '_DUP';
160                     }
161                 }else{
162                     // id exist so retrieve the data
163                     $obj->retrieve($link_field[$k]['id']);
164                 }
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"]));
171                     }
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]){
179                                     $save = true;
180                                     $obj->$extra_field_name=$v['extra_field'][$kk];
181                                 }
182                             }
183                         }
184                     }
185                 }
186                 // Save the new or updated record
187                 if($save){
188                     if(!$obj->ACLAccess('save')){
189                         ACLController::displayNoAccess(true);
190                         sugar_cleanup(true);
191                     }
192                     $obj->save();
193                     $link_field[$k]['id']=$obj->id;
194                 }
195             }
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'));
206                                 }else{
207                                     $bean->$field->add($new_v['id'], array($role_field=>'NULL'));
208                                 }
209                             }else{
210                                 $bean->$field->add($new_v['id'], array());
211                             }
212                         }
213                     }
214                     foreach($oldvalues as $old_v){
215                         $match = false;
216                         foreach($link_field as $new_v){
217                             if($new_v['id'] == $old_v['id']){
218                                 $match = true;
219                             }
220                         }
221                         if(!$match){
222                             $bean->$field->delete($bean->id, $old_v['id']);
223                         }
224                     }
225                 }
226             }
227         }
228     }
229
230 }
231 ?>