Browse Source

Added careers cloning (BT#1916)

ywarnier 14 years ago
parent
commit
27d23c70df
3 changed files with 55 additions and 4 deletions
  1. 10 1
      main/admin/careers.php
  2. 37 1
      main/inc/lib/career.lib.php
  3. 8 2
      main/inc/lib/promotion.lib.php

+ 10 - 1
main/admin/careers.php

@@ -59,7 +59,7 @@ $extra_params['height'] = 'auto';
 
 //With this function we can add actions to the jgrid (edit, delete, etc)
 $action_links = 'function action_formatter(cellvalue, options, rowObject) {
-                         return \'<a href="?action=edit&id=\'+options.rowId+\'"><img width="20px" src="../img/edit.png" title="'.get_lang('Edit').'"></a> <a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;"  href="?action=delete&id=\'+options.rowId+\'"><img title="'.get_lang('Delete').'" src="../img/delete.png"></a>\'; 
+                         return \'<a href="?action=edit&id=\'+options.rowId+\'"><img width="20px" src="../img/edit.png" title="'.get_lang('Edit').'"></a> <a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;"  href="?action=delete&id=\'+options.rowId+\'"><img title="'.get_lang('Delete').'" src="../img/delete.png"></a> <a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;"  href="?action=copy&id=\'+options.rowId+\'"><img title="'.get_lang('Copy').'" src="../img/copy.gif"></a>\'; 
                  }';
 ?>
 <script>
@@ -144,6 +144,15 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') {
         Display::display_confirmation_message(get_lang('Deleted'));
     }
     $career->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 = $career->copy($_GET['id']);
+    if ($res) {
+        Display::display_confirmation_message(get_lang('Copied'));
+    }
+    $career->display();
 } else {
     $career->display();   
 }

+ 37 - 1
main/inc/lib/career.lib.php

@@ -101,5 +101,41 @@ class Career extends Model {
         $form->addRule('name', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
         return $form;
     }
-    
+    /**
+     * Copies the career to a new one
+     * @param   integer     Career ID
+     * @return  integer     New career ID on success, false on failure
+     */
+    public function copy($id) {
+        $career = $this->get($id);
+        $new = array();
+        foreach ($career 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;
+            }
+        }
+        $cid = $this->save($new);
+        //Now also copy each session of the promotion as a new session and register it inside the promotion
+        $promotion = new Promotion();
+        $promo_list   = $promotion->get_all_promotions_by_career_id($id);    
+        if (!empty($promo_list)) {
+            foreach($promo_list  as $item) {
+                $pid = $promotion->copy($item['id'], $cid);
+            }
+        }
+        return $cid;
+    }    
 }

+ 8 - 2
main/inc/lib/promotion.lib.php

@@ -111,11 +111,12 @@ class Promotion extends Model {
         return $form;
     }
     /**
-     * Copies the promotions to a new one
+     * Copies the promotion to a new one
      * @param   integer     Promotion ID
+     * @param   integer     Career ID, in case we want to change it
      * @return  integer     New promotion ID on success, false on failure
      */
-    public function copy($id) {
+    public function copy($id, $career_id = null) {
         $promotion = $this->get($id);
         $new = array();
         foreach ($promotion as $key => $val) {
@@ -131,6 +132,11 @@ class Promotion extends Model {
                     $val = api_get_utc_datetime();
                     $new[$key] = $val;
                     break;
+                case 'career_id':
+                    if (!empty($career_id)) {
+                    	$val = (int)$career_id;
+                    }
+                    $new[$key] = $val;
                 default:
                     $new[$key] = $val;
                     break;