Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
CControllerBase.php
1 <?php
2 
3 /**
4  * CControllerBase.php - Implementation file for AJAX Backend Controller Base class
5  *
6  * @package SellerDeck Extensions
7  *
8  * @author Péter Erdődi
9  * @copyright © SellerDeck Ltd 2015. All rights reserved.
10  */
11 
12 namespace SDExtension;
13 
15  {
16 
17  /**
18  * @var string $m_sAction Action
19  */
20  protected $m_sAction = "";
21 
22  /**
23  * @var array $m_aResponse Response
24  */
25  protected $m_aResponse = [];
26 
27  /**
28  * @var array $m_nOutputFormat Output Format
29  */
30  protected $m_nOutputFormat = FORMAT_JSON;
31 
32  public function __call($sName, $aArguments)
33  {
34  echo $sName;
35  return [
36  'status' => 'success',
37  ];
38  }
39 
40  /**
41  * __construct - Run process
42  *
43  * @access public
44  * @param $aInput array Input data
45  * @return bool Success
46  */
47  public function __construct()
48  {
49 
50  $this->m_sAction = filter_input(INPUT_GET, CONTROLLER_PARAMETER_ACTION);
51  if (null != $this->m_sAction)
52  {
53  $this->m_sAction = ucfirst($this->m_sAction);
54  }
55  else
56  {
57  $this->m_sAction = CONTROLLER_DEFAULT_ACTION;
58  }
59  $this->m_nOutputFormat = filter_input(INPUT_GET, CONTROLLER_PARAMETER_FORMAT);
60  if (null != $this->m_nOutputFormat)
61  {
62  $this->m_nOutputFormat = strtolower($this->m_nOutputFormat);
63  }
64  else
65  {
66  $this->m_nOutputFormat = FORMAT_JSON;
67  }
68  }
69 
70  /**
71  * Run - Run process
72  *
73  * @access public
74  * @return bool Success
75  */
76  public function Run()
77  {
78  if (FORMAT_STREAM == $this->m_nOutputFormat)
79  {
80  sse_header();
81  }
82  $this->m_aResponse = $this->{"Action" . $this->m_sAction}();
83  return $this;
84  }
85 
86  /**
87  * SetOutputFormat - Set Output Format
88  *
89  * @access public
90  * @return bool Success
91  */
92  public function SetOutputFormat($nOutputFormat = FORMAT_JSON)
93  {
94  $this->m_nOutputFormat = $nOutputFormat;
95  }
96 
97  /**
98  * GetStandardResponse - Get Standard Response
99  *
100  * @access public
101  * @param $sAction string Action name
102  * @param $sStatus string Status
103  * @param $aData array Data
104  * @param $sView string View
105  * @return array Response data
106  */
107  public function GetStandardResponse($sAction = '', $sStatus = STATUS_SUCCESS, $aData = [], $sView = '')
108  {
109  return [
110  'action' => $sAction,
111  'status' => $sStatus,
112  'data' => $aData,
113  'view' => $sView,
114  ];
115  }
116 
117  /**
118  * GetOutput - Get Output
119  *
120  * @access public
121  * @return bool Success
122  */
123  public function GetOutput($nOutputFormat = null)
124  {
125  if (null == $nOutputFormat)
126  {
127  $nOutputFormat = $this->m_nOutputFormat;
128  }
129  switch ($nOutputFormat)
130  {
131  case FORMAT_ARRAY:
132  return $this->m_aResponse;
133  case FORMAT_TEXT:
134  header('Content-Type: text/plain');
135  return print_r($this->m_aResponse, true);
136  case FORMAT_STREAM:
137  return "data: " . json_encode($this->m_aResponse);
138  case FORMAT_PHP:
139  header('Content-Type: text/plain');
140  return var_export($this->m_aResponse, true);
141  case FORMAT_HTML:
142  header('Content-Type: text/html');
143  return arr_get($this->m_aResponse, 'view', '');
144 // case FORMAT_YAML:
145 // header('Content-Type: application/xml');
146 // return Array2XML($this->m_aResponse);
147 // case FORMAT_XML:
148 // header('Content-Type: application/xml');
149 // return Array2XML($this->m_aResponse);
150  case FORMAT_JSON:
151  default:
152  header('Content-Type: application/json');
153  return json_encode($this->m_aResponse);
154  }
155  return $this->m_aResponse;
156  }
157 
158  }
SetOutputFormat($nOutputFormat=FORMAT_JSON)
GetStandardResponse($sAction= '', $sStatus=STATUS_SUCCESS, $aData=[], $sView= '')
GetOutput($nOutputFormat=null)