Code

Updated DaemonEvent to allow sending additional attributes by using $this->attributes...
[gosa.git] / gosa-plugins / goto / addons / goto / class_goto_import_file.inc
1 <?php
3 class goto_import_file extends plugin
4 {
6   var $events     = array();
7   var $csv_fields = array();
9   public function __construct($config,&$parent)
10   {
11     plugin::plugin($config,NULL);
12     $this->parent = $parent;
13     $this->daemon_events  = DaemonEvent::get_event_types( SYSTEM_EVENT);
15     $this->csv_fields = array(
16         "0" => "MAC",  "1" => "HEADER", "2" => "OGROUP",
17         "3" => "BASE", "4" => "FQDN",   "5" => "IP",     "6" => "DHCP");
18   }
21   private function parse_csv($str)
22   {
23     
24     /* Some file checks 
25      */
26     $lines = split("\n",$str);
27     if(empty($str) || !count($lines)){
28       msg_dialog::display(_("Import"),_("Could not import file, it seems to be empty."),ERROR_DIALOG);
29       return;
30     }
32     /* Reset current events 
33      */
34     $this->events = array(); 
36     /* Parse each line of the given file
37      */
38     foreach($lines as $line){
40       // Skip empty lines.
41       if(empty($line)) continue;
43       /* Load values from file 
44        */
45       $fields = split(";",$line);
46       $event = array();
47       foreach($this->csv_fields as $key => $val) {
48         $event[$val] = "";
49         if(isset($fields[$key])){
50           $event[$val] = $fields[$key];
51         }
52       }
53       $this->events[] = $event;
54     }
55     $this->check_fields();
56   }
59   public function execute()
60   {
61     /* Import file 
62      */
63     if(isset($_POST['import']) && isset($_FILES['file'])) {
64       if(isset($_FILES['file']['tmp_name'])){
65         $str = file_get_contents($_FILES['file']['tmp_name']);
66         $this->parse_csv($str);
67       }
68     }
69     
70     /* Import started 
71      */
72     $confirmed = FALSE;
73     foreach($_POST as $name => $value){ 
74       if(preg_match("/^MSG_OK/",$name)){
75         $confirmed = TRUE;
76       }
77     }
78     if(isset($_POST['start_import']) || $confirmed){
79       $error = FALSE;
80       if(!$confirmed){
81         foreach($this->events as $event){
82           if(!empty($event['ERROR'])){
83             $error = TRUE;
84             break;
85           } 
86         }
87         if($error){
88           msg_dialog::display(_("Import"),
89               _("Entries marked as having errors will be skippen, do you still want to proceed?"),CONFIRM_DIALOG);
90         }
91       }
92       if(!$error){
94         $success = 0;
95         $fail = 0;
97         foreach($this->events as $key => $event){
98           if(!empty($event['ERROR'])){  
99             $fail ++;
100             continue;
101           }
102           $class= $this->daemon_events['QUEUED'][$event['HEADER']];
103           $object = new $class($this->config);
104           $object->set_type(TRIGGERED_EVENT);
105           $object->add_targets(array($event['MAC']));
106           if(!$this->parent->o_queue->append($object)){
107             msg_dialog::display(_("Service infrastructure"),msgPool::siError($this->parent->o_queue->get_error()),ERROR_DIALOG);
108             $fail ++;
109           }else{
110             unset($this->events[$key]);
111             $success ++;
112           }
113         }
114         msg_dialog::display(_("Import"),sprintf(_("Import complete: %s events successfully send, %s failed."),$success,$fail),INFO_DIALOG);
115       }
116     }
119     /* Prepare output 
120      */
121     $evts = $this->events;
122     foreach($this->events as $id => $evt){
123       foreach($evt as $key => $val){
124         if(in_array($key,$this->csv_fields)){
125           $evts[$id][$key] = "<span style=\"white-space: nowrap;\">".strip_tags($val)."</span>"; 
126         }
127       }
128     }
129  
130     $smarty = get_smarty();
131     $smarty->assign("info",$evts);
132     $smarty->assign("count",count($evts));
133     return($smarty->fetch(get_template_path('goto_import_file.tpl', TRUE)));
134   }
137   public function check()
138   {
139     $message = plugin::check();
140     $this->check_fields();
141     return($message);
142   }
144   
145   private function check_fields()
146   {
147     foreach($this->events as $key => $event){
148       $this->events[$key]['ERROR'] = "";
149       if(empty($event['MAC']) || !tests::is_mac($event['MAC'])){
150         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("Mac")).", ";
151       }
152       if(empty($event['HEADER']) || !isset($this->daemon_events['QUEUED'][$event['HEADER']])){
153         $this->events[$key]['ERROR'] .=  msgPool::invalid(_("Event")).", ";
154       }
155       $this->events[$key]['ERROR'] = trim($this->events[$key]['ERROR'],", ");
156     }
157   } 
158
160 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
161 ?>