2 /*********************************************************************************
3 * SugarCRM is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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}');
72 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
74 if(empty($this->tpl_path)){
75 $this->tpl_path = $this->findTemplate('EditView');
77 return $this->fetch($this->tpl_path);
81 function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
82 $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex, true);
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
89 public function save(&$bean, $params, $field, $properties, $prefix = ''){
90 if(isset($_POST["primary_" . $field . "_collection"])){
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);
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;
112 if(isset($_POST["primary_" . $field . "_collection"])){
113 $primary = $_POST["primary_" . $field . "_collection"];
114 settype($primary, 'int');
115 $link_field[$primary]['primary']=true;
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]);
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){
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]);
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]);
142 if(!isset($link_field[$k]['id']) || empty($link_field[$k]['id'])){
143 // There is no ID so it is a new record
145 $obj->name=$link_field[$k]['name'];
147 // We duplicate an existing record because new_on_update is set
148 $obj->retrieve($link_field[$k]['id']);
150 $obj->name = $obj->name . '_DUP';
153 // id exist so retrieve the data
154 $obj->retrieve($link_field[$k]['id']);
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"]));
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]){
171 $obj->$extra_field_name=$v['extra_field'][$kk];
177 // Save the new or updated record
179 if(!$obj->ACLAccess('save')){
180 ACLController::displayNoAccess(true);
184 $link_field[$k]['id']=$obj->id;
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'));
198 $bean->$field->add($new_v['id'], array($role_field=>'NULL'));
201 $bean->$field->add($new_v['id'], array());
205 foreach($oldvalues as $old_v){
207 foreach($link_field as $new_v){
208 if($new_v['id'] == $old_v['id']){
213 $bean->$field->delete($bean->id, $old_v['id']);