Merge pull request #6360 from monishdeb/search-fixes-master
[civicrm-core.git] / CRM / Case / Form / CustomData.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
35 * This class generates form components for custom data
36 *
37 * It delegates the work to lower level subclasses and integrates the changes
38 * back in. It also uses a lot of functionality with the CRM API's, so any change
39 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
6a488035
TO
40 */
41class CRM_Case_Form_CustomData extends CRM_Core_Form {
42
43 /**
44 * The entity id, used when editing/creating custom data
45 *
46 * @var int
47 */
48 protected $_entityID;
49
50 /**
fe482240 51 * The custom data type.
6a488035
TO
52 *
53 * @var int
54 */
55 protected $_cdType;
56
57 /**
fe482240 58 * Entity sub type of the table id.
6a488035
TO
59 *
60 * @var string
6a488035
TO
61 */
62 protected $_subTypeID;
63
64 /**
100fef9d 65 * Pre processing work done here.
6a488035
TO
66 *
67 * gets session variables for table name, id of entity in table, type of entity and stores them.
6a488035 68 */
00be9182 69 public function preProcess() {
353ffa53
TO
70 $this->_groupID = CRM_Utils_Request::retrieve('groupID', 'Positive', $this, TRUE);
71 $this->_entityID = CRM_Utils_Request::retrieve('entityID', 'Positive', $this, TRUE);
6a488035
TO
72 $this->_subTypeID = CRM_Utils_Request::retrieve('subType', 'Positive', $this, TRUE);
73 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
74
75 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Case',
76 $this,
77 $this->_entityID,
78 $this->_groupID,
79 $this->_subTypeID
80 );
81 // simplified formatted groupTree
82 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $this);
e08129f7 83 // Array contains only one item
6a488035
TO
84 foreach ($groupTree as $groupValues) {
85 $this->_customTitle = $groupValues['title'];
e08129f7 86 CRM_Utils_System::setTitle(ts('Edit %1', array(1 => $groupValues['title'])));
6a488035
TO
87 }
88
89 $this->_defaults = array();
90 CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $this->_defaults);
91 $this->setDefaults($this->_defaults);
92
7b226831 93 CRM_Core_BAO_CustomGroup::buildQuickForm($this, $groupTree);
6a488035
TO
94
95 //need to assign custom data type and subtype to the template
96 $this->assign('entityID', $this->_entityID);
97 $this->assign('groupID', $this->_groupID);
98 $this->assign('subType', $this->_subTypeID);
99 $this->assign('contactID', $this->_contactID);
100 }
101
102 /**
fe482240 103 * Build the form object.
6a488035
TO
104 */
105 public function buildQuickForm() {
106 // make this form an upload since we dont know if the custom data injected dynamically
107 // is of type file etc
108 $this->addButtons(array(
109 array(
110 'type' => 'upload',
111 'name' => ts('Save'),
112 'isDefault' => TRUE,
113 ),
114 array(
115 'type' => 'cancel',
116 'name' => ts('Cancel'),
117 ),
118 )
119 );
120 }
121
122 /**
123 * Process the user submitted custom data values.
6a488035
TO
124 */
125 public function postProcess() {
126 $params = $this->controller->exportValues($this->_name);
6a488035
TO
127
128 $transaction = new CRM_Core_Transaction();
129
130 CRM_Core_BAO_CustomValueTable::postProcess($params,
6a488035
TO
131 'civicrm_case',
132 $this->_entityID,
133 'Case'
134 );
135
136 $session = CRM_Core_Session::singleton();
137 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view/case', "reset=1&id={$this->_entityID}&cid={$this->_contactID}&action=view"));
138
353ffa53 139 $session = CRM_Core_Session::singleton();
6a488035
TO
140 $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Change Custom Data', 'name');
141 $activityParams = array(
142 'activity_type_id' => $activityTypeID,
143 'source_contact_id' => $session->get('userID'),
144 'is_auto' => TRUE,
145 'subject' => $this->_customTitle . " : change data",
146 'status_id' => CRM_Core_OptionGroup::getValue('activity_status',
147 'Completed',
148 'name'
149 ),
150 'target_contact_id' => $this->_contactID,
151 'details' => json_encode($this->_defaults),
152 'activity_date_time' => date('YmdHis'),
153 );
154 $activity = CRM_Activity_BAO_Activity::create($activityParams);
155
156 $caseParams = array(
157 'activity_id' => $activity->id,
158 'case_id' => $this->_entityID,
159 );
160 CRM_Case_BAO_Case::processCaseActivity($caseParams);
161
162 $transaction->commit();
163 }
96025800 164
6a488035 165}