From 7749b8ed569f6d27b0bb2ed4c2040e8b61ed4422 Mon Sep 17 00:00:00 2001
From: Frédéric Guillot <fguillot@users.noreply.github.com>
Date: Sun, 9 Mar 2014 23:21:23 -0400
Subject: Automatic actions

---
 controllers/action.php  | 140 ++++++++++++++++++++++++++++++++++++++++++++++++
 controllers/app.php     |   2 +
 controllers/base.php    |  51 +++++-------------
 controllers/board.php   |   2 +
 controllers/config.php  |   2 +
 controllers/project.php |   2 +
 controllers/task.php    |   2 +
 controllers/user.php    |   2 +
 8 files changed, 164 insertions(+), 39 deletions(-)
 create mode 100644 controllers/action.php

(limited to 'controllers')

diff --git a/controllers/action.php b/controllers/action.php
new file mode 100644
index 00000000..3ee44364
--- /dev/null
+++ b/controllers/action.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace Controller;
+
+require_once __DIR__.'/Base.php';
+
+/**
+ * Automatic actions management
+ *
+ * @package controllers
+ * @author  Frederic Guillot
+ */
+class Action extends Base
+{
+    /**
+     * List of automatic actions for a given project
+     *
+     * @access public
+     */
+    public function index()
+    {
+        $project_id = $this->request->getIntegerParam('project_id');
+        $project = $this->project->getById($project_id);
+
+        if (! $project) {
+            $this->session->flashError(t('Project not found.'));
+            $this->response->redirect('?controller=project');
+        }
+
+        $this->response->html($this->template->layout('action_index', array(
+            'values' => array('project_id' => $project['id']),
+            'project' => $project,
+            'actions' => $this->action->getAllByProject($project['id']),
+            'available_actions' => $this->action->getAvailableActions(),
+            'available_events' => $this->action->getAvailableEvents(),
+            'available_params' => $this->action->getAllActionParameters(),
+            'columns_list' => $this->board->getColumnsList($project['id']),
+            'users_list' => $this->project->getUsersList($project['id'], false),
+            'projects_list' => $this->project->getList(false),
+            'menu' => 'projects',
+            'title' => t('Automatic actions')
+        )));
+    }
+
+    /**
+     * Define action parameters (step 2)
+     *
+     * @access public
+     */
+    public function params()
+    {
+        $project_id = $this->request->getIntegerParam('project_id');
+        $project = $this->project->getById($project_id);
+
+        if (! $project) {
+            $this->session->flashError(t('Project not found.'));
+            $this->response->redirect('?controller=project');
+        }
+
+        $values = $this->request->getValues();
+        $action = $this->action->load($values['action_name'], $values['project_id']);
+
+        $this->response->html($this->template->layout('action_params', array(
+            'values' => $values,
+            'action_params' => $action->getActionRequiredParameters(),
+            'columns_list' => $this->board->getColumnsList($project['id']),
+            'users_list' => $this->project->getUsersList($project['id'], false),
+            'projects_list' => $this->project->getList(false),
+            'project' => $project,
+            'menu' => 'projects',
+            'title' => t('Automatic actions')
+        )));
+    }
+
+    /**
+     * Create a new action (last step)
+     *
+     * @access public
+     */
+    public function create()
+    {
+        $project_id = $this->request->getIntegerParam('project_id');
+        $project = $this->project->getById($project_id);
+
+        if (! $project) {
+            $this->session->flashError(t('Project not found.'));
+            $this->response->redirect('?controller=project');
+        }
+
+        $values = $this->request->getValues();
+
+        list($valid, $errors) = $this->action->validateCreation($values);
+
+        if ($valid) {
+
+            if ($this->action->create($values)) {
+                $this->session->flash(t('Your automatic action have been created successfully.'));
+            }
+            else {
+                $this->session->flashError(t('Unable to create your automatic action.'));
+            }
+        }
+
+        $this->response->redirect('?controller=action&action=index&project_id='.$project['id']);
+    }
+
+    /**
+     * Confirmation dialog before removing an action
+     *
+     * @access public
+     */
+    public function confirm()
+    {
+        $this->response->html($this->template->layout('action_remove', array(
+            'action' => $this->action->getById($this->request->getIntegerParam('action_id')),
+            'available_events' => $this->action->getAvailableEvents(),
+            'available_actions' => $this->action->getAvailableActions(),
+            'menu' => 'projects',
+            'title' => t('Remove an action')
+        )));
+    }
+
+    /**
+     * Remove an action
+     *
+     * @access public
+     */
+    public function remove()
+    {
+        $action = $this->action->getById($this->request->getIntegerParam('action_id'));
+
+        if ($action && $this->action->remove($action['id'])) {
+            $this->session->flash(t('Action removed successfully.'));
+        } else {
+            $this->session->flashError(t('Unable to remove this action.'));
+        }
+
+        $this->response->redirect('?controller=action&action=index&project_id='.$action['project_id']);
+    }
+}
diff --git a/controllers/app.php b/controllers/app.php
index 981abbbe..633433fc 100644
--- a/controllers/app.php
+++ b/controllers/app.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class App extends Base
 {
     public function index()
diff --git a/controllers/base.php b/controllers/base.php
index 6dc9c0be..dd7c0642 100644
--- a/controllers/base.php
+++ b/controllers/base.php
@@ -2,48 +2,18 @@
 
 namespace Controller;
 
-require __DIR__.'/../lib/request.php';
-require __DIR__.'/../lib/response.php';
-require __DIR__.'/../lib/session.php';
-require __DIR__.'/../lib/template.php';
-require __DIR__.'/../lib/helper.php';
-require __DIR__.'/../lib/translator.php';
-require __DIR__.'/../models/base.php';
-require __DIR__.'/../models/acl.php';
-require __DIR__.'/../models/config.php';
-require __DIR__.'/../models/user.php';
-require __DIR__.'/../models/project.php';
-require __DIR__.'/../models/task.php';
-require __DIR__.'/../models/board.php';
-require __DIR__.'/../models/comment.php';
-
 abstract class Base
 {
-    protected $request;
-    protected $response;
-    protected $session;
-    protected $template;
-    protected $user;
-    protected $project;
-    protected $task;
-    protected $board;
-    protected $config;
-    protected $acl;
-    protected $comment;
-
-    public function __construct()
+    public function __construct(\Core\Registry $registry)
     {
-        $this->request = new \Request;
-        $this->response = new \Response;
-        $this->session = new \Session;
-        $this->template = new \Template;
-        $this->config = new \Model\Config;
-        $this->user = new \Model\User;
-        $this->project = new \Model\Project;
-        $this->task = new \Model\Task;
-        $this->board = new \Model\Board;
-        $this->acl = new \Model\Acl;
-        $this->comment = new \Model\Comment;
+        $this->acl = $registry->acl;
+        $this->action = $registry->action;
+        $this->board = $registry->board;
+        $this->config = $registry->config;
+        $this->project = $registry->project;
+        $this->task = $registry->task;
+        $this->user = $registry->user;
+        $this->comment = $registry->comment;
     }
 
     public function beforeAction($controller, $action)
@@ -74,6 +44,9 @@ abstract class Base
         if (! $this->acl->isPageAccessAllowed($controller, $action)) {
             $this->response->redirect('?controller=user&action=forbidden');
         }
+
+        // Attach events for automatic actions
+        $this->action->attachEvents();
     }
 
     public function checkProjectPermissions($project_id)
diff --git a/controllers/board.php b/controllers/board.php
index 13714b3c..9cdc4386 100644
--- a/controllers/board.php
+++ b/controllers/board.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class Board extends Base
 {
     // Change a task assignee directly from the board
diff --git a/controllers/config.php b/controllers/config.php
index 064fa06d..c4880b4a 100644
--- a/controllers/config.php
+++ b/controllers/config.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class Config extends Base
 {
     // Settings page
diff --git a/controllers/project.php b/controllers/project.php
index 8d8584bc..8b232e94 100644
--- a/controllers/project.php
+++ b/controllers/project.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class Project extends Base
 {
     // Display access forbidden page
diff --git a/controllers/task.php b/controllers/task.php
index fba4d4f5..05dd935e 100644
--- a/controllers/task.php
+++ b/controllers/task.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class Task extends Base
 {
     // Webhook to create a task (useful for external software)
diff --git a/controllers/user.php b/controllers/user.php
index 10d3ad21..700e5fae 100644
--- a/controllers/user.php
+++ b/controllers/user.php
@@ -2,6 +2,8 @@
 
 namespace Controller;
 
+require_once __DIR__.'/Base.php';
+
 class User extends Base
 {
     // Display access forbidden page
-- 
cgit v1.2.3