PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/include/admin/genorder.php

http://opixido-ocms.googlecode.com/
PHP | 274 lines | 157 code | 79 blank | 38 comment | 34 complexity | 3ab62b93c4725fea0310ecb85d80a6a6 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. #
  3. # This file is part of oCMS.
  4. #
  5. # oCMS is free software: you cgan redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # oCMS is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with oCMS. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # @author Celio Conort / Opixido
  19. # @copyright opixido 2012
  20. # @link http://code.google.com/p/opixido-ocms/
  21. # @package ocms
  22. #
  23. /*
  24. *
  25. * GenOrder
  26. */
  27. class GenOrder {
  28. public $order_field;
  29. public $table;
  30. public $id;
  31. public $fk_id;
  32. public $fk_champ;
  33. function GenOrder($table, $id = 0, $fk_id = 0, $fk_champ = '') {
  34. global $_Gconfig;
  35. if (!isset($_Gconfig['fullArboRev'])) {
  36. foreach ($_Gconfig['fullArbo'] as $root_table => $v) {
  37. $v = array_pop($v);
  38. $_Gconfig['fullArboRev'][$v[0]] = array($v[1], $v[2]);
  39. }
  40. }
  41. global $orderFields;
  42. $this->table = $table;
  43. $this->id = $id;
  44. $this->fk_id = $fk_id;
  45. if (!empty($orderFields[$this->table]) && is_array($orderFields[$this->table])) {
  46. $this->DoIt = true;
  47. } else {
  48. $this->DoIt = false;
  49. return;
  50. }
  51. $this->order_field = $orderFields[$this->table][0];
  52. if ($fk_champ == '') {
  53. $this->fk_champ = akev($orderFields[$this->table], 1);
  54. } else {
  55. $this->fk_champ = $fk_champ;
  56. }
  57. $this->pk = GetPrimaryKey($this->table);
  58. if (($this->id > 0 ) && strlen($this->fk_champ)) {
  59. $sql = 'SELECT ' . $this->fk_champ . ',' . $this->order_field . ' FROM ' . $this->table . ' WHERE ' . $this->pk . ' = "' . $this->id . '"';
  60. $row = GetSingle($sql);
  61. $this->fk_id = akev($row, $this->fk_champ);
  62. $this->curOrderValue = $row[$this->order_field];
  63. }
  64. }
  65. function GetUp() {
  66. global $_Gconfig;
  67. if (!$this->DoIt)
  68. return;
  69. logAction('get_up', $this->table, $_REQUEST['curId']);
  70. $sql = 'SELECT ' . $this->order_field . ',' . $this->pk . '
  71. FROM ' . $this->table . '
  72. WHERE ' . $this->order_field . ' < ' . $this->curOrderValue . '
  73. AND ' . $this->fk_champ . ' = "' . $this->fk_id . '" ';
  74. if (!empty($_Gconfig['fullArboRev'][$this->table])) {
  75. if ($this->fk_champ == $_Gconfig['fullArboRev'][$this->table][0]) {
  76. $sql .= ' AND ' . $_Gconfig['fullArboRev'][$this->table][1] . ' IS NULL ';
  77. }
  78. }
  79. $sql .= '
  80. ' . $this->specialClause() . '
  81. ORDER BY ' . $this->order_field . ' DESC LIMIT 0,1';
  82. //echo $sql;
  83. $row = GetSingle($sql);
  84. //debug($row[$this->pk] ." -> ".$this->curOrderValue);
  85. //debug($this->id ." -> ".$row[$this->order_field]);
  86. if (count($row)) {
  87. $sql1 = 'UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $this->curOrderValue . ' WHERE ' . $this->pk . ' = ' . $row[$this->pk];
  88. $sql2 = 'UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $row[$this->order_field] . ' WHERE ' . $this->pk . ' = ' . $this->id;
  89. DoSql($sql1);
  90. DoSql($sql2);
  91. }
  92. }
  93. function GetDown() {
  94. global $_Gconfig;
  95. if (!$this->DoIt)
  96. return;
  97. logAction('get_down', $this->table, $_REQUEST['curId']);
  98. $sql = 'SELECT ' . $this->order_field . ',' . $this->pk . '
  99. FROM ' . $this->table . '
  100. WHERE ' . $this->order_field . ' > ' . $this->curOrderValue . '
  101. AND ' . $this->fk_champ . ' = "' . $this->fk_id . '"';
  102. if (!empty($_Gconfig['fullArboRev'][$this->table])) {
  103. if ($this->fk_champ == $_Gconfig['fullArboRev'][$this->table][0]) {
  104. $sql .= ' AND ' . $_Gconfig['fullArboRev'][$this->table][1] . ' IS NULL ';
  105. }
  106. }
  107. $sql .= '
  108. ' . $this->specialClause() . '
  109. ORDER BY ' . $this->order_field . ' ASC LIMIT 0,1';
  110. $row = GetSingle($sql);
  111. //debug($row[$this->pk] ." -> ".$this->curOrderValue);
  112. //debug($this->id ." -> ".$row[$this->order_field]);
  113. if (count($row)) {
  114. $sql1 = 'UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $this->curOrderValue . ' WHERE ' . $this->pk . ' = ' . $row[$this->pk];
  115. $sql2 = 'UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $row[$this->order_field] . ' WHERE ' . $this->pk . ' = ' . $this->id;
  116. DoSql($sql1);
  117. DoSql($sql2);
  118. }
  119. }
  120. function ReOrder() {
  121. /*
  122. * Reorder the whole table
  123. */
  124. if (!$this->DoIt) {
  125. return;
  126. }
  127. if ($this->fk_id) {
  128. $sql = 'SELECT * FROM ' . $this->table . ' WHERE 1 ';
  129. if (strlen($this->fk_champ)) {
  130. $sql .= ' AND ' . $this->fk_champ . ' = "' . $this->fk_id . '"';
  131. }
  132. $sql .= ' ' . $this->specialClause() . ' ';
  133. $sql .= ' ORDER BY ' . $this->order_field . ' , ' . GetTitleFromTable($this->table, " , ");
  134. $res = GetAll($sql);
  135. /*
  136. * On parcourt le groupe d'enregistrements en question
  137. */
  138. $this->reorderRes($res);
  139. }
  140. }
  141. private function specialClause() {
  142. if ($this->table == 's_rubrique') {
  143. return ' ' . sqlRubriqueOnlyReal() . ' ';
  144. } else {
  145. return '';
  146. }
  147. }
  148. function ReorderRes($res = false) {
  149. if (!$this->DoIt || !$this->fk_id)
  150. return;
  151. if (!$res) {
  152. $res = GetAll('SELECT * FROM ' . $this->table . ' WHERE ' . $this->fk_champ . ' = ' . sql($this->fk_id) . ' ORDER BY ' . $this->order_field);
  153. }
  154. $normalOrder = 0;
  155. foreach ($res as $row) {
  156. $normalOrder++;
  157. /* Si l'ordre ne correspond pas on le change */
  158. if ($row[$this->order_field] != $normalOrder) {
  159. $sql = 'UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $normalOrder . ' WHERE ' . $this->pk . ' = "' . $row[$this->pk] . '"';
  160. $res = DoSql($sql);
  161. // echo $sql . '<br/>';
  162. }
  163. }
  164. }
  165. function orderAfterInsertLastAtTop() {
  166. if (!$this->DoIt)
  167. return;
  168. if (strlen($this->fk_champ)) {
  169. $sql = ('UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $this->order_field . ' + 1
  170. WHERE ' . $this->fk_champ . ' = "' . $this->fk_id . '"
  171. ');
  172. $sql .= $this->specialClause();
  173. DoSql($sql);
  174. $sql = ('UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = 1
  175. WHERE ' . GetPrimaryKey($this->table) . ' = "' . $this->id . '"
  176. ');
  177. doSql($sql);
  178. }
  179. $this->reorderRes();
  180. }
  181. function OrderAfterInsertLastAtBottom() {
  182. if (!$this->DoIt)
  183. return;
  184. $sql = 'SELECT MAX(' . $this->order_field . ') AS MAXI FROM ' . $this->table . ' WHERE 1 ';
  185. if (strlen($this->fk_champ)) {
  186. $sql .= ' AND ' . $this->fk_champ . ' = "' . $this->fk_id . '"';
  187. }
  188. $sql .= $this->specialClause();
  189. $sql .= 'LIMIT 0,1';
  190. $max = GetSingle($sql);
  191. $max = $max['MAXI'] + 1;
  192. DoSql('UPDATE ' . $this->table . ' SET ' . $this->order_field . ' = ' . $max . ' WHERE ' . $this->pk . ' = ' . $this->id);
  193. $this->reorderRes();
  194. }
  195. }