diff options
Diffstat (limited to 'app/Model')
-rw-r--r-- | app/Model/Base.php | 80 | ||||
-rw-r--r-- | app/Model/Project.php | 4 | ||||
-rw-r--r-- | app/Model/Subtask.php | 4 | ||||
-rw-r--r-- | app/Model/TaskCreation.php | 4 | ||||
-rw-r--r-- | app/Model/TaskImport.php | 156 | ||||
-rw-r--r-- | app/Model/TaskModification.php | 6 | ||||
-rw-r--r-- | app/Model/User.php | 8 | ||||
-rw-r--r-- | app/Model/UserImport.php | 118 |
8 files changed, 13 insertions, 367 deletions
diff --git a/app/Model/Base.php b/app/Model/Base.php index 635ed09a..714b4308 100644 --- a/app/Model/Base.php +++ b/app/Model/Base.php @@ -33,86 +33,6 @@ abstract class Base extends \Kanboard\Core\Base } /** - * Remove keys from an array - * - * @access public - * @param array $values Input array - * @param string[] $keys List of keys to remove - */ - public function removeFields(array &$values, array $keys) - { - foreach ($keys as $key) { - if (array_key_exists($key, $values)) { - unset($values[$key]); - } - } - } - - /** - * Remove keys from an array if empty - * - * @access public - * @param array $values Input array - * @param string[] $keys List of keys to remove - */ - public function removeEmptyFields(array &$values, array $keys) - { - foreach ($keys as $key) { - if (array_key_exists($key, $values) && empty($values[$key])) { - unset($values[$key]); - } - } - } - - /** - * Force fields to be at 0 if empty - * - * @access public - * @param array $values Input array - * @param string[] $keys List of keys - */ - public function resetFields(array &$values, array $keys) - { - foreach ($keys as $key) { - if (isset($values[$key]) && empty($values[$key])) { - $values[$key] = 0; - } - } - } - - /** - * Force some fields to be integer - * - * @access public - * @param array $values Input array - * @param string[] $keys List of keys - */ - public function convertIntegerFields(array &$values, array $keys) - { - foreach ($keys as $key) { - if (isset($values[$key])) { - $values[$key] = (int) $values[$key]; - } - } - } - - /** - * Force some fields to be null if empty - * - * @access public - * @param array $values Input array - * @param string[] $keys List of keys - */ - public function convertNullFields(array &$values, array $keys) - { - foreach ($keys as $key) { - if (array_key_exists($key, $values) && empty($values[$key])) { - $values[$key] = null; - } - } - } - - /** * Build SQL condition for a given time range * * @access protected diff --git a/app/Model/Project.php b/app/Model/Project.php index a79e46a1..d2e5b7ce 100644 --- a/app/Model/Project.php +++ b/app/Model/Project.php @@ -334,7 +334,7 @@ class Project extends Base $values['identifier'] = strtoupper($values['identifier']); } - $this->convertIntegerFields($values, array('priority_default', 'priority_start', 'priority_end')); + $this->helper->model->convertIntegerFields($values, array('priority_default', 'priority_start', 'priority_end')); if (! $this->db->table(self::TABLE)->save($values)) { $this->db->cancelTransaction(); @@ -402,7 +402,7 @@ class Project extends Base $values['identifier'] = strtoupper($values['identifier']); } - $this->convertIntegerFields($values, array('priority_default', 'priority_start', 'priority_end')); + $this->helper->model->convertIntegerFields($values, array('priority_default', 'priority_start', 'priority_end')); return $this->exists($values['id']) && $this->db->table(self::TABLE)->eq('id', $values['id'])->save($values); diff --git a/app/Model/Subtask.php b/app/Model/Subtask.php index b5898fcf..3f5cfe83 100644 --- a/app/Model/Subtask.php +++ b/app/Model/Subtask.php @@ -168,8 +168,8 @@ class Subtask extends Base */ public function prepare(array &$values) { - $this->removeFields($values, array('another_subtask')); - $this->resetFields($values, array('time_estimated', 'time_spent')); + $this->helper->model->removeFields($values, array('another_subtask')); + $this->helper->model->resetFields($values, array('time_estimated', 'time_spent')); } /** diff --git a/app/Model/TaskCreation.php b/app/Model/TaskCreation.php index 576eb18c..2d2e5504 100644 --- a/app/Model/TaskCreation.php +++ b/app/Model/TaskCreation.php @@ -52,8 +52,8 @@ class TaskCreation extends Base $values = $this->dateParser->convert($values, array('date_due')); $values = $this->dateParser->convert($values, array('date_started'), true); - $this->removeFields($values, array('another_task')); - $this->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated')); + $this->helper->model->removeFields($values, array('another_task')); + $this->helper->model->resetFields($values, array('date_started', 'creator_id', 'owner_id', 'swimlane_id', 'date_due', 'score', 'category_id', 'time_estimated')); if (empty($values['column_id'])) { $values['column_id'] = $this->column->getFirstColumnId($values['project_id']); diff --git a/app/Model/TaskImport.php b/app/Model/TaskImport.php deleted file mode 100644 index ccab0152..00000000 --- a/app/Model/TaskImport.php +++ /dev/null @@ -1,156 +0,0 @@ -<?php - -namespace Kanboard\Model; - -use Kanboard\Core\Csv; -use SimpleValidator\Validator; -use SimpleValidator\Validators; - -/** - * Task Import - * - * @package model - * @author Frederic Guillot - */ -class TaskImport extends Base -{ - /** - * Number of successful import - * - * @access public - * @var integer - */ - public $counter = 0; - - /** - * Project id to import tasks - * - * @access public - * @var integer - */ - public $projectId; - - /** - * Get mapping between CSV header and SQL columns - * - * @access public - * @return array - */ - public function getColumnMapping() - { - return array( - 'reference' => 'Reference', - 'title' => 'Title', - 'description' => 'Description', - 'assignee' => 'Assignee Username', - 'creator' => 'Creator Username', - 'color' => 'Color Name', - 'column' => 'Column Name', - 'category' => 'Category Name', - 'swimlane' => 'Swimlane Name', - 'score' => 'Complexity', - 'time_estimated' => 'Time Estimated', - 'time_spent' => 'Time Spent', - 'date_due' => 'Due Date', - 'is_active' => 'Closed', - ); - } - - /** - * Import a single row - * - * @access public - * @param array $row - * @param integer $line_number - */ - public function import(array $row, $line_number) - { - $row = $this->prepare($row); - - if ($this->validateCreation($row)) { - if ($this->taskCreation->create($row) > 0) { - $this->logger->debug('TaskImport: imported successfully line '.$line_number); - $this->counter++; - } else { - $this->logger->error('TaskImport: creation error at line '.$line_number); - } - } else { - $this->logger->error('TaskImport: validation error at line '.$line_number); - } - } - - /** - * Format row before validation - * - * @access public - * @param array $row - * @return array - */ - public function prepare(array $row) - { - $values = array(); - $values['project_id'] = $this->projectId; - $values['reference'] = $row['reference']; - $values['title'] = $row['title']; - $values['description'] = $row['description']; - $values['is_active'] = Csv::getBooleanValue($row['is_active']) == 1 ? 0 : 1; - $values['score'] = (int) $row['score']; - $values['time_estimated'] = (float) $row['time_estimated']; - $values['time_spent'] = (float) $row['time_spent']; - - if (! empty($row['assignee'])) { - $values['owner_id'] = $this->user->getIdByUsername($row['assignee']); - } - - if (! empty($row['creator'])) { - $values['creator_id'] = $this->user->getIdByUsername($row['creator']); - } - - if (! empty($row['color'])) { - $values['color_id'] = $this->color->find($row['color']); - } - - if (! empty($row['column'])) { - $values['column_id'] = $this->column->getColumnIdByTitle($this->projectId, $row['column']); - } - - if (! empty($row['category'])) { - $values['category_id'] = $this->category->getIdByName($this->projectId, $row['category']); - } - - if (! empty($row['swimlane'])) { - $values['swimlane_id'] = $this->swimlane->getIdByName($this->projectId, $row['swimlane']); - } - - if (! empty($row['date_due'])) { - $values['date_due'] = $this->dateParser->getTimestampFromIsoFormat($row['date_due']); - } - - $this->removeEmptyFields( - $values, - array('owner_id', 'creator_id', 'color_id', 'column_id', 'category_id', 'swimlane_id', 'date_due') - ); - - return $values; - } - - /** - * Validate user creation - * - * @access public - * @param array $values - * @return boolean - */ - public function validateCreation(array $values) - { - $v = new Validator($values, array( - new Validators\Integer('project_id', t('This value must be an integer')), - new Validators\Required('project_id', t('The project is required')), - new Validators\Required('title', t('The title is required')), - new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), - new Validators\MaxLength('reference', t('The maximum length is %d characters', 50), 50), - )); - - return $v->execute(); - } -} diff --git a/app/Model/TaskModification.php b/app/Model/TaskModification.php index 8e59b3fe..a77b78a4 100644 --- a/app/Model/TaskModification.php +++ b/app/Model/TaskModification.php @@ -87,9 +87,9 @@ class TaskModification extends Base $values = $this->dateParser->convert($values, array('date_due')); $values = $this->dateParser->convert($values, array('date_started'), true); - $this->removeFields($values, array('another_task', 'id')); - $this->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent')); - $this->convertIntegerFields($values, array('priority', 'is_active', 'recurrence_status', 'recurrence_trigger', 'recurrence_factor', 'recurrence_timeframe', 'recurrence_basedate')); + $this->helper->model->removeFields($values, array('another_task', 'id')); + $this->helper->model->resetFields($values, array('date_due', 'date_started', 'score', 'category_id', 'time_estimated', 'time_spent')); + $this->helper->model->convertIntegerFields($values, array('priority', 'is_active', 'recurrence_status', 'recurrence_trigger', 'recurrence_factor', 'recurrence_timeframe', 'recurrence_basedate')); $values['date_modification'] = time(); } diff --git a/app/Model/User.php b/app/Model/User.php index 2d87d35b..0e11422b 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -253,10 +253,10 @@ class User extends Base } } - $this->removeFields($values, array('confirmation', 'current_password')); - $this->resetFields($values, array('is_ldap_user', 'disable_login_form')); - $this->convertNullFields($values, array('gitlab_id')); - $this->convertIntegerFields($values, array('gitlab_id')); + $this->helper->model->removeFields($values, array('confirmation', 'current_password')); + $this->helper->model->resetFields($values, array('is_ldap_user', 'disable_login_form')); + $this->helper->model->convertNullFields($values, array('gitlab_id')); + $this->helper->model->convertIntegerFields($values, array('gitlab_id')); } /** diff --git a/app/Model/UserImport.php b/app/Model/UserImport.php deleted file mode 100644 index 0ec4e802..00000000 --- a/app/Model/UserImport.php +++ /dev/null @@ -1,118 +0,0 @@ -<?php - -namespace Kanboard\Model; - -use SimpleValidator\Validator; -use SimpleValidator\Validators; -use Kanboard\Core\Security\Role; -use Kanboard\Core\Csv; - -/** - * User Import - * - * @package model - * @author Frederic Guillot - */ -class UserImport extends Base -{ - /** - * Number of successful import - * - * @access public - * @var integer - */ - public $counter = 0; - - /** - * Get mapping between CSV header and SQL columns - * - * @access public - * @return array - */ - public function getColumnMapping() - { - return array( - 'username' => 'Username', - 'password' => 'Password', - 'email' => 'Email', - 'name' => 'Full Name', - 'is_admin' => 'Administrator', - 'is_manager' => 'Manager', - 'is_ldap_user' => 'Remote User', - ); - } - - /** - * Import a single row - * - * @access public - * @param array $row - * @param integer $line_number - */ - public function import(array $row, $line_number) - { - $row = $this->prepare($row); - - if ($this->validateCreation($row)) { - if ($this->user->create($row)) { - $this->logger->debug('UserImport: imported successfully line '.$line_number); - $this->counter++; - } else { - $this->logger->error('UserImport: creation error at line '.$line_number); - } - } else { - $this->logger->error('UserImport: validation error at line '.$line_number); - } - } - - /** - * Format row before validation - * - * @access public - * @param array $row - * @return array - */ - public function prepare(array $row) - { - $row['username'] = strtolower($row['username']); - - foreach (array('is_admin', 'is_manager', 'is_ldap_user') as $field) { - $row[$field] = Csv::getBooleanValue($row[$field]); - } - - if ($row['is_admin'] == 1) { - $row['role'] = Role::APP_ADMIN; - } elseif ($row['is_manager'] == 1) { - $row['role'] = Role::APP_MANAGER; - } else { - $row['role'] = Role::APP_USER; - } - - unset($row['is_admin']); - unset($row['is_manager']); - - $this->removeEmptyFields($row, array('password', 'email', 'name')); - - return $row; - } - - /** - * Validate user creation - * - * @access public - * @param array $values - * @return boolean - */ - public function validateCreation(array $values) - { - $v = new Validator($values, array( - new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50), - new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), User::TABLE, 'id'), - new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6), - new Validators\Email('email', t('Email address invalid')), - new Validators\Integer('is_ldap_user', t('This value must be an integer')), - )); - - return $v->execute(); - } -} |