Code

996407c3076ae1dacc55f3c458c61ea069dff41b
[gosa.git] / include / class_CopyPasteHandler.inc
1 <?php
2 //FIXME: Comments are somewhat off the normal comment usage
4 class CopyPasteHandler {
6   var $config;
7   var $current;
9   var $copyCurrent = false;
10   var $cutCurrent  = false;
11   var $dialogOpen  = false;
12   var $objectdn    = false;
14   var $lastdn      = "";
16   var $was_successfull = false;
18   /* Create CP handler
19    */
20   function CopyPasteHandler($config){
21     $this->config = $config;    
22     $this->current= NULL;
23   }
26   /* Returns wether the current object can be pasted 
27    */   
28   function isCurrentObjectPastAble(){
30     /* Check if we got a valid object 
31      */
32     if($this->current == NULL){
33       return(false);
34     }
35     return(true);
36   }     
39   /* Clears all copy & paste informations 
40    */
41   function Clear()
42   {
43     $this->copyCurrent  = false;
44     $this->cutCurrent   = false;
45     $this->dialogOpen   = false;
46     $this->current              = NULL;
47   }
50   /* return current obejct dn
51    */
52   function GetCurrentDn()
53   {
54     return($this->objectdn);
55   }
58   /* Add Object which should be copied
59    */
60   function Copy($obj,$emptyObj)
61   {
62     $this->copyCurrent = true;
63     $this->objectdn    = $obj->dn;
64     $this->current = $emptyObj;
65     foreach($obj->by_object as $name => $obj){
66       
67       $this->current->by_object[$name]->PrepareForCopyPaste($obj);
68   
69       foreach(array("is_account") as $attr){
70         if(isset($obj->$attr)){
71           $this->current->by_object[$name]->$attr = $obj->$attr;
72         }
73       }
74     }
75     if($this->isCurrentObjectPastAble()){
76       return(true);
77     }else{
78       $this->cutCurrent = $this->copyCurrent = false;
79       $this->obj = NULL;
80     }
81     return(false);
82   }
85   /* Add Object which should be cutted
86    */
87   function Cut($obj){
88     $this->cutCurrent = true;
89     $this->current        = $obj;
90     $this->objectdn    = $obj->dn;
91     if($this->isCurrentObjectPastAble()){
92       return(true);
93     }else{
94       $this->cutCurrent = $this->copyCurrent = false;
95       $this->obj = NULL;
96     }
97     return(false);
98   }
101   /* Returns true if current object 
102    *  is cutted. And awaits to be pasted anywhere
103    */
104   function isCurrentCutted(){
105     return($this->cutCurrent);
106   }
109   /* Returns true if current object  
110    *  was copied, and awaits to be pasted again
111    */
112   function isCurrentCopied(){
113     return($this->copyCurrent);
114   }
117   /* stillOpen
118    */
119   function stillOpen(){ 
120     if(isset($_POST['AbortCopyPaste'])){
121       $this->dialogOpen = false;
122     }
124     return($this->dialogOpen);
125   }
128   /* Displays a dialog which allows the user to fix all dependencies of this object.
129      Create unique names, ids, or what ever
130    */
131   function execute($displayMessageOnSuccess = true)
132   {
133     $this->dialogOpen = true;
135     /* Cut & paste
136      */
137     if($this->cutCurrent){
138       $this->current->save();
139       $this->dialogOpen =false;
140       $smarty = get_smarty();
141       $smarty->assign("Complete",true);
142       $this->lastdn= $this->current->dn;
143       $this->Clear();
144       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
146       /* Copy & paste
147        */
148     }else{
149       if(isset($_POST['PerformCopyPaste'])){
150         $msgs = $this->check(); 
151         if(count ($msgs) ){
152           foreach( $msgs as $msg){
153             print_red($msg);
154           }
155         }else{
156           $this->current->save();
157           $this->dialogOpen =false;
158           $smarty = get_smarty();       
159           $smarty->assign("Complete",true);
160           $this->lastdn = $this->current->dn;
161           $this->Clear();
162           if($displayMessageOnSuccess){
163             return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
164           }else{
165             //FIXME: Types!!! Either use strings, or use bool. But don't mix it!
166             return( false);
167           }
168         }
169       }
170       $smarty = get_smarty();   
171       $smarty->assign("Complete",false);
172       $smarty->assign("AttributesToFix",$this->generateAttributesToFix());      
173       $smarty->assign("SubDialog",$this->current->SubDialog);
174     
175       $smarty->assign("objectDN"                 ,$this->objectdn);     
176       $smarty->assign("message", sprintf(_("You are going to copy the entry '%s'."), $this->objectdn)); 
177       return($smarty->fetch(get_template_path("copyPasteDialog.tpl",FALSE)));
178     }
179   }
181   /* Create dialog which asks unique attributes values ... 
182    *  calls tabs -> getCopyDialog() 
183    *    which calls tab -> getCopyDialog()
184    */
185   function generateAttributesToFix()
186   {
187     return($this->current->getCopyDialog());
188   }
191   /* Set a single attribute to specified value
192    *  example :   ("base", $newBase );
193    */
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    */
206   function save_object()
207   {
208     /* Assign posted var to all tabs
209      */
210     $this->current->saveCopyDialog();
211   }
214   /* Returns possible errors returned from all including tabs ..
215    */
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     }
225     return($ret);
226   }
228   
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 ?>