PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/shared_addons/modules/workflow/controllers/admin_transitions.php

https://gitlab.com/Riky_Lesmana/SugarSweetCakes
PHP | 313 lines | 186 code | 37 blank | 90 comment | 31 complexity | 5c4b78f737ffc588144789d5dabe55cd MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Workflow Module
  4. *
  5. * Manage workflow of Streams entry
  6. *
  7. */
  8. class Admin_transitions extends Admin_Controller
  9. {
  10. // This will set the active section tab
  11. protected $section = 'workflows';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. // Check permission
  16. if(! group_has_role('workflow', 'access_transitions_backend')){
  17. $this->session->set_flashdata('error', lang('cp:access_denied'));
  18. redirect('admin');
  19. }
  20. $this->lang->load('workflow');
  21. $this->load->driver('Streams');
  22. $this->load->library('Workflow');
  23. $this->load->model('groups/group_m');
  24. }
  25. /**
  26. * List all Transitions using Streams CP Driver
  27. *
  28. * We are using the Streams API to grab
  29. * data from the transitions database. It handles
  30. * pagination as well.
  31. *
  32. * @return void
  33. */
  34. public function index($workflow_id = 0)
  35. {
  36. // Check permission
  37. if(! group_has_role('workflow', 'view_all_transitions') AND ! group_has_role('workflow', 'view_own_transitions')){
  38. $this->session->set_flashdata('error', lang('cp:access_denied'));
  39. redirect('admin');
  40. }
  41. // Get workflow entry
  42. if(! $data['workflow_entry'] = $this->streams->entries->get_entry($workflow_id, 'workflows', 'workflow')){
  43. die();
  44. }
  45. // -------------------------------------
  46. // Get Statuses entries.
  47. // -------------------------------------
  48. $params = array();
  49. $params['stream'] = 'statuses';
  50. $params['namespace'] = 'workflow';
  51. $params['order_by'] = 'status_sort_order';
  52. $params['sort'] = 'asc'; //'asc', 'desc', 'random'
  53. $params['where'] = 'status_workflow = '.$workflow_id; //string or array
  54. $params['paginate'] = 'no';
  55. $data['statuses'] = $this->streams->entries->get_entries($params);
  56. // Build the page. See views/admin/index.php
  57. // for the view code.
  58. $this->template->title(lang('workflow:transitions:plural'));
  59. $this->template->build('admin/transitions_index', $data);
  60. }
  61. /**
  62. * Display one Transitions
  63. *
  64. * We are using the Streams API to grab
  65. * data from the transitions database.
  66. *
  67. * @return void
  68. */
  69. public function view($id = 0)
  70. {
  71. // Check permission
  72. if(! group_has_role('workflow', 'view_all_transitions') AND ! group_has_role('workflow', 'view_own_transitions')){
  73. $this->session->set_flashdata('error', lang('cp:access_denied'));
  74. redirect('admin');
  75. }
  76. // Get our entry. We are simply specifying
  77. // the stream/namespace.
  78. $data['transitions'] = $this->streams->entries->get_entry($id, 'transitions', 'workflow', true);
  79. // Check view all/own permission
  80. if(! group_has_role('workflow', 'view_all_transitions')){
  81. if($data['transitions']->created_by != $this->current_user->id){
  82. $this->session->set_flashdata('error', lang('cp:access_denied'));
  83. redirect('admin');
  84. }
  85. }
  86. // Get multiple relationship
  87. /*$field = $this->fields_m->get_field_by_slug('%field_slug%', 'workflow');
  88. $attributes = array(
  89. 'stream_slug' => 'transitions', // The stream of the related stream.
  90. 'row_id' => $id, // The ID of the current entry row.
  91. );
  92. $data['transitions']->%field_slug% = $this->type->types->multiple->plugin_override($field, $attributes);*/
  93. // Build the page. See views/admin/index.php
  94. // for the view code.
  95. $this->template->title(lang('workflow:transitions:view'));
  96. $this->template->build('admin/transitions_entry', $data);
  97. }
  98. /**
  99. * Create a new Transitions entry
  100. *
  101. * We are building entry form manually using the fields API
  102. * and displaying the output in a custom view file.
  103. *
  104. * @return void
  105. */
  106. public function create($status_from_id, $status_to_id)
  107. {
  108. // Check permission
  109. if(! group_has_role('workflow', 'create_transitions')){
  110. $this->session->set_flashdata('error', lang('cp:access_denied'));
  111. redirect('admin');
  112. }
  113. // Load everything we need
  114. $this->load->library('streams_core/Fields');
  115. if( ! $data['status_from_entry'] = $this->streams->entries->get_entry($status_from_id, 'statuses', 'workflow')){
  116. die();
  117. }
  118. if( ! $data['status_to_entry'] = $this->streams->entries->get_entry($status_to_id, 'statuses', 'workflow')){
  119. die();
  120. }
  121. if( ! $data['workflow_entry'] = $this->streams->entries->get_entry($data['status_from_entry']->status_workflow, 'workflows', 'workflow')){
  122. die();
  123. }
  124. if($_POST){
  125. $_POST['transition_triggers'] = serialize($this->input->post('transition_triggers'));
  126. }
  127. $stream = $this->streams->streams->get_stream('transitions', 'workflow');
  128. $namespace = 'workflow';
  129. $mode = 'new'; //'new', 'edit'
  130. $entry = null;
  131. $plugin = false;
  132. $recaptcha = false;
  133. $extra = array(
  134. 'return' => 'admin/workflow/transitions/index/'.$data['workflow_entry']->id,
  135. 'success_message' => lang('workflow:transitions:submit_success'),
  136. 'failure_message' => lang('workflow:transitions:submit_failure'),
  137. 'title' => 'lang:workflow:transitions:new',
  138. //'required' => '<span>*</span>',
  139. //'email_notification' => false,
  140. );
  141. $skips = array();
  142. $defaults = array();
  143. // Get our field form elements.
  144. $data['fields'] = $this->fields->build_form($stream, $mode, $entry, $plugin, $recaptcha, $skips, $extra, $defaults);
  145. $data['stream'] = $stream;
  146. $data['mode'] = $mode;
  147. $data['return'] = $extra['return'];
  148. $data['entry'] = $entry;
  149. // Make input_slug as the array key
  150. // in order to ease form templating.
  151. if(is_array($data['fields'])){
  152. $fields_arr = array();
  153. foreach($data['fields'] as $field){
  154. $fields_arr[$field['input_slug']] = $field;
  155. }
  156. $data['fields'] = $fields_arr;
  157. }
  158. $data['groups'] = $this->group_m->get_all();
  159. // Build the form page.
  160. $this->template->title(lang('workflow:transitions:new'));
  161. $this->template->build('admin/transitions_form', $data);
  162. }
  163. /**
  164. * Edit a Transitions entry
  165. *
  166. * We're passing the
  167. * id of the entry, which will allow entry_form to
  168. * repopulate the data from the database.
  169. * We are building entry form manually using the fields API
  170. * and displaying the output in a custom view file.
  171. *
  172. * @param int [$id] The id of the Transitions to the be deleted.
  173. * @return void
  174. */
  175. public function edit($id = 0)
  176. {
  177. // Check permission
  178. if(! group_has_role('workflow', 'edit_all_transitions') AND ! group_has_role('workflow', 'edit_own_transitions')){
  179. $this->session->set_flashdata('error', lang('cp:access_denied'));
  180. redirect('admin');
  181. }
  182. // Check view all/own permission
  183. if(! group_has_role('workflow', 'edit_all_transitions')){
  184. $created_by_user_id = $this->streams->entries->get_entry($id, 'transitions', 'workflow', true)->created_by_user_id;
  185. if($created_by_user_id != $this->current_user->id){
  186. $this->session->set_flashdata('error', lang('cp:access_denied'));
  187. redirect('admin');
  188. }
  189. }
  190. // Load everything we need
  191. $this->load->library('streams_core/Fields');
  192. $stream = $this->streams->streams->get_stream('transitions', 'workflow');
  193. $namespace = 'workflow';
  194. $mode = 'edit'; //'new', 'edit'
  195. if( ! $entry = $this->row_m->get_row($id, $stream, false)){
  196. $this->log_error('invalid_row', 'form');
  197. }
  198. if( ! $data['status_from_entry'] = $this->streams->entries->get_entry($entry->transition_from, 'statuses', 'workflow')){
  199. die();
  200. }
  201. if( ! $data['status_to_entry'] = $this->streams->entries->get_entry($entry->transition_to, 'statuses', 'workflow')){
  202. die();
  203. }
  204. if( ! $data['workflow_entry'] = $this->streams->entries->get_entry($data['status_from_entry']->status_workflow, 'workflows', 'workflow')){
  205. die();
  206. }
  207. $data['groups'] = $this->group_m->get_all();
  208. if($_POST){
  209. $_POST['transition_triggers'] = serialize($this->input->post('transition_triggers'));
  210. }
  211. $plugin = false;
  212. $recaptcha = false;
  213. $extra = array(
  214. 'return' => 'admin/workflow/transitions/index/'.$data['workflow_entry']->id,
  215. 'success_message' => lang('workflow:transitions:submit_success'),
  216. 'failure_message' => lang('workflow:transitions:submit_failure'),
  217. 'title' => 'lang:workflow:transitions:edit',
  218. //'required' => '<span>*</span>',
  219. //'email_notification' => false,
  220. );
  221. $skips = array();
  222. $defaults = array();
  223. // Get our field form elements.
  224. $data['fields'] = $this->fields->build_form($stream, $mode, $entry, $plugin, $recaptcha, $skips, $extra, $defaults);
  225. $data['stream'] = $stream;
  226. $data['mode'] = $mode;
  227. $data['return'] = $extra['return'];
  228. $data['entry'] = $entry;
  229. // Make input_slug as the array key
  230. // in order to ease form templating.
  231. if(is_array($data['fields'])){
  232. $fields_arr = array();
  233. foreach($data['fields'] as $field){
  234. $fields_arr[$field['input_slug']] = $field;
  235. }
  236. $data['fields'] = $fields_arr;
  237. }
  238. // Build the form page.
  239. $this->template->title(lang('workflow:transitions:edit'));
  240. $this->template->build('admin/transitions_form', $data);
  241. }
  242. /**
  243. * Delete a Transitions entry
  244. *
  245. * @param int [$id] The id of Transitions to be deleted
  246. * @return void
  247. */
  248. public function delete($id = 0)
  249. {
  250. // Check permission
  251. if(! group_has_role('workflow', 'delete_all_transitions') AND ! group_has_role('workflow', 'delete_own_transitions')){
  252. $this->session->set_flashdata('error', lang('cp:access_denied'));
  253. redirect('admin');
  254. }
  255. // Check view all/own permission
  256. if(! group_has_role('workflow', 'delete_all_transitions')){
  257. $created_by_user_id = $this->streams->entries->get_entry($id, 'transitions', 'workflow', true)->created_by_user_id;
  258. if($created_by_user_id != $this->current_user->id){
  259. $this->session->set_flashdata('error', lang('cp:access_denied'));
  260. redirect('admin');
  261. }
  262. }
  263. if( ! $transition_entry = $this->streams->entries->get_entry($id, 'transitions', 'workflow')){
  264. die();
  265. }
  266. if( ! $status_from_entry = $this->streams->entries->get_entry($transition_entry->transition_from, 'statuses', 'workflow')){
  267. die();
  268. }
  269. if( ! $workflow_entry = $this->streams->entries->get_entry($status_from_entry->status_workflow, 'workflows', 'workflow')){
  270. die();
  271. }
  272. $this->streams->entries->delete_entry($id, 'transitions', 'workflow');
  273. $this->session->set_flashdata('error', lang('workflow:transitions:deleted'));
  274. redirect('admin/workflow/transitions/index/'.$workflow_entry->id);
  275. }
  276. }