diff --git a/main/admin/promotions.php b/main/admin/promotions.php
index ef5b1c8415..c38cfde4d4 100644
--- a/main/admin/promotions.php
+++ b/main/admin/promotions.php
@@ -64,7 +64,7 @@ $extra_params['autowidth'] = 'true'; //use the width of the parent
$extra_params['height'] = 'auto'; //use the width of the parent
//With this function we can add actions to the jgrid
$action_links = 'function action_formatter (cellvalue, options, rowObject) {
- return \'
\';
+ return \'
\';
}';
?>
@@ -158,7 +158,16 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') {
Display::display_confirmation_message(get_lang('Deleted'));
}
$promotion->display();
+} elseif (isset($_GET['action']) && $_GET['action'] == 'copy') {
+ if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
+ api_not_allowed();
+ }
+ $res = $promotion->copy($_GET['id']);
+ if ($res) {
+ Display::display_confirmation_message(get_lang('Copied'));
+ }
+ $promotion->display();
} else {
- $promotion->display();
+ $promotion->display();
}
Display::display_footer();
\ No newline at end of file
diff --git a/main/inc/lib/promotion.lib.php b/main/inc/lib/promotion.lib.php
index 622d89a787..d8d197e1ce 100644
--- a/main/inc/lib/promotion.lib.php
+++ b/main/inc/lib/promotion.lib.php
@@ -110,4 +110,44 @@ class Promotion extends Model {
return $form;
}
-}
+ /**
+ * Copies the promotions to a new one
+ * @param integer Promotion ID
+ * @return integer New promotion ID on success, false on failure
+ */
+ public function copy($id) {
+ $promotion = $this->get($id);
+ $new = array();
+ foreach ($promotion as $key => $val) {
+ switch ($key) {
+ case 'id':
+ case 'updated_at':
+ break;
+ case 'name':
+ $val .= ' '.get_lang('Copy');
+ $new[$key] = $val;
+ break;
+ case 'created_at':
+ $val = api_get_utc_datetime();
+ $new[$key] = $val;
+ break;
+ default:
+ $new[$key] = $val;
+ break;
+ }
+ }
+ $pid = $this->save($new);
+ //Now also copy each session of the promotion as a new session and register it inside the promotion
+ require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
+ $session_list = SessionManager::get_all_sessions_by_promotion($id);
+ if (!empty($session_list)) {
+ foreach($session_list as $item) {
+ $sid = SessionManager::copy_session($item['id'], true, true);
+ if ($sid != 0) {
+ SessionManager::suscribe_sessions_to_promotion($pid,array($sid));
+ }
+ }
+ }
+ return $pid;
+ }
+}
\ No newline at end of file