/assets/lib/SimpleTab/table.abstract.php

https://github.com/dmi3yy/modx.evo.custom · PHP · 304 lines · 194 code · 30 blank · 80 comment · 46 complexity · bca1a3d52587c22265cd2e7756641a99 MD5 · raw file

  1. <?php namespace SimpleTab;
  2. require_once(MODX_BASE_PATH . 'assets/lib/MODxAPI/autoTable.abstract.php');
  3. require_once(MODX_BASE_PATH . 'assets/lib/Helpers/FS.php');
  4. require_once(MODX_BASE_PATH . 'assets/lib/Helpers/PHPThumb.php');
  5. /**
  6. * Class dataTable
  7. * @package SimpleTab
  8. */
  9. class dataTable extends \autoTable
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $params = array();
  15. /**
  16. * @var \Helpers\FS|null
  17. */
  18. protected $fs = null;
  19. /**
  20. * @var null
  21. */
  22. protected $indexName = null;
  23. /**
  24. * @var null
  25. */
  26. protected $rfName = null;
  27. /**
  28. * @var null
  29. */
  30. protected $thumbsCache = null;
  31. /**
  32. * dataTable constructor.
  33. * @param \DocumentParser $modx
  34. * @param bool $debug
  35. */
  36. public function __construct($modx, $debug = false)
  37. {
  38. parent::__construct($modx, $debug);
  39. $this->modx = $modx;
  40. $this->fs = \Helpers\FS::getInstance();
  41. }
  42. /**
  43. * @param $ids
  44. * @param $rid
  45. */
  46. protected function clearIndexes($ids, $rid)
  47. {
  48. $ids = $this->cleanIDs($ids, ',', array(0));
  49. $ids = $this->sanitarIn($ids);
  50. $table = $this->makeTable($this->table);
  51. $rows = $this->query("SELECT MIN(`{$this->indexName}`) FROM {$table} WHERE `{$this->pkName}` IN ({$ids})");
  52. $index = $this->modx->db->getValue($rows);
  53. $index = $index - 1;
  54. $this->query("SET @index := " . $index);
  55. $this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->indexName}`>{$index} AND `{$this->rfName}`={$rid} AND `{$this->pkName}` NOT IN ({$ids})) ORDER BY `{$this->indexName}` ASC");
  56. $out = $this->modx->db->getAffectedRows();
  57. return $out;
  58. }
  59. /**
  60. * @param $field
  61. * @return $this
  62. */
  63. public function touch($field)
  64. {
  65. $this->set($field, date('Y-m-d H:i:s', time() + $this->modx->config['server_offset_time']));
  66. return $this;
  67. }
  68. /**
  69. * @param $ids
  70. * @param $dir
  71. * @param $rid
  72. */
  73. public function place($ids, $dir, $rid)
  74. {
  75. $table = $this->makeTable($this->table);
  76. $ids = $this->cleanIDs($ids, ',', array(0));
  77. if (empty($ids) || is_scalar($ids)) {
  78. return false;
  79. }
  80. $rows = $this->query("SELECT count(`{$this->pkName}`) FROM {$table} WHERE `{$this->rfName}`={$rid}");
  81. $index = $this->modx->db->getValue($rows);
  82. $cnt = count($ids);
  83. $ids = implode(',', $ids);
  84. if ($dir == 'top') {
  85. $this->query("SET @index := " . ($index - $cnt - 1));
  86. $this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` IN ({$ids})) ORDER BY `{$this->indexName}` ASC");
  87. $this->query("SET @index := -1");
  88. } else {
  89. $this->query("SET @index := -1");
  90. $this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` IN ({$ids})) ORDER BY `{$this->indexName}` ASC");
  91. $this->query("SET @index := " . ($cnt - 1));
  92. }
  93. $this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` NOT IN ({$ids})) AND `{$this->rfName}` = {$rid} ORDER BY `{$this->indexName}` ASC");
  94. $out = $this->modx->db->getAffectedRows();
  95. return $out;
  96. }
  97. /**
  98. * @param $url
  99. * @param bool $cache
  100. */
  101. public function deleteThumb($url, $cache = false)
  102. {
  103. $url = $this->fs->relativePath($url);
  104. if (empty($url)) {
  105. return;
  106. }
  107. if ($this->fs->checkFile($url)) {
  108. unlink(MODX_BASE_PATH . $url);
  109. }
  110. $dir = $this->fs->takeFileDir($url);
  111. $iterator = new \FilesystemIterator($dir);
  112. if (! $iterator->valid()) {
  113. rmdir($dir);
  114. }
  115. if ($cache) {
  116. return;
  117. }
  118. $thumbsCache = \APIhelpers::getkey($this->params, 'thumbsCache', $this->thumbsCache);
  119. $thumb = $thumbsCache . $url;
  120. if ($this->fs->checkFile($thumb)) {
  121. $this->deleteThumb($thumb, true);
  122. }
  123. }
  124. /**
  125. * @param $ids
  126. * @param bool $fire_events
  127. * @return $this
  128. */
  129. public function delete($ids, $fire_events = false)
  130. {
  131. $out = parent::delete($ids, $fire_events);
  132. $this->query("ALTER TABLE {$this->makeTable($this->table)} AUTO_INCREMENT = 1");
  133. return $out;
  134. }
  135. /**
  136. * @param $ids
  137. * @param $rid
  138. * @param bool $fire_events
  139. * @return $this
  140. */
  141. public function deleteAll($ids, $rid, $fire_events = false)
  142. {
  143. $this->clearIndexes($ids, $rid);
  144. return $this->delete($ids, $fire_events);
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function fieldNames()
  150. {
  151. $fields = array_keys($this->getDefaultFields());
  152. $fields[] = $this->fieldPKName();
  153. return $fields;
  154. }
  155. /**
  156. * @param string $name
  157. * @return string
  158. */
  159. public function stripName($name)
  160. {
  161. $filename = $this->fs->takeFileName($name);
  162. $ext = $this->fs->takeFileExt($name);
  163. return $this->modx->stripAlias($filename) . '.' . $ext;
  164. }
  165. /**
  166. * @param $source
  167. * @param $target
  168. * @param $point
  169. * @param $rid
  170. * @param $orderDir
  171. * @return int|void
  172. */
  173. public function reorder($source, $target, $point, $rid, $orderDir)
  174. {
  175. $rid = (int)$rid;
  176. $point = strtolower($point);
  177. $orderDir = strtolower($orderDir);
  178. $sourceIndex = (int)$source[$this->indexName];
  179. $targetIndex = (int)$target[$this->indexName];
  180. $sourceId = (int)$source[$this->pkName];
  181. $table = $this->makeTable($this->table);
  182. $rows = 0;
  183. /* more refactoring needed */
  184. if ($targetIndex < $sourceIndex) {
  185. if (($point == 'top' && $orderDir == 'asc') || ($point == 'bottom' && $orderDir == 'desc')) {
  186. $this->modx->db->update(
  187. "`{$this->indexName}`=`{$this->indexName}`+1",
  188. $table,
  189. "`{$this->indexName}`>={$targetIndex} AND `{$this->indexName}`<{$sourceIndex} AND `{$this->rfName}`={$rid}"
  190. );
  191. $rows = $this->modx->db->update(
  192. "`{$this->indexName}`={$targetIndex}",
  193. $table,
  194. "`{$this->pkName}`={$sourceId}"
  195. );
  196. } elseif (($point == 'bottom' && $orderDir == 'asc') || ($point == 'top' && $orderDir == 'desc')) {
  197. $this->modx->db->update(
  198. "`{$this->indexName}`=`{$this->indexName}`+1",
  199. $table,
  200. "`{$this->indexName}`>{$targetIndex} AND `{$this->indexName}`<{$sourceIndex} AND `{$this->rfName}`={$rid}"
  201. );
  202. $rows = $this->modx->db->update(
  203. "`{$this->indexName}`=1+{$targetIndex}",
  204. $table,
  205. "`{$this->pkName}`={$sourceId}"
  206. );
  207. }
  208. } else {
  209. if (($point == 'bottom' && $orderDir == 'asc') || ($point == 'top' && $orderDir == 'desc')) {
  210. $this->modx->db->update(
  211. "`{$this->indexName}`=`{$this->indexName}`-1",
  212. $table,
  213. "`{$this->indexName}`<={$targetIndex} AND `{$this->indexName}`>={$sourceIndex} AND `{$this->rfName}`={$rid}"
  214. );
  215. $rows = $this->modx->db->update(
  216. "`{$this->indexName}`={$targetIndex}",
  217. $table,
  218. "`{$this->pkName}`={$sourceId}"
  219. );
  220. } elseif (($point == 'top' && $orderDir == 'asc') || ($point == 'bottom' && $orderDir == 'desc')) {
  221. $this->modx->db->update(
  222. "`{$this->indexName}`=`{$this->indexName}`-1",
  223. $table,
  224. "`{$this->indexName}`<{$targetIndex} AND `{$this->indexName}`>={$sourceIndex} AND `{$this->rfName}`={$rid}"
  225. );
  226. $rows = $this->modx->db->update(
  227. "`{$this->indexName}`=-1+{$targetIndex}",
  228. $table,
  229. "`{$this->pkName}`={$sourceId}"
  230. );
  231. }
  232. }
  233. return $rows;
  234. }
  235. /**
  236. * @param $folder
  237. * @param $url
  238. * @param $options
  239. * @return bool
  240. */
  241. public function makeThumb($folder, $url, $options)
  242. {
  243. if (empty($url)) {
  244. return false;
  245. }
  246. $thumb = new \Helpers\PHPThumb();
  247. $inputFile = MODX_BASE_PATH . $this->fs->relativePath($url);
  248. $outputFile = MODX_BASE_PATH . $this->fs->relativePath($folder) . '/' . $this->fs->relativePath($url);
  249. $dir = $this->fs->takeFileDir($outputFile);
  250. $this->fs->makeDir($dir, $this->modx->config['new_folder_permissions']);
  251. if ($thumb->create($inputFile, $outputFile, $options)) {
  252. return true;
  253. } else {
  254. $this->modx->logEvent(0, 3, $thumb->debugMessages, __NAMESPACE__);
  255. return false;
  256. }
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function getParams()
  262. {
  263. return $this->params;
  264. }
  265. /**
  266. * @param array $params
  267. */
  268. public function setParams($params = array())
  269. {
  270. if (is_array($params)) {
  271. $this->params = $params;
  272. }
  273. }
  274. }