Fix bug duplicate project with a too long name

This commit is contained in:
Frederic Guillot
2015-01-25 18:08:28 -05:00
parent e6cf1bf236
commit 1fc6d69e2e
7 changed files with 185 additions and 64 deletions

View File

@@ -36,6 +36,7 @@ use Symfony\Component\EventDispatcher\Event;
* @property \Model\Notification $notification
* @property \Model\Project $project
* @property \Model\ProjectPermission $projectPermission
* @property \Model\ProjectDuplication $projectDuplication
* @property \Model\ProjectAnalytic $projectAnalytic
* @property \Model\ProjectActivity $projectActivity
* @property \Model\ProjectDailySummary $projectDailySummary

View File

@@ -307,7 +307,7 @@ class Project extends Base
$this->checkCSRFParam();
if ($this->project->duplicate($project['id'])) {
if ($this->projectDuplication->duplicate($project['id'])) {
$this->session->flash(t('Project cloned successfully.'));
} else {
$this->session->flashError(t('Unable to clone this project.'));

View File

@@ -27,6 +27,7 @@ use Pimple\Container;
* @property \Model\LastLogin $lastLogin
* @property \Model\Notification $notification
* @property \Model\Project $project
* @property \Model\ProjectDuplication $projectDuplication
* @property \Model\ProjectPermission $projectPermission
* @property \Model\SubTask $subTask
* @property \Model\SubtaskHistory $subtaskHistory

View File

@@ -261,65 +261,6 @@ class Project extends Base
->filter(array($this, 'applyColumnStats'));
}
/**
* Create a project from another one.
*
* @author Antonio Rabelo
* @param integer $project_id Project Id
* @return integer Cloned Project Id
*/
public function createProjectFromAnotherProject($project_id)
{
$project = $this->getById($project_id);
$values = array(
'name' => $project['name'].' ('.t('Clone').')',
'is_active' => true,
'last_modified' => 0,
'token' => '',
'is_public' => 0,
'is_private' => empty($project['is_private']) ? 0 : 1,
);
if (! $this->db->table(self::TABLE)->save($values)) {
return 0;
}
return $this->db->getConnection()->getLastId();
}
/**
* Clone a project
*
* @author Antonio Rabelo
* @param integer $project_id Project Id
* @return integer Cloned Project Id
*/
public function duplicate($project_id)
{
$this->db->startTransaction();
// Get the cloned project Id
$clone_project_id = $this->createProjectFromAnotherProject($project_id);
if (! $clone_project_id) {
$this->db->cancelTransaction();
return false;
}
foreach (array('board', 'category', 'projectPermission', 'action') as $model) {
if (! $this->$model->duplicate($project_id, $clone_project_id)) {
$this->db->cancelTransaction();
return false;
}
}
$this->db->closeTransaction();
return (int) $clone_project_id;
}
/**
* Create a project
*

View File

@@ -0,0 +1,89 @@
<?php
namespace Model;
/**
* Project Duplication
*
* @package model
* @author Frederic Guillot
* @author Antonio Rabelo
*/
class ProjectDuplication extends Base
{
/**
* Get a valid project name for the duplication
*
* @access public
* @param string $name Project name
* @param integer $max_length Max length allowed
* @return string
*/
public function getClonedProjectName($name, $max_length = 50)
{
$suffix = ' ('.t('Clone').')';
if (strlen($name.$suffix) > 50) {
$name = substr($name, 0, 50 - strlen($suffix));
}
return $name.$suffix;
}
/**
* Create a project from another one
*
* @param integer $project_id Project Id
* @return integer Cloned Project Id
*/
public function copy($project_id)
{
$project = $this->project->getById($project_id);
$values = array(
'name' => $this->getClonedProjectName($project['name']),
'is_active' => true,
'last_modified' => 0,
'token' => '',
'is_public' => 0,
'is_private' => empty($project['is_private']) ? 0 : 1,
);
if (! $this->db->table(Project::TABLE)->save($values)) {
return 0;
}
return $this->db->getConnection()->getLastId();
}
/**
* Clone a project with all settings
*
* @param integer $project_id Project Id
* @return integer Cloned Project Id
*/
public function duplicate($project_id)
{
$this->db->startTransaction();
// Get the cloned project Id
$clone_project_id = $this->copy($project_id);
if (! $clone_project_id) {
$this->db->cancelTransaction();
return false;
}
foreach (array('board', 'category', 'projectPermission', 'action') as $model) {
if (! $this->$model->duplicate($project_id, $clone_project_id)) {
$this->db->cancelTransaction();
return false;
}
}
$this->db->closeTransaction();
return (int) $clone_project_id;
}
}

View File

@@ -28,6 +28,7 @@ class ClassProvider implements ServiceProviderInterface
'Project',
'ProjectActivity',
'ProjectAnalytic',
'ProjectDuplication',
'ProjectDailySummary',
'ProjectPermission',
'SubTask',