Code

e09e6926bd4d212f112086cd148be70b8a708981
[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   function CopyPasteHandler($config){
19     $this->config = $config;    
20     $this->current= NULL;
21   }
24   /* Returns wether the current object can be pasted */ 
25   function isCurrentObjectPastAble(){
27     /* Check if we got a valid object */
28     if($this->current == NULL){
29       return(false);
30     }
31     return(true);
32   }     
35   /* Clears all copy & paste informations */
36   function Clear()
37   {
38     $this->copyCurrent  = false;
39     $this->cutCurrent   = false;
40     $this->dialogOpen   = false;
41     $this->current              = NULL;
42   }
45   /* return current obejct dn */
46   function GetCurrentDn()
47   {
48     return($this->objectdn);
49   }
52   /* Add Object which should be copied  */
53   function Copy($obj,$emptyObj)
54   {
55     $this->copyCurrent = true;
56     $this->objectdn    = $obj->dn;
57     $this->current = $emptyObj;
58     foreach($obj->by_object as $name => $obj){
59     
60       /* Prepare every single class, to be copied  */
61       $this->current->by_object[$name]->PrepareForCopyPaste($obj);
62  
63       /* handle some special vars */ 
64       foreach(array("is_account") as $attr){
65         if(isset($obj->$attr)){
66           $this->current->by_object[$name]->$attr = $obj->$attr;
67         }
68       }
69     }
71     /* Ensure that UnitTags will be kept when pasted */
72     if(isset($obj->gosaUnitTag) && !empty($obj->gosaUnitTag)){
73       foreach($this->current->by_object as $name => $obj){
74         if(!in_array("gosaAdministrativeUnitTag",$this->current->by_object[$name]->objectclasses)){
75           $this->current->by_object[$name]->objectclasses[] = "gosaAdministrativeUnitTag";
76           $this->current->by_object[$name]->gosaUnitTag = $obj->gosaUnitTag;
77           if(!in_array("gosaUnitTag",$this->current->by_object[$name]->attributes)){
78             $this->current->by_object[$name]->attributes[] =  "gosaUnitTag";
79           }
80         }
81       }
82     }
84     if($this->isCurrentObjectPastAble()){
85       return(true);
86     }else{
87       $this->cutCurrent = $this->copyCurrent = false;
88       $this->obj = NULL;
89     }
90     return(false);
91   }
94   /* Add Object which should be cutted  */
95   function Cut($obj){
96     $this->cutCurrent = true;
97     $this->current        = $obj;
98     $this->objectdn    = $obj->dn;
99     if($this->isCurrentObjectPastAble()){
100       return(true);
101     }else{
102       $this->cutCurrent = $this->copyCurrent = false;
103       $this->obj = NULL;
104     }
105     return(false);
106   }
109   /* Returns true if current object 
110    *  is cutted. And awaits to be pasted anywhere  */
111   function isCurrentCutted(){
112     return($this->cutCurrent);
113   }
116   /* Returns true if current object  
117    *  was copied, and awaits to be pasted again  */
118   function isCurrentCopied(){
119     return($this->copyCurrent);
120   }
123   /* Returns true if the copy$paste dialog is still open  */
124   function stillOpen(){ 
125     if(isset($_POST['AbortCopyPaste'])){
126       $this->dialogOpen = false;
127     }
128     return($this->dialogOpen);
129   }
132   /* Displays a dialog which allows the user to fix all dependencies of this object.
133      Create unique names, ids, or what ever */
134   function execute()
135   {
136     $this->dialogOpen = true;
138     /* Cut & paste
139      */
140     if($this->cutCurrent){
142       $msgs = $this->check();   
143       if(count ($msgs) ){
144         foreach( $msgs as $msg){
145           print_red($msg);
146         }
147         $this->dialogOpen =false;
148       }else{
149         $this->current->save();
150         $this->dialogOpen =false;
151         $this->Clear();
152       }
153       /* Copy & paste
154        */
155     }else{
156       if(isset($_POST['PerformCopyPaste'])){
157         $msgs = $this->check(); 
158         if(count ($msgs) ){
159           foreach( $msgs as $msg){
160             print_red($msg);
161           }
162         }else{
163           $this->current->save();
164           $this->lastdn = $this->current->dn;
165           $this->dialogOpen =false;
166           $this->Clear();
167         }
168       }
169       if($this->current){
170         $smarty = get_smarty(); 
171         $smarty->assign("Complete",false);
172         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());    
173         $smarty->assign("SubDialog",$this->current->SubDialog);
174         $smarty->assign("objectDN"               ,$this->objectdn);     
175         $smarty->assign("message", sprintf(_("You are going to copy the entry '%s'."), $this->objectdn));       
176         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
177       }
178     }
179   }
181   /* Create dialog which asks unique attributes/values ... 
182    *  call tabs -> getCopyDialog() 
183    *    which calls tab -> getCopyDialog()  */
184   function generateAttributesToFix()
185   {
186     if($this->current){
187       return($this->current->getCopyDialog());  
188     }
189   }
192   /* Set a single attribute to specified value
193    *  example :   ("base", $newBase );    */
194   function SetVar($name,$value)
195   {
196     foreach($this->     current->by_object as $key => $obj){
197       if(isset($this->current->by_object[$key]->$name)){
198         $this->current->by_object[$key]->$name = $value;
199       }
200     }
201   }
204   /* Save new values posted by copy & paste dialog */
205   function save_object()
206   {
207     /* Assign posted var to all tabs
208      */
209     if($this->current){
210       $this->current->saveCopyDialog();
211     }
212   }
215   /* Returns errors from including tabs. */
216   function check()
217   {
218     $ret = array();
219     foreach($this->     current->by_object as $obj){
220       if($obj->is_account){
221         $ret = array_merge($ret , $obj->check());
222       }
223     }
224     return($ret);
225   }
227   
228   /* returns the paste icon for headpages */ 
229   function generatePasteIcon()
230   {
231     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
232     if($this->isCurrentObjectPastAble()){
233       if($this->isCurrentCutted()){
234         $img= "images/cutpaste.png";
235       }else{
236         $img= "images/copypaste.png";
237       }
238       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
239         src='".$img."' alt='"._("Paste")."' title='".$this->GetCurrentDn()."'>&nbsp;";
240     }else{
241       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\" >&nbsp;";
242     }
244     return ($Copy_Paste);
245   }
248 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
249 ?>