70c139fed11cd09060fbfbc1d5287117.resolve.albumfiles.resolver 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Gallery
  4. *
  5. * Copyright 2010-2012 by Shaun McCormick <shaun@modx.com>
  6. *
  7. * Gallery is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Gallery is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Gallery; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package gallery
  21. */
  22. /**
  23. * Resolve changes to structure that files are stored in
  24. *
  25. * @package gallery
  26. * @subpackage build
  27. */
  28. if ($object->xpdo) {
  29. switch ($options[xPDOTransport::PACKAGE_ACTION]) {
  30. case xPDOTransport::ACTION_INSTALL:
  31. case xPDOTransport::ACTION_UPGRADE:
  32. $modx =& $object->xpdo;
  33. $modelPath = $modx->getOption('gallery.core_path',null,$modx->getOption('core_path').'components/gallery/').'model/';
  34. $modx->addPackage('gallery',$modelPath);
  35. $modx->getCacheManager();
  36. $results = array();
  37. $galleryFileStructureVersion = (float)$modx->getOption('gallery.file_structure_version',NULL,0);
  38. if ($galleryFileStructureVersion < 1) {
  39. $c = $modx->newQuery('galItem');
  40. $c->select(array(
  41. 'galItem.*',
  42. 'AlbumItems.album',
  43. ));
  44. $c->innerJoin('galAlbumItem','AlbumItems');
  45. $c->sortby('album','DESC');
  46. $c->sortby('filename','DESC');
  47. $items = $modx->getCollection('galItem',$c);
  48. $basePath = $modx->getOption('gallery.files_path');
  49. $currentAlbum = 0;
  50. $albumPath = '';
  51. foreach ($items as $item) {
  52. $oldFullPath = $basePath.$item->get('filename');
  53. if (!file_exists($oldFullPath)) continue;
  54. if ($item->get('album') != $currentAlbum) {
  55. $albumRelativePath = $item->get('album').'/';
  56. $albumFullPath = $basePath.$albumRelativePath;
  57. /* if directory doesn't exist, create it */
  58. if (!file_exists($albumFullPath) || !is_dir($albumFullPath)) {
  59. if (!$modx->cacheManager->writeTree($albumFullPath)) {
  60. $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not create directory: '.$albumFullPath);
  61. continue;
  62. }
  63. }
  64. /* make sure directory is readable/writable */
  65. if (!is_readable($albumFullPath) || !is_writable($albumFullPath)) {
  66. $modx->log(xPDO::LOG_LEVEL_ERROR,'[Gallery] Could not write to directory: '.$albumFullPath);
  67. continue;
  68. }
  69. $currentAlbum = $item->get('album');
  70. }
  71. /* calculate new file paths */
  72. $ext = pathinfo($oldFullPath,PATHINFO_EXTENSION);
  73. $newFileName = $albumRelativePath.$item->get('id').'.'.$ext;
  74. $newFullPath = $basePath.$newFileName;
  75. /* already moved? */
  76. if ($newFileName == $item->get('filename')) continue;
  77. /* move old file to this new location */
  78. if (@copy($oldFullPath,$newFullPath)) {
  79. $item->set('filename',$newFileName);
  80. $item->save();
  81. /* remove old file */
  82. @unlink($oldFullPath);
  83. }
  84. $results[] = array(
  85. 'oldFullPath' => $oldFullPath,
  86. 'newFullPath' => $newFullPath,
  87. 'newFileName' => $newFileName,
  88. );
  89. }
  90. /* create structure version to prevent script from running again */
  91. $setting = $modx->getObject('modSystemSetting',array('key' => 'gallery.file_structure_version'));
  92. if (!$setting) {
  93. $setting = $modx->newObject('modSystemSetting');
  94. $setting->set('key','gallery.file_structure_version');
  95. $setting->set('namespace','gallery');
  96. $setting->set('xtype','textfield');
  97. $setting->set('area','system');
  98. }
  99. $setting->set('value','1.0');
  100. $setting->save();
  101. }
  102. break;
  103. }
  104. }
  105. return true;