Merge pull request #15834 from francescbassas/patch-16
[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 elseif ($action == CRM_Core_Action::COPY) {
45 $this->_pages['CRM_Contact_Form_Search_Custom'] = NULL;
46 list($task, $result) = $this->taskName($controller, 'Custom');
47 }
48 else {
49 $this->_pages['CRM_Contact_Form_Search_Basic'] = NULL;
50 list($task, $result) = $this->taskName($controller, 'Basic');
51 }
52 $this->_task = $task;
53 if (is_array($task)) {
54 foreach ($task as $t) {
55 $this->_pages[$t] = NULL;
56 }
57 }
58 else {
59 $this->_pages[$task] = NULL;
60 }
61
62 if ($result) {
63 $this->_pages['CRM_Contact_Form_Task_Result'] = NULL;
64 }
65
66 $this->addSequentialPages($this->_pages, $action);
67 }
68
69 /**
70 * Determine the form name based on the action. This allows us
71 * to avoid using conditional state machine, much more efficient
72 * and simpler
73 *
74 * @param CRM_Core_Controller $controller
75 * The controller object.
76 *
77 * @param string $formName
78 *
79 * @return array
80 * the name of the form that will handle the task
81 */
82 public function taskName($controller, $formName = 'Search') {
83 // total hack, check POST vars and then session to determine stuff
84 $value = CRM_Utils_Array::value('task', $_POST);
85 if (!isset($value)) {
86 $value = $this->_controller->get('task');
87 }
88 $this->_controller->set('task', $value);
89
90 $componentMode = $this->_controller->get('component_mode');
91 $modeValue = CRM_Contact_Form_Search::getModeValue($componentMode);
92 $taskClassName = $modeValue['taskClassName'];
93 return $taskClassName::getTask($value);
94 }
95
96 /**
97 * Return the form name of the task.
98 *
99 * @return string
100 */
101 public function getTaskFormName() {
102 if (is_array($this->_task)) {
103 // return first page
104 return CRM_Utils_String::getClassName($this->_task[0]);
105 }
106 else {
107 return CRM_Utils_String::getClassName($this->_task);
108 }
109 }
110
111 /**
112 * Since this is a state machine for search and we want to come back to the same state
113 * we dont want to issue a reset of the state session when we are done processing a task
114 */
115 public function shouldReset() {
116 return FALSE;
117 }
118
119 }