Update version in comments
[civicrm-core.git] / CRM / Campaign / Form / Survey / Questions.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for processing a survey
38 *
39 */
40class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey {
41
42 /**
c490a46a 43 * Set default values for the form. Note that in edit/view mode
6a488035
TO
44 * the default values are retrieved from the database
45 *
a6c01b45
CW
46 * @return array
47 * array of default values
6a488035 48 */
00be9182 49 public function setDefaultValues() {
6a488035
TO
50 $defaults = array();
51
52 $ufJoinParams = array(
53 'entity_table' => 'civicrm_survey',
54 'module' => 'CiviCampaign',
55 'entity_id' => $this->_surveyId,
56 );
57
79d7553f 58 list($defaults['contact_profile_id'], $second)
59 = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
60 $defaults['activity_profile_id'] = $second ? array_shift($second) : '';
61
62 return $defaults;
63 }
64
65 /**
fe482240 66 * Build the form object.
6a488035 67 *
6a488035 68 * @return void
6a488035
TO
69 */
70 public function buildQuickForm() {
71 $subTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $this->_surveyId, 'activity_type_id');
72 if (!CRM_Core_BAO_CustomGroup::autoCreateByActivityType($subTypeId)) {
73 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE); // everything
74 // FIXME: Displays weird "/\ Array" message; doesn't work with tabs
342936e2
DL
75 CRM_Core_Session::setStatus(
76 ts(
77 'There are no custom data sets for activity type "%1". To create one, <a href="%2" target="%3">click here</a>.',
78 array(
79 1 => $activityTypes[$subTypeId],
80 2 => CRM_Utils_System::url('civicrm/admin/custom/group', 'action=add&reset=1'),
21dfd5f5 81 3 => '_blank',
342936e2
DL
82 )
83 )
84 );
6a488035
TO
85 }
86
87 $allowCoreTypes = CRM_Campaign_BAO_Survey::surveyProfileTypes();
88 $allowSubTypes = array(
89 'ActivityType' => array($subTypeId),
90 );
91 $entities = array(
92 array('entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'),
93 array('entity_name' => 'activity_1', 'entity_type' => 'ActivityModel', 'entity_sub_type' => $subTypeId),
94 );
95 $this->addProfileSelector('contact_profile_id', ts('Contact Info'), $allowCoreTypes, $allowSubTypes, $entities);
96 $this->addProfileSelector('activity_profile_id', ts('Questions'), $allowCoreTypes, $allowSubTypes, $entities);
97 // Note: Because this is in a tab, we also preload the schema via CRM_Campaign_Form_Survey::preProcess
98
99 parent::buildQuickForm();
100 }
101
102
103 /**
fe482240 104 * Process the form.
6a488035 105 *
6a488035 106 * @return void
6a488035
TO
107 */
108 public function postProcess() {
109 // store the submitted values in an array
110 $params = $this->controller->exportValues($this->_name);
111
112 // also update the ProfileModule tables
113 $ufJoinParams = array(
114 'is_active' => 1,
353ffa53 115 'module' => 'CiviCampaign',
6a488035 116 'entity_table' => 'civicrm_survey',
353ffa53 117 'entity_id' => $this->_surveyId,
6a488035
TO
118 );
119
120 // first delete all past entries
121 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
122
123 $uf = array();
124 $wt = 2;
125 if (!empty($params['contact_profile_id'])) {
126 $uf[1] = $params['contact_profile_id'];
127 $wt = 1;
128 }
129 if (!empty($params['activity_profile_id'])) {
130 $uf[2] = $params['activity_profile_id'];
131 }
132
133 $uf = array_values($uf);
134 if (!empty($uf)) {
135 foreach ($uf as $weight => $ufGroupId) {
136 $ufJoinParams['weight'] = $weight + $wt;
137 $ufJoinParams['uf_group_id'] = $ufGroupId;
138 CRM_Core_BAO_UFJoin::create($ufJoinParams);
139 unset($ufJoinParams['id']);
140 }
141 }
142
143 parent::endPostProcess();
144 }
96025800 145
6a488035 146}