Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[civicrm-core.git] / CRM / Custom / Form / MoveField.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
20/**
21 * This class is to build the form for Deleting Group
22 */
23class CRM_Custom_Form_MoveField extends CRM_Core_Form {
24
25 /**
fe482240 26 * The src group id.
6a488035
TO
27 *
28 * @var int
29 */
30 protected $_srcGID;
31
32 /**
fe482240 33 * The src field id.
6a488035
TO
34 *
35 * @var int
36 */
37 protected $_srcFID;
38
39 /**
fe482240 40 * The dst group id.
6a488035
TO
41 *
42 * @var int
43 */
44 protected $_dstGID;
45
46 /**
fe482240 47 * The dst field id.
6a488035
TO
48 *
49 * @var int
50 */
51 protected $_dstFID;
52
53 /**
fe482240 54 * The title of the field being moved.
6a488035
TO
55 *
56 * @var string
57 */
58 protected $_srcFieldLabel;
59
60 /**
fe482240 61 * Set up variables to build the form.
6a488035
TO
62 *
63 * @return void
b44e3f84 64 * @access protected
8ef12e64 65 */
00be9182 66 public function preProcess() {
6a488035
TO
67 $this->_srcFID = CRM_Utils_Request::retrieve('fid', 'Positive',
68 $this, TRUE
69 );
70
71 $this->_srcGID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
72 $this->_srcFID,
73 'custom_group_id'
74 );
75
76 $this->_srcFieldLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField',
77 $this->_srcFID,
78 'label'
79 );
80
81 CRM_Utils_System::setTitle(ts('Custom Field Move: %1',
be2fb01f 82 [1 => $this->_srcFieldLabel]
353ffa53 83 ));
6a488035
TO
84
85 $session = CRM_Core_Session::singleton();
86 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_srcGID}"));
87 }
88
89 /**
fe482240 90 * Build the form object.
6a488035 91 *
355ba699 92 * @return void
6a488035
TO
93 */
94 public function buildQuickForm() {
95
cd43c5e3 96 $customGroup = CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
6a488035
TO
97 unset($customGroup[$this->_srcGID]);
98 if (empty($customGroup)) {
99 CRM_Core_Error::statusBounce(ts('You need more than one custom group to move fields'));
100 }
101
be2fb01f 102 $customGroup = [
87a890cc 103 '' => ts('- select -'),
be2fb01f 104 ] + $customGroup;
6a488035
TO
105 $this->add('select',
106 'dst_group_id',
107 ts('Destination'),
108 $customGroup,
109 TRUE
110 );
111
be2fb01f 112 $this->addButtons([
518fa0ee
SL
113 [
114 'type' => 'next',
115 'name' => ts('Move Custom Field'),
116 'isDefault' => TRUE,
117 ],
118 [
119 'type' => 'cancel',
120 'name' => ts('Cancel'),
121 ],
122 ]);
6a488035 123
be2fb01f 124 $this->addFormRule(['CRM_Custom_Form_MoveField', 'formRule'], $this);
6a488035
TO
125 }
126
e0ef6999
EM
127 /**
128 * @param $fields
129 * @param $files
130 * @param $self
131 *
132 * @return array|bool
133 */
00be9182 134 public static function formRule($fields, $files, $self) {
6a488035 135 $self->_dstGID = $fields['dst_group_id'];
353ffa53 136 $tmp = CRM_Core_BAO_CustomField::_moveFieldValidate($self->_srcFID, $self->_dstGID);
be2fb01f 137 $errors = [];
6a488035
TO
138 if ($tmp['newGroupID']) {
139 $errors['dst_group_id'] = $tmp['newGroupID'];
140 }
141 return empty($errors) ? TRUE : $errors;
142 }
143
144 /**
fe482240 145 * Process the form when submitted.
6a488035
TO
146 *
147 * @return void
6a488035
TO
148 */
149 public function postProcess() {
150 CRM_Core_BAO_CustomField::moveField($this->_srcFID, $this->_dstGID);
151
152 $dstGroup = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup',
153 $this->_dstGID,
154 'title'
155 );
156 $srcUrl = CRM_Utils_System::url('civicrm/admin/custom/group/field', "reset=1&action=browse&gid={$this->_dstGID}");
157 CRM_Core_Session::setStatus(ts("%1 has been moved to the custom set <a href='%3'>%2</a>.",
be2fb01f 158 [
353ffa53
TO
159 1 => $this->_srcFieldLabel,
160 2 => $dstGroup,
21dfd5f5 161 3 => $srcUrl,
be2fb01f 162 ]), '', 'success');
6a488035 163 }
96025800 164
6a488035 165}