]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Collection/SugarFieldCollection.php
Release 6.1.4
[Github/sugarcrm.git] / include / SugarFields / Fields / Collection / SugarFieldCollection.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 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 = 'include/SugarFields/Fields/Collection/DetailView.tpl';
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         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
73             if(!$searchView) {
74                 if(empty($this->tpl_path)){
75                         $this->tpl_path = 'include/SugarFields/Fields/Collection/EditView.tpl';
76                 }
77                 return $this->fetch($this->tpl_path);
78             }
79     }
80
81         function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
82                 $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, true);
83     }
84      /**
85      * This should be called when the bean is saved. The bean itself will be passed by reference
86      * @param SugarBean bean - the bean performing the save
87      * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
88      */
89         public function save(&$bean, $params, $field, $properties, $prefix = ''){
90         if(isset($_POST["primary_" . $field . "_collection"])){
91             $save = false;
92             $value_name = $field . "_values";
93             $link_field = array();
94             // populate $link_field from POST
95             foreach($_POST as $name=>$value){
96                 if(strpos($name, $field . "_collection_") !== false){
97                     $num = substr($name, -1);
98                     if(is_numeric($num)){
99                         settype($num, 'int');
100                         if(strpos($name, $field . "_collection_extra_") !== false){
101                             $extra_field = substr($name, $field . "_collection_extra_" . $num);
102                             $link_field[$num]['extra_field'][$extra_field]=$value;
103                         }else if ($name == $field . "_collection_" . $num){
104                             $link_field[$num]['name']=$value;
105                         }else if ($name == "id_" . $field . "_collection_" . $num){
106                             $link_field[$num]['id']=$value;
107                         }
108                     }
109                 }
110             }
111             // Set Primary
112             if(isset($_POST["primary_" . $field . "_collection"])){
113                 $primary = $_POST["primary_" . $field . "_collection"];
114                 settype($primary, 'int');
115                 $link_field[$primary]['primary']=true;
116             }
117             // Create or update record and take care of the extra_field
118             require('include/modules.php');
119             require_once('data/Link.php');
120                 $class = load_link_class($bean->field_defs[$field]);
121                 
122             $link_obj = new $class($bean->field_defs[$field]['relationship'], $bean, $bean->field_defs[$field]);
123             $module = $link_obj->getRelatedModuleName();
124             $beanName = $beanList[$module];
125             require_once($beanFiles[$beanName]);
126             foreach($link_field as $k=>$v){
127                 $save = false;
128                 $update_fields = array();
129                 $obj = new $beanName();
130                 if(!isset($link_field[$k]['name']) || empty($link_field[$k]['name'])){
131                     // There is no name so it is an empty record -> ignore it!
132                     unset($link_field[$k]);
133                     break;
134                 }
135                 if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id']) || (isset($_POST[$field . "_new_on_update"]) && $_POST[$field . "_new_on_update"] === 'true')){
136                     // Create a new record
137                     if(isset($_POST[$field . "_allow_new"]) && ($_POST[$field . "_allow_new"] === 'false' || $_POST[$field . "_allow_new"] === false)){
138                         // Not allow to create a new record so remove from $link_field
139                         unset($link_field[$k]);
140                         break;
141                     }
142                     if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id'])){
143                         // There is no ID so it is a new record
144                         $save = true;
145                         $obj->name=$link_field[$k]['name'];
146                     }else{
147                         // We duplicate an existing record because new_on_update is set
148                         $obj->retrieve($link_field[$k]['id']);
149                         $obj->id='';
150                         $obj->name = $obj->name . '_DUP';
151                     }
152                 }else{
153                     // id exist so retrieve the data
154                     $obj->retrieve($link_field[$k]['id']);
155                 }
156                 // Update the extra field for the new or the existing record
157                 if(isset($v['extra_field']) && is_array($v['extra_field'])){
158                     // Retrieve the changed fields
159                     if(isset($_POST["update_fields_{$field}_collection"]) && !empty($_POST["update_fields_{$field}_collection"])){
160                         $JSON = getJSONobj();
161                         $update_fields = $JSON->decode(html_entity_decode($_POST["update_fields_{$field}_collection"]));
162                     }
163                     // Update the changed fields
164                     foreach($update_fields as $kk=>$vv){
165                         if(!isset($_POST[$field . "_allow_update"]) || ($_POST[$field . "_allow_update"] !== 'false' && $_POST[$field . "_allow_update"] !== false)){
166                             //allow to update the extra_field in the record
167                             if(isset($v['extra_field'][$kk]) && $vv == true){
168                                 $extra_field_name = str_replace("_".$field."_collection_extra_".$k,"",$kk);
169                                 if($obj->$extra_field_name != $v['extra_field'][$kk]){
170                                     $save = true;
171                                     $obj->$extra_field_name=$v['extra_field'][$kk];
172                                 }
173                             }
174                         }
175                     }
176                 }
177                 // Save the new or updated record
178                 if($save){
179                     if(!$obj->ACLAccess('save')){
180                         ACLController::displayNoAccess(true);
181                         sugar_cleanup(true);
182                     }
183                     $obj->save();
184                     $link_field[$k]['id']=$obj->id;
185                 }
186             }
187             // Save new relationship or delete deleted relationship
188             if(!empty($link_field)){
189                 if($bean->load_relationship($field)){
190                     $oldvalues = $bean->$field->get(true);
191                     $role_field = $bean->$field->_get_link_table_role_field($bean->$field->_relationship_name);
192                     foreach($link_field as $new_v){
193                         if(!empty($new_v['id'])){
194                             if(!empty($role_field)){
195                                 if(isset($new_v['primary']) && $new_v['primary']){
196                                     $bean->$field->add($new_v['id'], array($role_field=>'primary'));
197                                 }else{
198                                     $bean->$field->add($new_v['id'], array($role_field=>'NULL'));
199                                 }
200                             }else{
201                                 $bean->$field->add($new_v['id'], array());
202                             }
203                         }
204                     }
205                     foreach($oldvalues as $old_v){
206                         $match = false;
207                         foreach($link_field as $new_v){
208                             if($new_v['id'] == $old_v['id']){
209                                 $match = true;
210                             }
211                         }
212                         if(!$match){
213                             $bean->$field->delete($bean->id, $old_v['id']);
214                         }
215                     }
216                 }
217             }
218         }
219     }
220
221 }
222 ?>