Code

Added copy & paste for user
[gosa.git] / include / class_CopyPasteHandler.inc
1 <?php
3 class CopyPasteHandler {
5   var $config;
6   var $current;
8   var $copyCurrent = false;
9   var $cutCurrent  = false;
10   var $dialogOpen  = false;
11   var $objectdn    = false;
13   var $lastdn      = "";
15   var $was_successfull = false;
17   /* Create CP handler
18    */
19   function CopyPasteHandler($config){
20     $this->config = $config;    
21     $this->current= NULL;
22   }
25   /* Returns wether the current object can be pasted 
26    */   
27   function isCurrentObjectPastAble(){
29     /* Check if we got a valid object 
30      */
31     if($this->current == NULL){
32       return(false);
33     }
34     return(true);
35   }     
38   /* Clears all copy & paste informations 
39    */
40   function Clear()
41   {
42     $this->copyCurrent  = false;
43     $this->cutCurrent   = false;
44     $this->dialogOpen   = false;
45     $this->current              = NULL;
46   }
49   /* return current obejct dn
50    */
51   function GetCurrentDn()
52   {
53     return($this->objectdn);
54   }
57   /* Add Object which should be copied
58    */
59   function Copy($obj,$emptyObj)
60   {
61     $this->copyCurrent = true;
62     $this->objectdn    = $obj->dn;
63     $this->current = $emptyObj;
64     foreach($obj->by_object as $name => $obj){
65       foreach($obj->attributes as $attr){
66         $this->current->by_object[$name]->$attr = $obj->$attr;
67       }
68       foreach(array("is_account") as $attr){
69         if(isset($obj->$attr)){
70           $this->current->by_object[$name]->$attr = $obj->$attr;
71         }
72       }
73     }
74     if($this->isCurrentObjectPastAble()){
75       return(true);
76     }else{
77       $this->cutCurrent = $this->copyCurrent = false;
78       $this->obj = NULL;
79     }
80     return(false);
81   }
84   /* Add Object which should be cutted
85    */
86   function Cut($obj){
87     $this->cutCurrent = true;
88     $this->current        = $obj;
89     $this->objectdn    = $obj->dn;
90     if($this->isCurrentObjectPastAble()){
91       return(true);
92     }else{
93       $this->cutCurrent = $this->copyCurrent = false;
94       $this->obj = NULL;
95     }
96     return(false);
97   }
100   /* Returns true if current object 
101    *  is cutted. And awaits to be pasted anywhere
102    */
103   function isCurrentCutted(){
104     return($this->cutCurrent);
105   }
108   /* Returns true if current object  
109    *  was copied, and awaits to be pasted again
110    */
111   function isCurrentCopied(){
112     return($this->copyCurrent);
113   }
116   /* stillOpen
117    */
118   function stillOpen(){ 
119     if(isset($_POST['AbortCopyPaste'])){
120       $this->dialogOpen = false;
121     }
123     return($this->dialogOpen);
124   }
127   /* Displays a dialog which allows the user to fix all dependencies of this object.
128      Create unique names, ids, or what ever
129    */
130   function execute($displayMessageOnSuccess = true)
131   {
132     $this->dialogOpen = true;
134     /* Cut & paste
135      */
136     if($this->cutCurrent){
137       $this->current->save();
138       $this->dialogOpen =false;
139       $smarty = get_smarty();
140       $smarty->assign("Complete",true);
141       $this->lastdn= $this->current->dn;
142       $this->Clear();
143       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
145       /* Copy & paste
146        */
147     }else{
148       if(isset($_POST['PerformCopyPaste'])){
149         $msgs = $this->check(); 
150         if(count ($msgs) ){
151           foreach( $msgs as $msg){
152             print_red($msg);
153           }
154         }else{
155           $this->current->save();
156           $this->dialogOpen =false;
157           $smarty = get_smarty();       
158           $smarty->assign("Complete",true);
159           $this->lastdn = $this->current->dn;
160           $this->Clear();
161           if($displayMessageOnSuccess){
162             return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
163           }else{
164             return( false);
165           }
166         }
167       }
168       $smarty = get_smarty();   
169       $smarty->assign("Complete",false);
170       $smarty->assign("AttributesToFix",$this->generateAttributesToFix());      
171       $smarty->assign("objectDN"                 ,$this->objectdn);     
172       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
173     }
174   }
176   /* Create dialog which asks unique attributes values ... 
177    *  calls tabs -> getCopyDialog() 
178    *    which calls tab -> getCopyDialog()
179    */
180   function generateAttributesToFix()
181   {
182     return($this->current->getCopyDialog());
183   }
186   /* Set a single attribute to specified value
187    *  example :   ("base", $newBase );
188    */
189   function SetVar($name,$value)
190   {
191     foreach($this->     current->by_object as $key => $obj){
192       if(isset($this->current->by_object[$key]->$name)){
193         $this->current->by_object[$key]->$name = $value;
194       }
195     }
196   }
199   /* Save new values posted by copy & paste dialog
200    */
201   function save_object()
202   {
203     /* Assign posted var to all tabs
204      */
205     $this->current->saveCopyDialog();
206   }
209   /* Returns possible errors returned from all including tabs ..
210    */
211   function check()
212   {
213     $ret = array();
214     foreach($this->     current->by_object as $obj){
215       $ret = array_merge($ret , $obj->check());
216     }
218     return($ret);
219   }
222 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
223 ?>