PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/fieldunicity.class.php

https://github.com/ardowz/Thesis-SideB
PHP | 496 lines | 462 code | 2 blank | 32 comment | 1 complexity | 2d396842ed844cd8ab697ee17736a1f4 MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id: fieldunicity.class.php 14917 2011-07-13 09:10:10Z moyo $
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2003-2011 by the INDEPNET Development Team.
  7. http://indepnet.net/ http://glpi-project.org
  8. -------------------------------------------------------------------------
  9. LICENSE
  10. This file is part of GLPI.
  11. GLPI is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. GLPI is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with GLPI; if not, write to the Free Software Foundation, Inc.,
  21. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. --------------------------------------------------------------------------
  23. */
  24. // ----------------------------------------------------------------------
  25. // Original Author of file: Walid Nouh
  26. // Purpose of file:
  27. // ----------------------------------------------------------------------
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. /// Class FieldUnicity
  32. class FieldUnicity extends CommonDropdown {
  33. // From CommonDBTM
  34. public $dohistory = true;
  35. var $second_level_menu = "control";
  36. static function getTypeName() {
  37. global $LANG;
  38. return $LANG['setup'][811];
  39. }
  40. function canCreate() {
  41. return haveRight('config', 'w');
  42. }
  43. function canView() {
  44. return haveRight('config', 'r');
  45. }
  46. function getAdditionalFields() {
  47. global $LANG;
  48. return array(array('name' => 'is_active',
  49. 'label' => $LANG['common'][60],
  50. 'type' => 'bool'),
  51. array('name' => 'itemtype',
  52. 'label' => $LANG['common'][17],
  53. 'type' => 'unicity_itemtype'),
  54. array('name' => 'fields',
  55. 'label' => $LANG['setup'][815],
  56. 'type' => 'unicity_fields'),
  57. array('name' => 'action_refuse',
  58. 'label' => $LANG['setup'][821],
  59. 'type' => 'bool'),
  60. array('name' => 'action_notify',
  61. 'label' => $LANG['setup'][822],
  62. 'type' => 'bool'));
  63. }
  64. /**
  65. * Add more tabs to display
  66. *
  67. * @param $options array
  68. **/
  69. function defineMoreTabs($options=array()) {
  70. global $LANG;
  71. $ong = array();
  72. $ong[12] = $LANG['title'][38];
  73. $ong[2] = $LANG['setup'][826];
  74. return $ong;
  75. }
  76. /**
  77. * Display more tabs
  78. *
  79. * @param $tab
  80. **/
  81. function displayMoreTabs($tab) {
  82. switch ($tab) {
  83. case 2:
  84. self::showDoubles($this);
  85. break;
  86. case 12 :
  87. Log::showForItem($this);
  88. break;
  89. case -1 :
  90. self::showDoubles($this);
  91. Log::showForItem($this);
  92. break;
  93. }
  94. }
  95. /**
  96. * Display specific fields for FieldUnicity
  97. *
  98. * @param $ID
  99. * @param $field array
  100. **/
  101. function displaySpecificTypeField($ID, $field=array()) {
  102. switch ($field['type']) {
  103. case 'unicity_itemtype' :
  104. $this->showItemtype($ID, $this->fields['itemtype']);
  105. break;
  106. case 'unicity_fields' :
  107. self::selectCriterias($this);
  108. break;
  109. }
  110. }
  111. /**
  112. * Display a dropdown which contains all the available itemtypes
  113. *
  114. * @param ID the field unicity item id
  115. * @param value the selected value
  116. *
  117. * @return nothing
  118. **/
  119. function showItemtype($ID, $value=0) {
  120. global $CFG_GLPI;
  121. //Criteria already added : only display the selected itemtype
  122. if ($ID > 0) {
  123. $item = new $this->fields['itemtype'];
  124. echo $item->getTypeName();
  125. echo "<input type='hidden' name='itemtype' value='".$this->fields['itemtype']."'";
  126. } else {
  127. //Add criteria : display dropdown
  128. $options[0] = DROPDOWN_EMPTY_VALUE;
  129. foreach ($CFG_GLPI['unicity_types'] as $itemtype) {
  130. if (class_exists($itemtype)) {
  131. $item = new $itemtype();
  132. if ($item->can(-1,'r')) {
  133. $options[$itemtype] = $item->getTypeName($itemtype);
  134. }
  135. }
  136. }
  137. asort($options);
  138. $rand = Dropdown::showFromArray('itemtype', $options);
  139. $params = array('itemtype' => '__VALUE__',
  140. 'id' => $ID);
  141. ajaxUpdateItemOnSelectEvent("dropdown_itemtype$rand", "span_fields",
  142. $CFG_GLPI["root_doc"]."/ajax/dropdownUnicityFields.php",
  143. $params);
  144. }
  145. }
  146. /**
  147. * Return criteria unicity for an itemtype, in an entity
  148. *
  149. * @param itemtype the itemtype for which unicity must be checked
  150. * @param entities_id the entity for which configuration must be retrivied
  151. * @param $check_active
  152. *
  153. * @return an array of fields to check, or an empty array if no
  154. **/
  155. public static function getUnicityFieldsConfig($itemtype, $entities_id=0, $check_active=true) {
  156. global $DB;
  157. //Get the first active configuration for this itemtype
  158. $query = "SELECT *
  159. FROM `glpi_fieldunicities`
  160. WHERE `itemtype` = '$itemtype' ".
  161. getEntitiesRestrictRequest("AND", 'glpi_fieldunicities', "", $entities_id,
  162. true);
  163. if ($check_active) {
  164. $query .= " AND `is_active` = '1' ";
  165. }
  166. $query .= "ORDER BY `entities_id` DESC";
  167. $current_entity = false;
  168. $return = array();
  169. foreach ($DB->request($query) as $data) {
  170. //First row processed
  171. if (!$current_entity) {
  172. $current_entity = $data['entities_id'];
  173. }
  174. //Process only for one entity, not more
  175. if ($current_entity != $data['entities_id']) {
  176. break;
  177. }
  178. $return[] = $data;
  179. }
  180. return $return;
  181. }
  182. /**
  183. * Display a list of available fields for unicity checks
  184. *
  185. * @param $unicity an instance of CommonDBTM class
  186. *
  187. * @return nothing
  188. **/
  189. static function selectCriterias(CommonDBTM $unicity) {
  190. global $DB;
  191. //Do not check unicity on fields in DB with theses types
  192. $blacklisted_types = array('longtext', 'text');
  193. echo "<span id='span_fields' name='span_fields'>";
  194. if (!isset($unicity->fields['itemtype']) || !$unicity->fields['itemtype']) {
  195. echo "</span>";
  196. return;
  197. }
  198. if (!isset($unicity->fields['entities_id'])) {
  199. $unicity->fields['entities_id'] = $_SESSION['glpiactive_entity'];
  200. }
  201. $unicity_fields = explode(',', $unicity->fields['fields']);
  202. //Search option for this type
  203. $target = new $unicity->fields['itemtype'];
  204. //Construct list
  205. echo "<span id='span_fields' name='span_fields'>";
  206. echo "<select name='_fields[]' multiple size='15' style='width:400px'>";
  207. foreach ($DB->list_fields(getTableForItemType($unicity->fields['itemtype'])) as $field) {
  208. $searchOption = $target->getSearchOptionByField('field', $field['Field']);
  209. if (empty($searchOption)) {
  210. if ($table = getTableNameForForeignKeyField($field['Field'])) {
  211. $searchOption = $target->getSearchOptionByField('field', 'name', $table);
  212. }
  213. }
  214. if (!empty($searchOption)
  215. && !in_array($field['Type'],$blacklisted_types)
  216. && !in_array($field['Field'],$target->getUnallowedFieldsForUnicity())) {
  217. echo "<option value='".$field['Field']."'";
  218. if (isset($unicity_fields) && in_array($field['Field'],$unicity_fields)) {
  219. echo " selected ";
  220. }
  221. echo ">".$searchOption['name']."</option>";
  222. }
  223. }
  224. echo "</select></span>";
  225. }
  226. function getSearchOptions() {
  227. global $LANG;
  228. $tab = array();
  229. $tab['common'] = $LANG['setup'][811];
  230. $tab[1]['table'] = $this->getTable();
  231. $tab[1]['field'] = 'name';
  232. $tab[1]['name'] = $LANG['common'][16];
  233. $tab[1]['datatype'] = 'itemlink';
  234. $tab[1]['itemlink_type'] = $this->getType();
  235. $tab[1]['massiveaction'] = false;
  236. $tab[2]['table'] = $this->getTable();
  237. $tab[2]['field'] = 'id';
  238. $tab[2]['name'] = $LANG['common'][2];
  239. $tab[2]['massiveaction'] = false;
  240. $tab[3]['table'] = $this->getTable();
  241. $tab[3]['field'] = 'fields';
  242. $tab[3]['name'] = $LANG['setup'][815];
  243. $tab[3]['massiveaction'] = false;
  244. $tab[4]['table'] = $this->getTable();
  245. $tab[4]['field'] = 'itemtype';
  246. $tab[4]['name'] = $LANG['common'][17];
  247. $tab[4]['massiveaction'] = false;
  248. $tab[4]['datatype'] = 'itemtypename';
  249. $tab[4]['forcegroupby'] = true;
  250. $tab[5]['table'] = $this->getTable();
  251. $tab[5]['field'] = 'action_refuse';
  252. $tab[5]['name'] = $LANG['setup'][821];
  253. $tab[5]['datatype'] = 'bool';
  254. $tab[6]['table'] = $this->getTable();
  255. $tab[6]['field'] = 'action_notify';
  256. $tab[6]['name'] = $LANG['setup'][822];
  257. $tab[6]['datatype'] = 'bool';
  258. $tab[86]['table'] = $this->getTable();
  259. $tab[86]['field'] = 'is_recursive';
  260. $tab[86]['name'] = $LANG['entity'][9];
  261. $tab[86]['datatype'] = 'bool';
  262. $tab[16]['table'] = $this->getTable();
  263. $tab[16]['field'] = 'comment';
  264. $tab[16]['name'] = $LANG['common'][25];
  265. $tab[16]['datatype'] = 'text';
  266. $tab[30]['table'] = $this->getTable();
  267. $tab[30]['field'] = 'is_active';
  268. $tab[30]['name'] = $LANG['common'][60];
  269. $tab[30]['datatype'] = 'bool';
  270. $tab[30]['massiveaction'] = false;
  271. $tab[80]['table'] = 'glpi_entities';
  272. $tab[80]['field'] = 'completename';
  273. $tab[80]['name'] = $LANG['entity'][0];
  274. $tab[80]['forcegroupby'] = true;
  275. return $tab;
  276. }
  277. /**
  278. * Perform checks to be sure that an itemtype and at least a field are selected
  279. *
  280. * @param input the values to insert in DB
  281. *
  282. * @return input the values to insert, but modified
  283. **/
  284. static function checkBeforeInsert($input) {
  285. global $LANG;
  286. if (!$input['itemtype'] || empty($input['_fields'])) {
  287. addMessageAfterRedirect($LANG['setup'][817], true, ERROR);
  288. $input = array();
  289. } else {
  290. $input['fields'] = implode(',',$input['_fields']);
  291. unset($input['_fields']);
  292. }
  293. return $input;
  294. }
  295. function prepareInputForAdd($input) {
  296. return self::checkBeforeInsert($input);
  297. }
  298. function prepareInputForUpdate($input) {
  299. $input['fields'] = implode(',',$input['_fields']);
  300. unset($input['_fields']);
  301. return $input;
  302. }
  303. /**
  304. * Delete all criterias for an itemtype
  305. *
  306. * @param itemtype
  307. *
  308. * @return nothing
  309. **/
  310. static function deleteForItemtype($itemtype) {
  311. global $DB;
  312. $query = "DELETE
  313. FROM `glpi_fieldunicities`
  314. WHERE `itemtype` LIKE '%Plugin$itemtype%'";
  315. $DB->query($query);
  316. }
  317. /**
  318. * List doubles
  319. *
  320. * @param $unicity an instance of FieldUnicity class
  321. **/
  322. static function showDoubles(FieldUnicity $unicity) {
  323. global $LANG, $DB;
  324. $fields = array();
  325. $where_fields = array();
  326. $item = new $unicity->fields['itemtype'];
  327. foreach (explode(',',$unicity->fields['fields']) as $field) {
  328. $fields[] = $field;
  329. $where_fields[] = $field;
  330. }
  331. if (!empty($fields)) {
  332. $colspan = count($fields) + 1;
  333. echo "<table class='tab_cadre_fixe'>";
  334. echo "<tr><th colspan='".$colspan."'>".$LANG['setup'][826]."</th></tr>";
  335. $entities = array($unicity->fields['entities_id']);
  336. if ($unicity->fields['is_recursive']) {
  337. $entities = getSonsOf('glpi_entities', $unicity->fields['entities_id']);
  338. }
  339. $fields_string = implode(',', $fields);
  340. if ($item->maybeTemplate()) {
  341. $where_template = " AND `".$item->getTable()."`.`is_template` = '0'";
  342. } else {
  343. $where_template = "";
  344. }
  345. $where_fields_string = implode(',', $where_fields);
  346. $query = "SELECT $fields_string,
  347. COUNT(*) AS cpt
  348. FROM `".$item->getTable()."`
  349. WHERE `".$item->getTable()."`.`entities_id` IN (".implode(',',$entities).")
  350. $where_template
  351. GROUP BY $fields_string
  352. ORDER BY cpt DESC";
  353. $results = array();
  354. foreach ($DB->request($query) as $data) {
  355. if ($data['cpt'] > 1) {
  356. $results[] = $data;
  357. }
  358. }
  359. if (empty($results)) {
  360. echo "<tr><td class='center' colspan='$colspan'>".$LANG['stats'][2]."</td></tr>";
  361. } else {
  362. echo "<tr>";
  363. foreach ($fields as $field) {
  364. $searchOption = $item->getSearchOptionByField('field',$field);
  365. echo "<th>".$searchOption["name"]."</th>";
  366. }
  367. echo "<th>".$LANG['tracking'][29]."</th></tr>";
  368. foreach ($results as $result) {
  369. echo "<tr>";
  370. foreach ($fields as $field) {
  371. echo "<td>".$result[$field]."</td>";
  372. }
  373. echo "<td>".$result['cpt']."</td></tr>";
  374. }
  375. }
  376. } else {
  377. echo "<tr><td class='center' colspan='$colspan'>".$LANG['stats'][2]."</td></tr>";
  378. }
  379. echo "</table>";
  380. }
  381. /**
  382. * Display debug information for current object
  383. **/
  384. function showDebug() {
  385. $params = array('message' => '',
  386. 'action_type' => true,
  387. 'action_user' => getUserName(getLoginUserID()),
  388. 'entities_id' => $_SESSION['glpiactive_entity'],
  389. 'itemtype' => get_class($this),
  390. 'date' => $_SESSION['glpi_currenttime'],
  391. 'refuse' => true);
  392. NotificationEvent::debugEvent($this, $params);
  393. }
  394. }
  395. ?>