12cf525f85e9dfd5297706d3734d654ca7310a04
[civicrm-core.git] / CRM / Contact / StateMachine / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_StateMachine_Search extends CRM_Core_StateMachine {
18
19 /**
20 * The task that the wizard is currently processing
21 *
22 * @var string
23 */
24 protected $_task;
25
26 /**
27 * Class constructor.
28 *
29 * @param object $controller
30 * @param \const|int $action
31 */
32 public function __construct($controller, $action = CRM_Core_Action::NONE) {
33 parent::__construct($controller, $action);
34
35 $this->_pages = [];
36 if ($action == CRM_Core_Action::ADVANCED) {
37 $this->_pages['CRM_Contact_Form_Search_Advanced'] = NULL;
38 list($task, $result) = $this->taskName($controller, 'Advanced');
39 }
40 elseif ($action == CRM_Core_Action::PROFILE) {
41 $this->_pages['CRM_Contact_Form_Search_Builder'] = NULL;
42 list($task, $result) = $this->taskName($controller, 'Builder');
43 }
44 // @todo - this 'should' be removable but it's getting to this controller, for now
45 elseif ($action == CRM_Core_Action::COPY) {
46 $this->_pages['CRM_Contact_Form_Search_Custom'] = NULL;
47 list($task, $result) = $this->taskName($controller, 'Custom');
48 }
49 else {
50 $this->_pages['CRM_Contact_Form_Search_Basic'] = NULL;
51 list($task, $result) = $this->taskName($controller, 'Basic');
52 }
53 $this->_task = $task;
54 if (is_array($task)) {
55 foreach ($task as $t) {
56 $this->_pages[$t] = NULL;
57 }
58 }
59 else {
60 $this->_pages[$task] = NULL;
61 }
62
63 if ($result) {
64 $this->_pages['CRM_Contact_Form_Task_Result'] = NULL;
65 }
66
67 $this->addSequentialPages($this->_pages, $action);
68 }
69
70 /**
71 * Determine the form name based on the action. This allows us
72 * to avoid using conditional state machine, much more efficient
73 * and simpler
74 *
75 * @param CRM_Core_Controller $controller
76 * The controller object.
77 *
78 * @param string $formName
79 *
80 * @return array
81 * the name of the form that will handle the task
82 */
83 public function taskName($controller, $formName = 'Search') {
84 // total hack, check POST vars and then session to determine stuff
85 $value = $_POST['task'] ?? NULL;
86 if (!isset($value)) {
87 $value = $this->_controller->get('task');
88 }
89 $this->_controller->set('task', $value);
90
91 $componentMode = $this->_controller->get('component_mode');
92 $modeValue = CRM_Contact_Form_Search::getModeValue($componentMode);
93 $taskClassName = $modeValue['taskClassName'];
94 return $taskClassName::getTask($value);
95 }
96
97 /**
98 * Return the form name of the task.
99 *
100 * @return string
101 */
102 public function getTaskFormName() {
103 if (is_array($this->_task)) {
104 // return first page
105 return CRM_Utils_String::getClassName($this->_task[0]);
106 }
107 else {
108 return CRM_Utils_String::getClassName($this->_task);
109 }
110 }
111
112 /**
113 * Since this is a state machine for search and we want to come back to the same state
114 * we dont want to issue a reset of the state session when we are done processing a task
115 */
116 public function shouldReset() {
117 return FALSE;
118 }
119
120 }