Code

Added some additional infos for copy & paste
[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     }
70     if($this->isCurrentObjectPastAble()){
71       return(true);
72     }else{
73       $this->cutCurrent = $this->copyCurrent = false;
74       $this->obj = NULL;
75     }
76     return(false);
77   }
80   /* Add Object which should be cutted  */
81   function Cut($obj){
82     $this->cutCurrent = true;
83     $this->current        = $obj;
84     $this->objectdn    = $obj->dn;
85     if($this->isCurrentObjectPastAble()){
86       return(true);
87     }else{
88       $this->cutCurrent = $this->copyCurrent = false;
89       $this->obj = NULL;
90     }
91     return(false);
92   }
95   /* Returns true if current object 
96    *  is cutted. And awaits to be pasted anywhere  */
97   function isCurrentCutted(){
98     return($this->cutCurrent);
99   }
102   /* Returns true if current object  
103    *  was copied, and awaits to be pasted again  */
104   function isCurrentCopied(){
105     return($this->copyCurrent);
106   }
109   /* Returns true if the copy$paste dialog is still open  */
110   function stillOpen(){ 
111     if(isset($_POST['AbortCopyPaste'])){
112       $this->dialogOpen = false;
113     }
114     return($this->dialogOpen);
115   }
118   /* Displays a dialog which allows the user to fix all dependencies of this object.
119      Create unique names, ids, or what ever */
120   function execute()
121   {
122     $this->dialogOpen = true;
124     /* Cut & paste
125      */
126     if($this->cutCurrent){
128       if($this->current){
129         $smarty = get_smarty();
130         $smarty->assign("type","cut");
131         $smarty->assign("Complete",false);
132         $smarty->assign("AttributesToFix","&nbsp;");
133         $smarty->assign("SubDialog",$this->current->SubDialog);
134         $smarty->assign("objectDN"     ,$this->objectdn);
135         $smarty->assign("message", sprintf(_("You are going to paste the cutted entry '%s'."), $this->objectdn));
136         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
137       }
139       if(isset($_POST['PerformCopyPaste'])){
140         $this->current->save();
141         $this->dialogOpen =false;
142         $this->Clear();
143       }
144       /* Copy & paste
145        */
146     }else{
147       if(isset($_POST['PerformCopyPaste'])){
148         $msgs = $this->check(); 
149         if(count ($msgs) ){
150           foreach( $msgs as $msg){
151             print_red($msg);
152           }
153         }else{
154           $this->current->save();
155           $this->lastdn = $this->current->dn;
156           $this->dialogOpen =false;
157           $this->Clear();
158         }
159       }
160       if($this->current){
161         $smarty = get_smarty(); 
162         $smarty->assign("type","copy");
163         $smarty->assign("Complete",false);
164         $smarty->assign("AttributesToFix",$this->generateAttributesToFix());    
165         $smarty->assign("SubDialog",$this->current->SubDialog);
166         $smarty->assign("objectDN"               ,$this->objectdn);     
167         $smarty->assign("message", sprintf(_("You are going to paste the copied entry '%s'."), $this->objectdn));       
168         return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
169       }
170     }
171   }
173   /* Create dialog which asks unique attributes/values ... 
174    *  call tabs -> getCopyDialog() 
175    *    which calls tab -> getCopyDialog()  */
176   function generateAttributesToFix()
177   {
178     if($this->current){
179       return($this->current->getCopyDialog());  
180     }
181   }
184   /* Set a single attribute to specified value
185    *  example :   ("base", $newBase );    */
186   function SetVar($name,$value)
187   {
188     foreach($this->     current->by_object as $key => $obj){
189       if(isset($this->current->by_object[$key]->$name)){
190         $this->current->by_object[$key]->$name = $value;
191       }
192     }
193   }
196   /* Save new values posted by copy & paste dialog */
197   function save_object()
198   {
199     /* Assign posted var to all tabs
200      */
201     if($this->current){
202       $this->current->saveCopyDialog();
203     }
204   }
207   /* Returns errors from including tabs. */
208   function check()
209   {
210     $ret = array();
211     foreach($this->     current->by_object as $obj){
212       if($obj->is_account){
213         $ret = array_merge($ret , $obj->check());
214       }
215     }
216     return($ret);
217   }
219   
220   /* returns the paste icon for headpages */ 
221   function generatePasteIcon()
222   {
223     $Copy_Paste= "&nbsp;<img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
224     if($this->isCurrentObjectPastAble()){
225       if($this->isCurrentCutted()){
226         $img= "images/cutpaste.png";
227       }else{
228         $img= "images/copypaste.png";
229       }
230       $Copy_Paste.= "<input type='image' name='editPaste' class='center'
231         src='".$img."' alt='"._("Paste")."' title='".$this->GetCurrentDn()."'>&nbsp;";
232     }else{
233       $Copy_Paste.= "<img class='center' src='images/cant_editpaste.png' alt=\""._("Can't paste")."\">&nbsp;";
234     }
236     return ($Copy_Paste);
237   }
240 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
241 ?>