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