summaryrefslogtreecommitdiff
path: root/app/Model/ProjectDuplication.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-01-25 18:08:28 -0500
committerFrederic Guillot <fred@kanboard.net>2015-01-25 18:08:28 -0500
commit1fc6d69e2e25d6e4feaa38fe03dab98c75cfb36c (patch)
tree984e346cefa4b34b6bb5cfa4454f2156e64fe1da /app/Model/ProjectDuplication.php
parente6cf1bf23648f02615eeeddd34b0f4f2578f1f0a (diff)
Fix bug duplicate project with a too long name
Diffstat (limited to 'app/Model/ProjectDuplication.php')
-rw-r--r--app/Model/ProjectDuplication.php89
1 files changed, 89 insertions, 0 deletions
diff --git a/app/Model/ProjectDuplication.php b/app/Model/ProjectDuplication.php
new file mode 100644
index 00000000..11a606d7
--- /dev/null
+++ b/app/Model/ProjectDuplication.php
@@ -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;
+ }
+}