1<?php
2
3/*
4   ------------------------------------------------------------------------
5   FusionInventory
6   Copyright (C) 2010-2016 by the FusionInventory Development Team.
7
8   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
9   ------------------------------------------------------------------------
10
11   LICENSE
12
13   This file is part of FusionInventory project.
14
15   FusionInventory is free software: you can redistribute it and/or modify
16   it under the terms of the GNU Affero General Public License as published by
17   the Free Software Foundation, either version 3 of the License, or
18   (at your option) any later version.
19
20   FusionInventory is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23   GNU Affero General Public License for more details.
24
25   You should have received a copy of the GNU Affero General Public License
26   along with FusionInventory. If not, see <http://www.gnu.org/licenses/>.
27
28   ------------------------------------------------------------------------
29
30   @package   FusionInventory
31   @author    Walid Nouh
32   @co-author
33   @copyright Copyright (c) 2010-2016 FusionInventory team
34   @license   AGPL License 3.0 or (at your option) any later version
35              http://www.gnu.org/licenses/agpl-3.0-standalone.html
36   @link      http://www.fusioninventory.org/
37   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
38   @since     2010
39
40   ------------------------------------------------------------------------
41 */
42
43if (!defined('GLPI_ROOT')) {
44   die("Sorry. You can't access directly to this file");
45}
46
47Session::checkLoginUser();
48
49$rule = $rulecollection->getRuleClass();
50
51if (!isset($_GET["id"])) {
52   $_GET["id"] = "";
53}
54
55$rulecollection->checkGlobal('r');
56
57if (isset($_GET["action"])) {
58   $rulecollection->checkGlobal('w');
59   $rulecollection->changeRuleOrder($_GET["id"], $_GET["action"]);
60   Html::back();
61
62} else if (isset($_POST["action"])) {
63   $rulecollection->checkGlobal('w');
64
65   // Use massive action system
66   switch ($_POST["action"]) {
67      case "delete" :
68         if (isset($_POST["item"]) && count($_POST["item"])) {
69            foreach ($_POST["item"] as $key => $val) {
70               $rule->getFromDB($key);
71               $rulecollection->deleteRuleOrder($rule->fields["ranking"]);
72               $rule->delete(array('id' => $key));
73            }
74            Event::log(0, "rules", 4, "setup", $_SESSION["glpiname"]." ".__('item\'s deletion'));
75
76            Html::back();
77         }
78         break;
79
80      case "move_rule" :
81         if (isset($_POST["item"]) && count($_POST["item"])) {
82            foreach ($_POST["item"] as $key => $val) {
83               $rule->getFromDB($key);
84               $rulecollection->moveRule($key, $_POST['ranking'], $_POST['move_type']);
85            }
86         }
87         break;
88
89      case "activate_rule" :
90         if (isset($_POST["item"])) {
91            $rule = new PluginFusioninventoryRule();
92            foreach ($_POST["item"] as $key => $val) {
93               if ($val == 1) {
94                  $input = array();
95                  $input['id'] = $key;
96                  $input['is_active'] = $_POST["activate_rule"];
97                  $rule->update($input);
98               }
99            }
100         }
101         break;
102   }
103
104} else if (isset($_POST["replay_rule"]) || isset($_GET["replay_rule"])) {
105   $rulecollection->checkGlobal('w');
106
107   // Current time
108   $start = explode(" ", microtime());
109   $start = $start[0]+$start[1];
110
111   // Limit computed from current time
112   $max = get_cfg_var("max_execution_time");
113   $max = $start + ($max>0 ? $max/2.0 : 30.0);
114
115   Html::header(_n('Rule', 'Rules', 2), $_SERVER['PHP_SELF'], "admin", $rulecollection->menu_type,
116                $rulecollection->menu_option);
117
118   if (!(isset($_POST['replay_confirm']) || isset($_GET['offset']))
119       && $rulecollection->warningBeforeReplayRulesOnExistingDB($_SERVER['PHP_SELF'])) {
120      Html::footer();
121      exit();
122   }
123
124   echo "<table class='tab_cadrehov'>";
125
126   echo "<tr><th><div class='relative'><strong>" .$rulecollection->getTitle(). "</strong>";
127   echo " - " .__('Replay the dictionary rules'). "</th></tr>\n";
128   echo "<tr><td class='center'>";
129   Html::createProgressBar(__('Work in progress...'));
130
131   echo "</td></tr>\n";
132   echo "</table>";
133
134   $manufacturer = 0;
135   if (!isset($_GET['offset'])) {
136      // First run
137      $offset       = $rulecollection->replayRulesOnExistingDB(0, $max, array(), $_POST);
138      $manufacturer = (isset($_POST["manufacturer"]) ? $_POST["manufacturer"] : 0);
139
140   } else {
141      // Next run
142      $offset       = $rulecollection->replayRulesOnExistingDB($_GET['offset'],
143                                                               $max,
144                                                               array(),
145                                                               $_GET);
146      $manufacturer = $_GET["manufacturer"];
147
148      // global start for stat
149      $start = $_GET["start"];
150   }
151
152   if ($offset < 0) {
153      // Work ended
154      $end   = explode(" ", microtime());
155      $duree = round($end[0]+$end[1]-$start);
156      Html::changeProgressBarMessage(__('Task completed.').
157                                       " (".Html::timestampToString($duree).")");
158      echo "<a href='".$_SERVER['PHP_SELF']."'>".__('Back')."</a>";
159
160   } else {
161      // Need more work
162      Html::redirect($_SERVER['PHP_SELF'].
163                        "?start=$start&replay_rule=1&offset=$offset&manufacturer="."$manufacturer");
164   }
165
166   Html::footer(TRUE);
167   exit();
168}
169
170Html::header(_n('Rule', 'Rules', 2), $_SERVER['PHP_SELF'], "admin", $rulecollection->menu_type,
171             $rulecollection->menu_option);
172
173   $tabs = array();
174   if ($rulecollection->showInheritedTab()) {
175      $tabs[0] = array(
176                   'title'  => __('Rules applied', 'fusioninventory').' : '.
177                                   Dropdown::getDropdownName('glpi_entities',
178                                                             $_SESSION['glpiactive_entity']),
179                   'url'    => $CFG_GLPI['root_doc']."/plugins/fusioninventory/ajax/rules.tabs.php",
180                   'params' => "target=".$_SERVER['PHP_SELF']."&glpi_tab=1&inherited=1&itemtype=".
181                                    get_class($rulecollection));
182   }
183
184   $title = _n('Rule', 'Rules', 2);
185
186   if ($rulecollection->isRuleRecursive()) {
187      $title = __('Local rules', 'fusioninventory').' : '.
188                     Dropdown::getDropdownName('glpi_entities', $_SESSION['glpiactive_entity']);
189   }
190   $tabs[1] = array('title'  => $title,
191                   'url'    => $CFG_GLPI['root_doc']."/plugins/fusioninventory/ajax/rules.tabs.php",
192                   'params' => "target=".$_SERVER['PHP_SELF']."&glpi_tab=0&inherited=0&itemtype=".
193                                 get_class($rulecollection));
194
195   if ($rulecollection->showChildrensTab()) {
196      $tabs[2] = array(
197                  'title'  => __('Rules applicable in the sub-entities'),
198
199                  'url'    => $CFG_GLPI['root_doc']."/plugins/fusioninventory/ajax/rules.tabs.php",
200                  'params' => "target=".$_SERVER['PHP_SELF'].
201                                    "&glpi_tab=2&inherited=0&childrens=1&itemtype=".
202                                    get_class($rulecollection));
203   }
204
205
206   echo "<div id='tabspanel' class='center-h'></div>";
207   Ajax::createTabs('tabspanel', 'tabcontent', $tabs, $rulecollection->getRuleClassName());
208   echo "<div id='tabcontent'>&nbsp;</div>";
209   echo "<script type='text/javascript'>loadDefaultTab();</script>";
210
211Html::footer();
212
213?>
214