imagine_cms 4.2.4 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +4 -0
  3. data/.ruby-version +1 -1
  4. data/Dockerfile +61 -0
  5. data/README.md +50 -0
  6. data/app/assets/javascripts/imagine_cms/core.js +4 -2
  7. data/app/assets/javascripts/imagine_cms/legacy/misc.js +29 -31
  8. data/app/controllers/cms/content_controller.rb +29 -25
  9. data/app/controllers/manage/application_controller.rb +4 -0
  10. data/app/controllers/manage/cms_controller.rb +13 -0
  11. data/app/controllers/{management/cms_controller.rb → manage/cms_pages_controller.rb} +625 -711
  12. data/app/controllers/manage/cms_snippets_controller.rb +67 -0
  13. data/app/controllers/manage/cms_templates_controller.rb +71 -0
  14. data/app/controllers/management/application_controller.rb +1 -1
  15. data/app/controllers/management/user_controller.rb +7 -6
  16. data/app/controllers/management/users_controller.rb +56 -56
  17. data/app/controllers/util_controller.rb +0 -1
  18. data/app/helpers/cms_application_helper.rb +17 -19
  19. data/app/mailers/imagine_cms_mailer.rb +16 -0
  20. data/app/models/cms_page.rb +8 -23
  21. data/app/models/user.rb +1 -1
  22. data/app/sweepers/cms_content_sweeper.rb +3 -3
  23. data/app/views/cms/content/_photo_gallery.html.erb +4 -4
  24. data/app/views/cms/content/_search_result.html.erb +1 -1
  25. data/app/views/imagine_cms/_header.html.erb +1 -1
  26. data/app/views/imagine_cms/_toolbar.html.erb +12 -12
  27. data/app/views/imagine_cms_mailer/request_review.text.erb +10 -0
  28. data/app/views/layouts/management.html.erb +29 -5
  29. data/app/views/manage/cms/index.html.erb +7 -0
  30. data/app/views/{management/cms → manage/cms_pages}/_complete_gallery.html.erb +0 -0
  31. data/app/views/{management/cms → manage/cms_pages}/_create_file_link.html.erb +0 -0
  32. data/app/views/{management/cms → manage/cms_pages}/_crop_feature_image.html.erb +20 -5
  33. data/app/views/{management/cms → manage/cms_pages}/_crop_image.html.erb +20 -5
  34. data/app/views/{management/cms → manage/cms_pages}/_crop_results.html.erb +0 -0
  35. data/app/views/manage/cms_pages/_crop_results_feature_image.html.erb +1 -0
  36. data/app/views/manage/cms_pages/_crop_results_thumb.html.erb +1 -0
  37. data/app/views/{management/cms → manage/cms_pages}/_crop_thumb.html.erb +20 -5
  38. data/app/views/{management/cms → manage/cms_pages}/_edit_page.html.erb +37 -49
  39. data/app/views/{management/cms → manage/cms_pages}/_gallery_index.html.erb +0 -0
  40. data/app/views/{management/cms → manage/cms_pages}/_gallery_setup.html.erb +0 -0
  41. data/app/views/{management/cms → manage/cms_pages}/_image.html.erb +0 -0
  42. data/app/views/{management/cms → manage/cms_pages}/_image_details.html.erb +0 -0
  43. data/app/views/{management/cms → manage/cms_pages}/_image_draggable.html.erb +0 -0
  44. data/app/views/manage/cms_pages/_list_page.html.erb +8 -0
  45. data/app/views/{management/cms → manage/cms_pages}/_list_page_select.html.erb +0 -0
  46. data/app/views/{management/cms → manage/cms_pages}/_list_pages.html.erb +0 -0
  47. data/app/views/{management/cms → manage/cms_pages}/_list_pages_select.html.erb +0 -0
  48. data/app/views/{management/cms → manage/cms_pages}/_page_attribute.html.erb +0 -0
  49. data/app/views/{management/cms → manage/cms_pages}/_page_list.html.erb +2 -2
  50. data/app/views/{management/cms → manage/cms_pages}/_page_list_source_folder.html.erb +0 -0
  51. data/app/views/{management/cms → manage/cms_pages}/_page_list_source_tag.html.erb +0 -0
  52. data/app/views/{management/cms → manage/cms_pages}/_select_gallery.html.erb +0 -0
  53. data/app/views/{management/cms → manage/cms_pages}/_sort_images.html.erb +0 -0
  54. data/app/views/{management/cms → manage/cms_pages}/_template_options.html.erb +3 -3
  55. data/app/views/{management/cms → manage/cms_pages}/_upload_feature_image.html.erb +0 -0
  56. data/app/views/{management/cms → manage/cms_pages}/_upload_file.html.erb +0 -0
  57. data/app/views/{management/cms → manage/cms_pages}/_upload_image.html.erb +1 -1
  58. data/app/views/{management/cms → manage/cms_pages}/_upload_thumb.html.erb +0 -0
  59. data/app/views/{management/cms → manage/cms_pages}/edit_page_content.html.erb +7 -0
  60. data/app/views/{management/cms → manage/cms_pages}/gallery_management.html.erb +1 -1
  61. data/app/views/{management/cms/pages.html.erb → manage/cms_pages/index.html.erb} +1 -1
  62. data/app/views/{management/cms/page_tags_for_lookup.html.erb → manage/cms_pages/page_tags_for_lookup.js.erb} +0 -0
  63. data/app/views/{management/cms → manage/cms_pages}/select_page.html.erb +0 -0
  64. data/app/views/{management/cms → manage/cms_pages}/toolbar_edit.html.erb +0 -0
  65. data/app/views/{management/cms → manage/cms_pages}/toolbar_preview.html.erb +6 -6
  66. data/app/views/manage/cms_snippets/_cms_snippet.html.erb +3 -0
  67. data/app/views/manage/cms_snippets/edit.html.erb +25 -0
  68. data/app/views/{management/cms/snippets.html.erb → manage/cms_snippets/index.html.erb} +2 -2
  69. data/app/views/manage/cms_templates/_cms_template.html.erb +3 -0
  70. data/app/views/{management/cms → manage/cms_templates}/_template_reference.html.erb +2 -2
  71. data/app/views/manage/cms_templates/edit.html.erb +26 -0
  72. data/app/views/{management/cms/templates.html.erb → manage/cms_templates/index.html.erb} +2 -2
  73. data/app/views/management/default/index.html.erb +3 -1
  74. data/app/views/management/users/edit.html.erb +2 -2
  75. data/app/views/management/users/index.html.erb +2 -2
  76. data/app/views/management/users/new.html.erb +1 -1
  77. data/config/initializers/constants.rb +3 -0
  78. data/config/routes.rb +57 -10
  79. data/db/migrate/20121108004747_add_settings_table.rb +8 -12
  80. data/db/migrate/20121108005110_add_tasks_table.rb +7 -11
  81. data/db/migrate/20121115083748_add_logs_table.rb +13 -18
  82. data/db/migrate/20121115083811_add_users_tables.rb +26 -26
  83. data/db/migrate/20121115084028_add_cms_tables.rb +91 -90
  84. data/db/migrate/20140423085357_add_redirect_fields_to_cms_pages.rb +2 -2
  85. data/db/migrate/20140423085358_add_redirect_fields_to_cms_page_versions.rb +2 -2
  86. data/docker-compose.override.yml.example +43 -0
  87. data/docker-compose.yml +21 -0
  88. data/docker/conf/.my.cnf +3 -0
  89. data/docker/conf/my.local.cnf +14 -0
  90. data/docker/conf/my.prod.cnf +9 -0
  91. data/docker/conf/nginx-vhost.conf +14 -0
  92. data/docker/services/memcached.sh +3 -0
  93. data/docker/services/sidekiq.sh +3 -0
  94. data/docker/services/webpack.sh +3 -0
  95. data/docker/startup/101_mkdir.sh +15 -0
  96. data/docker/startup/201_bundler.sh +4 -0
  97. data/docker/startup/211_yarn.sh +4 -0
  98. data/imagine_cms.gemspec +12 -11
  99. data/lib/extensions/action_controller_extensions.rb +4 -2
  100. data/lib/imagine_cms/engine.rb +6 -7
  101. data/lib/imagine_cms/version.rb +1 -1
  102. data/lib/upload_progress/lib/upload_progress.rb +3 -3
  103. data/test/dummy/app/assets/javascripts/application.js +0 -1
  104. metadata +94 -74
  105. data/.ruby-gemset +0 -1
  106. data/README.rdoc +0 -98
  107. data/app/views/management/cms/_crop_results_feature_image.html.erb +0 -1
  108. data/app/views/management/cms/_crop_results_thumb.html.erb +0 -1
  109. data/app/views/management/cms/_list_page.html.erb +0 -8
  110. data/app/views/management/cms/_snippet.html.erb +0 -3
  111. data/app/views/management/cms/_temp.html.erb +0 -3
  112. data/app/views/management/cms/edit_master.html.erb +0 -48
  113. data/app/views/management/cms/edit_snippet.html.erb +0 -25
  114. data/app/views/management/cms/edit_template.html.erb +0 -25
  115. data/app/views/management/cms/index.html.erb +0 -7
  116. data/app/views/management/cms/permission_denied.html.erb +0 -1
  117. data/lib/hash_object.rb +0 -39
  118. data/lib/hash_wrapper.rb +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d5f39688f4ce0db4088dcb9a9fd335d8559cc8b2
4
- data.tar.gz: f17c64bc6b869b8bb6ebd43f0ccb8f4a40ea8569
2
+ SHA256:
3
+ metadata.gz: 6a2d2087d9caf255378b1213b70ef79ce96674fd68158faa3d175a0efcc49bcb
4
+ data.tar.gz: 3d26b91a3140cc36df6ce24dcd8aa0c8042d178a1596d152990bab1f8da6aba4
5
5
  SHA512:
6
- metadata.gz: 1c346d09b43132531d39927c97f3747099980b5fb662900897ea0252975e2380192818aef42705f24be8579ec5dfb85d87c4c18249ce477f726143a6151bf990
7
- data.tar.gz: 3b5953c978eb5b1de34ef12e8ab469689791571d38ecfd89c246b610762f883c5f6ae4ee677ec0ae24c5a0f86b7f1083a995a51a0c96ddf229b1bca2053adf3e
6
+ metadata.gz: 4cd0d6ba6f816b5d8a4de2f63f5947bac71a4ee02ece4fa26af8d79db2b87c5d2360d7eb5f863ddb0667615e41731473a8d77d6ea0b3c5f013e751d8b86bd0ab
7
+ data.tar.gz: 28b07e811ffd01c9df9db417f75bfc782f44c72779441a2a70aab854c59d2f222ad4433d8492a7d0ff94b4c47b7f1efbe2882a20fbde7e4b9f78cf69fba197d6
data/.gitignore CHANGED
@@ -7,3 +7,7 @@ test/dummy/tmp/
7
7
  test/dummy/.sass-cache
8
8
 
9
9
  *.gem
10
+
11
+ prototype-rails
12
+
13
+ docker-compose.override.yml
@@ -1 +1 @@
1
- 2.2.4
1
+ 2.5.1
@@ -0,0 +1,61 @@
1
+ ### Docker image for imagine5 ###
2
+
3
+ # See https://github.com/phusion/passenger-docker/blob/master/Changelog.md for a list of version numbers.
4
+ FROM phusion/passenger-ruby25:1.0.0
5
+ LABEL maintainer="aaron@aaronnamba.com"
6
+
7
+ # Set up 3rd party repos
8
+ RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
9
+ RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
10
+ RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
11
+
12
+ # Update package list
13
+ RUN apt-get update
14
+
15
+ # Upgrade other preinstalled packages
16
+ RUN apt-get upgrade -y -o Dpkg::Options::="--force-confnew"
17
+
18
+ # Install other packages we depend on
19
+ RUN apt-get install -y nodejs yarn
20
+ RUN apt-get install -y tzdata
21
+ RUN apt-get install -y mysql-client
22
+ RUN apt-get install -y libmagickcore-dev libmagickwand-dev # for rmagick2 gem
23
+ RUN apt-get install -y imagemagick # for mini-magick gem
24
+ RUN apt-get install -y openjdk-8-jre-headless # java for fop
25
+ RUN apt-get install -y memcached # for rack-attack and rails cache
26
+ RUN apt-get install -y cmake pkg-config # for rugged (git)
27
+ RUN apt-get autoremove -y
28
+
29
+ # Enable services
30
+ RUN rm -f /etc/service/nginx/down
31
+
32
+ # Add custom services
33
+ RUN mkdir /etc/service/memcached
34
+ COPY docker/services/memcached.sh /etc/service/memcached/run
35
+ # RUN mkdir /etc/service/sidekiq
36
+ # COPY docker/services/sidekiq.sh /etc/service/sidekiq/run
37
+ # RUN mkdir /etc/service/webpack
38
+ # COPY docker/services/webpack.sh /etc/service/webpack/run
39
+
40
+ # Other startup scripts
41
+ RUN mkdir -p /etc/my_init.d
42
+ COPY docker/startup/101_mkdir.sh /etc/my_init.d/
43
+ COPY docker/startup/201_bundler.sh /etc/my_init.d/
44
+ COPY docker/startup/211_yarn.sh /etc/my_init.d/
45
+
46
+ # Remove the default vhost (so that ours will respond to any Host)
47
+ RUN rm -f /etc/nginx/sites-enabled/default
48
+
49
+ # Post-build clean up
50
+ RUN apt-get clean && rm -rf /tmp/* /var/tmp/*
51
+ # RUN rm -rf /var/lib/apt/lists/*
52
+
53
+ # Expose port 80 to the Docker host, so we can access it from the outside (remember to publish it using `docker run -p`).
54
+ EXPOSE 80
55
+
56
+ # For convenience
57
+ WORKDIR /home/app/myapp
58
+ COPY docker/conf/.my.cnf /home/app
59
+
60
+ # Run this to start all services (if no command was provided to `docker run`)
61
+ CMD ["/sbin/my_init"]
@@ -0,0 +1,50 @@
1
+ # Imagine CMS
2
+
3
+ [![Version](https://img.shields.io/github/tag/anamba/imagine_cms.svg?maxAge=360)](https://github.com/anamba/imagine_cms/releases/latest)
4
+ [![License](https://img.shields.io/github/license/anamba/imagine_cms.svg)](https://github.com/anamba/imagine_cms/blob/master/license.txt)
5
+
6
+
7
+ Imagine CMS is a web content management system developed by [Bigger Bird Creative, Inc.](https://biggerbird.com) in 2006 for its clients.
8
+ Other CMSes came with a learning curve: not a problem for daily users, but clients who only used it once a month or so would forget everything by the next time they logged in.
9
+ Simpler systems didn't have enough functionality to allow us to do what we wanted to do as designers and developers.
10
+ Thus, we created a CMS that is easy for clients to use, stays out of our way, and provides useful automation (publishing and unpublishing on a schedule, navigation links, RSS feeds, etc.).
11
+
12
+ ## Current Status
13
+
14
+ Imagine 5.2 is ready for production use (v5.2-stable branch). Imagine 4 is also available for apps tied to Rails 4.2, respectively (v4.2-stable branch).
15
+
16
+ However, unless you are already familiar with Imagine, this project is not suitable for wider use. Why? Well, the purpose of the Imagine gem at this point is strictly to upgrade legacy sites to modern versions of Rails as simply as possible, and to make sure that if something happens to Bigger Bird, our clients will not be left high and dry. This version was never intended to be used by the general public.
17
+
18
+ Imagine 7 will be a clean break and a great time to try Imagine CMS. Depending on how smoothly development goes, it could be released before Imagine 6 (awkward...).
19
+
20
+ ## History and Roadmap
21
+
22
+ Imagine was originally created in the Rails 1.0 days, to run a large number of technically straightforward content-based sites (versus the one or two highly-custom web applications most Rails developers were building at the time). We wanted to provide a way to create and manage static content with a little bit of automation, while also allowing limitless custom Ruby development. However, because plugins/engines were quite limited in those days, Imagine was implemented by monkey-patching core parts of Rails and creating a new framework on top of Rails. Although this allowed us great insight into the inner workings of Rails, this approach proved extremely difficult to port to Rails 2. Thus, sites using the original Imagine 1.x were stuck on Rails 1.x for many years.
23
+
24
+ All that is now firmly in the past. By extracting Imagine functionality into a Rails engine, we achieved compatibility with Rails 3.2 and Ruby 1.9 and removed roadblocks to a future upgrade to Rails 4 and Ruby 2, all while retaining backwards compatibility with the Imagine CMS database structure.
25
+
26
+ (Imagine 1.x and 2.x were internal-only releases, the open source version started at 3.0.)
27
+
28
+ * Imagine 3.0 (Rails 3.2, Ruby 1.9/2.0): [DONE, v3.0-stable] 100% restored functionality (plus a few extras and fixes), no database changes.
29
+ * Imagine 4.0 (Rails 4.0, Ruby 2.1): [DONE, v4.0-stable] Compatibility with Rails 4.0
30
+ * Imagine 4.1 (Rails 4.1, Ruby 2.1): [DONE, v4.1-stable] Compatibility with Rails 4.1
31
+ * Imagine 4.2 (Rails 4.2, Ruby 2.2): [DONE, v4.2-stable] Compatibility with Rails 4.2, many minor fixes and UI improvements
32
+ * Imagine 5.0 (Rails 5.0, Ruby 2.2): [SKIPPED] Compatibility with Rails 5.0
33
+ * Imagine 5.1 (Rails 5.1, Ruby 2.4): [SKIPPED] Compatibility with Rails 5.1
34
+ * **Imagine 5.2 (Rails 5.2, Ruby 2.5): [CURRENT RELEASE] Compatibility with Rails 5.2**
35
+ * Imagine 6 (Rails 6): [FUTURE] Compatibility with Rails 6
36
+ * Imagine 7 (Crystal): [IN PROGRESS] Rewrite in [Crystal](https://crystal-lang.org/) with [Apache CouchDB](http://couchdb.apache.org/) backend
37
+
38
+ Imagine 6 will continue to be Rails-based, but Imagine 7 is a complete, ground-up rewrite in Crystal. Development will begin in a new repository under an Imagine CMS organization, as a proper community-driven open source project (finally!). The core concepts will continue on, but not a single line of code will carry over.
39
+
40
+ ## Hosting
41
+
42
+ Imagine 3.x-6.x can run on typical Rails hosting platforms (anything that uses Passenger, Unicorn, Puma, etc.). On hosts that don't allow writing to the local filesystem (e.g. Heroku) you won't be able to use photo galleries or page caching, but other features should work (note: this mode of operation has not been fully tested).
43
+
44
+ ## Getting Help
45
+
46
+ Get paid support and hosting for Imagine CMS straight from the people who made it: [Bigger Bird Creative, Inc.](https://biggerbird.com) Neither is required to use Imagine CMS, of course.
47
+
48
+ ## Contributing
49
+
50
+ Imagine 7 (Crystal) will be a true open source project, but this project (Ruby-based Imagine) will remain more or less closed for the foreseeable future. If companies or individuals are willing to sponsor or co-develop new features, we can work something out.
@@ -1,9 +1,11 @@
1
1
  jQuery(document).ready(function () {
2
+ window.CodemirrorInstances = [];
2
3
  jQuery('textarea.codemirror-html').each(function () {
3
4
  var cm = CodeMirror.fromTextArea(this, {
4
5
  mode: 'application/x-erb-imagine',
5
6
  lineNumbers: true
6
7
  });
8
+ window.CodemirrorInstances.push(cm);
7
9
  })
8
10
  });
9
11
 
@@ -334,7 +336,7 @@ function showPageBrowser(field_id) {
334
336
  path = $(field_id).value;
335
337
 
336
338
  $('page_browser').innerHTML = 'Loading...'
337
- new Ajax.Updater('page_browser', '/manage/cms/select_page?path=' + path, {asynchronous:true, evalScripts:true});
339
+ new Ajax.Updater('page_browser', '/manage/cms_pages/select_page?path=' + path, {asynchronous:true, evalScripts:true});
338
340
  $('page_browser_selection').value = path;
339
341
  showDojoDialog('page_browser_dialog');
340
342
  }
@@ -396,7 +398,7 @@ function scanForPageObjects(page_id, parent_key, version) {
396
398
  jQuery.each(found, function (key, val) {
397
399
  if (!cmsPageObjects[key]) {
398
400
  cmsPageObjects[key] = val;
399
- jQuery.get('/manage/cms/insert_page_object_config/' + page_id + '?version= ' + version +
401
+ jQuery.get('/manage/cms_pages/' + page_id + '/insert_page_object_config?version= ' + version +
400
402
  '&name=' + key + '&type=' + val + '&parent_key=' + parent_key);
401
403
  }
402
404
  });
@@ -117,78 +117,76 @@ function selectTab(tabName, tabArray, tabBtnPrefix, tabDivPrefix, hiddenTextFiel
117
117
 
118
118
 
119
119
  function setReportDates(interval) {
120
- startDateField = $('report_start_date');
121
- endDateField = $('report_end_date');
122
- today = startDate = endDate = new Date();
120
+ var startDateField = $('report_start_date');
121
+ var endDateField = $('report_end_date');
122
+ var today = new Date();
123
+ var startDate = new Date();
124
+ var endDate = new Date();
123
125
 
124
126
  switch (interval) {
125
127
  case 'yesterday':
126
- startDate = new Date('' + (today.getMonth()+1) + '/' + (today.getDate()-1) + '/' + today.getYear());
128
+ startDate = new Date(today.getYear(), today.getMonth(), today.getDate()-1);
127
129
  endDate = startDate;
128
130
  break;
129
131
 
130
132
  case 'last_month':
131
- startDate = new Date('' + today.getMonth() + '/1/' + today.getYear());
132
- endDate = new Date('' + (today.getMonth()+1) + '/0/' + today.getYear());
133
+ startDate = new Date(today.getYear(), today.getMonth()-1, 1);
134
+ endDate = new Date(today.getYear(), today.getMonth(), 0);
133
135
  break;
134
136
 
135
137
  case 'last_quarter':
136
- startMonth = Math.floor(today.getMonth() / 3) * 3 - 3;
137
- startYear = today.getYear();
138
+ var startMonth = Math.floor(today.getMonth() / 3) * 3 - 3;
139
+ var startYear = today.getYear();
138
140
  if (startMonth < 0) {
139
141
  startMonth += 12;
140
142
  startYear--;
141
143
  }
142
- endMonth = startMonth + 3;
143
- endYear = startYear;
144
+ var endMonth = startMonth + 3;
145
+ var endYear = startYear;
144
146
  if (endMonth > 11) {
145
147
  endMonth -= 12;
146
148
  endYear++;
147
149
  }
148
- startDate = new Date('' + (startMonth+1) + '/1/' + startYear);
149
- endDate = new Date('' + (endMonth+1) + '/0/' + endYear);
150
+ startDate = new Date(startYear, startMonth, 1);
151
+ endDate = new Date(endYear, endMonth, 0);
150
152
  break;
151
153
 
152
154
  case 'last_year':
153
- startDate = new Date('1/1/' + (today.getYear()-1));
154
- endDate = new Date('12/31/' + (today.getYear()-1));
155
+ startDate = new Date(today.getYear()-1, 0, 1);
156
+ endDate = new Date(today.getYear()-1, 11, 31);
155
157
  break;
156
158
 
157
- case 'today':
158
- startDate = new Date('' + (today.getMonth()+1 )+ '/' + today.getDate() + '/' + today.getYear());
159
- endDate = startDate;
160
- break;
159
+ // case 'today': // same as default
161
160
 
162
161
  case 'this_month':
163
- startDate = new Date('' + (today.getMonth()+1) + '/1/' + today.getYear());
164
- endDate = new Date('' + (today.getMonth()+2) + '/0/' + today.getYear());
162
+ startDate = new Date(today.getYear(), today.getMonth(), 1);
163
+ endDate = new Date(today.getYear(), today.getMonth()+1, 0);
165
164
  break;
166
165
 
167
166
  case 'this_quarter':
168
- startMonth = Math.floor(today.getMonth() / 3) * 3;
169
- endMonth = startMonth + 3;
170
- endYear = startYear;
167
+ var startMonth = Math.floor(today.getMonth() / 3) * 3;
168
+ var endMonth = startMonth + 3;
169
+ var endYear = today.getYear();
171
170
  if (endMonth > 11) {
172
171
  endMonth -= 12;
173
172
  endYear++;
174
173
  }
175
- startDate = new Date('' + (startMonth+1) + '/1/' + today.getYear());
176
- endDate = new Date('' + (endMonth+1) + '/0/' + endYear);
174
+ startDate = new Date(today.getYear(), startMonth, 1);
175
+ endDate = new Date(endYear, endMonth, 0);
177
176
  break;
178
177
 
179
178
  case 'this_year':
180
- startDate = new Date('1/1/' + today.getYear());
181
- endDate = new Date('12/31/' + today.getYear());
179
+ startDate = new Date(today.getYear(), 0, 1);
180
+ endDate = new Date(today.getYear(), 11, 31);
182
181
  break;
183
182
 
184
183
  case 'all_time':
185
- startDate = new Date('1/1/' + (today.getYear()-5));
186
- endDate = new Date('12/31/' + (today.getYear()+50));
184
+ startDate = new Date(100, 0, 1);
185
+ endDate = new Date(1099, 11, 31);
187
186
  break;
188
187
 
189
188
  default:
190
- // same as "today"
191
- startDate = new Date('' + (today.getMonth()+1 )+ '/' + today.getDate() + '/' + today.getYear());
189
+ startDate = new Date(today.getYear(), today.getMonth(), today.getDate());
192
190
  endDate = startDate;
193
191
  break;
194
192
  }
@@ -6,7 +6,7 @@ module Cms # :nodoc:
6
6
 
7
7
  caches_action :rss_feed
8
8
 
9
- before_filter :convert_content_path
9
+ before_action :convert_content_path
10
10
 
11
11
  # Routes:
12
12
  # match 'plcalendar(/:action(/:id))' => 'cms/content#page_list_calendar'
@@ -34,7 +34,7 @@ module Cms # :nodoc:
34
34
  params[:page] = 'index'
35
35
  template_found = true
36
36
  end
37
- rescue Exception => e
37
+ rescue StandardError => e
38
38
  if e.message =~ /string contains null byte/
39
39
  # do nothing
40
40
  else
@@ -149,9 +149,9 @@ module Cms # :nodoc:
149
149
  params[:page] = 'index'
150
150
  end
151
151
 
152
- if @pg = CmsPage.includes(:template).find_by_path(db_path.join('/'))
152
+ if @pg ||= (CmsPage.includes(:template).find_by_path(db_path.join('/')) || CmsPage.includes(:template).find_by_path(db_path.map { |segment| segment.gsub(/([A-Za-z\d])_/, '\1-') }.join('/')))
153
153
  if edit_mode
154
- redirect_to controller: '/management/cms', action: 'edit_page_content', id: @pg and return true
154
+ redirect_to controller: '/manage/cms_pages', action: 'edit_page_content', id: @pg and return true
155
155
  else
156
156
  # return if page is offline and viewer is not an admin
157
157
  if @pg.published_version < 0
@@ -197,15 +197,17 @@ module Cms # :nodoc:
197
197
 
198
198
  render inline: template_content
199
199
 
200
- if UseCmsPageCaching && @allow_caching && perform_caching && request.format == Mime::HTML
201
- cache_page
200
+ if UseCmsPageCaching && @allow_caching && perform_caching && request.format == Mime[:html]
201
+ cache_page(nil, nil, nil)
202
202
  end
203
203
 
204
204
  return true
205
205
  end
206
206
  end
207
- rescue Exception => e
208
- logger.error "Error rendering from db: #{e.class}: #{e.message}"
207
+ rescue ActionController::RoutingError
208
+ # no need to log this
209
+ rescue StandardError => e
210
+ logger.error "Error rendering from db: #{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
209
211
  rendering_error(e) and return true
210
212
  end
211
213
 
@@ -294,7 +296,7 @@ module Cms # :nodoc:
294
296
  end
295
297
 
296
298
  @pages.each_with_index do |page, index|
297
- @page_contents[page.id] = render_to_string :inline => substitute_placeholders(@page_objects["#{key}-template"] || options[:template] || '', page, :index => index+1, :count => @pages.size)
299
+ @page_contents[page.id] = render_to_string :inline => substitute_placeholders(@page_objects["#{key}-template"] || '', page, :index => index+1, :count => @pages.size)
298
300
  end
299
301
  end
300
302
 
@@ -307,11 +309,16 @@ module Cms # :nodoc:
307
309
 
308
310
  def preview_template
309
311
  @pg = CmsPage.new
310
- @pg.template = CmsTemplate.new
311
- @pg.template.name = (params[:temp] || params[:snip])[:name] || 'New Template'
312
- @pg.template.content = (params[:temp] || params[:snip])[:content]
313
- @page_objects = HashObject.new
314
- render :inline => substitute_placeholders(@pg.template.content, @pg), :layout => 'application'
312
+ if params[:id] && (@cms_template = CmsTemplate.find_by_id(params[:id])) && (pg = @cms_template.pages.where('published_version >= 0').order('updated_on desc').first)
313
+ @pg = pg
314
+ else
315
+ @cms_template = CmsTemplate.new(name: 'Template Preview')
316
+ @pg.template = @cms_template
317
+ end
318
+ @page_objects = OpenStruct.new
319
+ @pg.template.content = params[:content]
320
+
321
+ render inline: render_cms_page_to_string(@pg, params[:content])
315
322
  end
316
323
 
317
324
  def page_list_calendar
@@ -358,28 +365,25 @@ module Cms # :nodoc:
358
365
 
359
366
  protected
360
367
 
361
- def render_cms_page_to_string(page)
368
+ def render_cms_page_to_string(page, template_content = nil)
369
+ template_content = (substitute_placeholders(template_content || page.template.content, page)) or return nil
370
+
362
371
  # sanitize possibly dangerous content before rendering
363
- template_content = substitute_placeholders(page.template.content, page)
364
372
  template_content.gsub!(/<(%.*?(exec|system)\s?\(.*?\s*%)>/, '&lt;\1&gt;')
365
373
  template_content.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '&lt;\1&gt;')
366
374
  template_content.gsub!(/<(%.*?\`.*?\s*%)>/, '&lt;\1&gt;')
367
375
 
368
- # silence do
369
- template_content = render_to_string(:inline => template_content,
370
- :locals => { :page => page, :safe_level => 0 })
371
- # end
376
+ template_content = render_to_string(inline: template_content,
377
+ locals: { page: page, safe_level: 0 })
372
378
 
379
+ # do it all over again one more time to get the second level stuff (usually within page lists)
373
380
  template_content = substitute_placeholders(template_content, page)
374
381
  template_content.gsub!(/<(%.*?(exec|system)\s?\(.*?\s*%)>/, '&lt;\1&gt;')
375
382
  template_content.gsub!(/<(%.*?\%x\s?\[.*?\s*%)>/, '&lt;\1&gt;')
376
383
  template_content.gsub!(/<(%.*?\`.*?\s*%)>/, '&lt;\1&gt;')
377
384
 
378
- # silence do
379
- template_content = render_to_string(:inline => template_content,
380
- :layout => 'application',
381
- :locals => { :page => page })
382
- # end
385
+ template_content = render_to_string(inline: template_content, layout: true,
386
+ locals: { page: page })
383
387
 
384
388
  template_content
385
389
  end
@@ -0,0 +1,4 @@
1
+ class Manage::ApplicationController < ApplicationController
2
+ before_action :authenticate_user
3
+ layout 'management'
4
+ end
@@ -0,0 +1,13 @@
1
+ class Manage::CmsController < Manage::ApplicationController
2
+ before_action :check_permissions
3
+
4
+ protected
5
+
6
+ def check_permissions
7
+ if !user_has_permission?(:manage_cms)
8
+ render '/imagine_cms/errors/permission_denied', :layout => false
9
+ return false
10
+ end
11
+ end
12
+
13
+ end
@@ -1,151 +1,30 @@
1
- class Management::CmsController < Management::ApplicationController # :nodoc:
1
+ class Manage::CmsPagesController < Manage::ApplicationController
2
2
  include ActionController::Caching::Pages
3
3
  self.page_cache_directory = "#{Rails.root}/public"
4
4
 
5
- before_filter :check_permissions
6
- before_filter :block_basic_users, :except => [
7
- :index, :edit_page_content,
8
- :disable_caching, :garbage_collect,
9
- :select_page, :list_pages_select, :request_review,
10
- :toolbar_preview, :toolbar_edit,
11
-
12
- :create_file_link, :upload_file, :receive_file,
13
-
14
- :upload_image, :receive_image, :crop_image, :save_crop, :upload_status,
15
- :upload_thumb, :crop_thumb, :save_crop_thumb,
16
- :upload_feature_image, :crop_feature_image, :save_crop_feature_image,
17
-
18
- :receive_gallery, :complete_gallery, :gallery_setup, :add_to_gallery,
19
- :gallery_management, :select_gallery, :set_gallery_order, :save_gallery_settings,
20
- :sort_images, :sort_images_save,
21
- :image_details, :update_caption,
22
- :delete_photo, :delete_gallery,
23
-
24
- :pages, :list_pages, :edit_page, :show_template_options, :page_attribute, :set_page_version
25
- ]
26
-
27
- before_filter :convert_invalid_chars_in_params
5
+ before_action :check_permissions
6
+ before_action :convert_invalid_chars_in_params
28
7
 
29
8
  upload_status_for :receive_image
9
+ upload_status_for :receive_gallery
30
10
  upload_status_for :add_to_gallery
31
11
 
32
12
  cache_sweeper :cms_content_sweeper
33
13
 
34
- def check_permissions
35
- if !user_has_permission?(:manage_cms)
36
- render '/imagine_cms/errors/permission_denied', :layout => false
37
- return false
38
- end
39
- end
40
-
41
- def block_basic_users
42
- return true unless UseCmsAccessLevels
43
- unless user_has_permission?(:manage_cms_full_access) && @user.cms_allowed_sections.to_s.strip.blank?
44
- render '/imagine_cms/errors/permission_denied'
45
- return false
46
- end
47
- end
48
-
49
- def validate_user_access
50
- unless @user.cms_allowed_sections.to_s.strip.blank?
51
- allowed_sections = @user.cms_allowed_sections.split(',').map { |s| s.strip }.reject { |s| s.blank? }
52
- if @pg
53
- path = '/' + @pg.path
54
- else
55
- parent = CmsPage.find_by_id(params[:parent_id] || params[:pg][:parent_id]) rescue nil
56
- return false if !parent
57
- path = '/' + parent.path
58
- end
59
-
60
- allowed = false
61
- allowed_sections.each { |s| allowed ||= (path =~ /^#{s}/) }
62
-
63
- if !allowed
64
- respond_to do |wants|
65
- wants.js { render :text => "Sorry, you don't have permission to edit this page." }
66
- wants.html { redirect_to "/#{@pg.path}#{@pg.path == '' ? '' : '/'}version/#{@pg.version}" }
67
- end
68
- return false
69
- end
70
- end
71
-
72
- true
73
- end
74
-
75
14
 
76
15
  def index
77
- end
78
-
79
- def templates
80
- @temps = CmsTemplate.order(:name)
81
- end
82
-
83
- def edit_template
84
- @temp = CmsTemplate.find_by_id(params[:id]) || CmsTemplate.new
85
-
86
- if request.post?
87
- @temp.assign_attributes(cms_template_params)
88
-
89
- # begin
90
- @pg = CmsPage.new
91
- @page_objects = HashObject.new
92
- render_to_string :inline => @temp.content
93
- # rescue Exception => e
94
- # message = e.message
95
- # flash.now[:error] = "<pre>#{ERB::Util.html_escape(message)}</pre>".html_safe
96
- # logger.debug e
97
- # return
98
- # end
99
-
100
- # this must come after the render_to_string so that we capture template
101
- # options embedded in snippets
102
- @temp.options = @template_options
103
-
104
- if !@temp.save
105
- flash.now[:error] = @temp.errors.full_messages.join('<br/>')
106
- else
107
- flash[:notice] = 'Template saved.'
108
- redirect_to :action => 'edit_template', :id => @temp.id and return
109
- end
110
- end
111
- end
112
-
113
- def snippets
114
- @snippets = CmsSnippet.order(:name)
115
- end
116
-
117
- def edit_snippet
118
- @snip = CmsSnippet.find_by_id(params[:id]) || CmsSnippet.new
119
-
120
- if request.post?
121
- @snip.assign_attributes(cms_snippet_params)
122
-
123
- begin
124
- @pg = CmsPage.new
125
- @page_objects = HashObject.new
126
- render_to_string :inline => @snip.content
127
- rescue Exception => e
128
- message = e.message
129
- flash.now[:error] = "<pre>#{ERB::Util.html_escape(message)}</pre>".html_safe
130
- logger.debug e
131
- return
132
- end
133
-
134
- if !@snip.save
135
- @error = @snip.errors.full_messages.join('<br/>')
136
- else
137
- flash[:notice] = 'Snippet saved.'
138
- redirect_to :action => 'edit_snippet', :id => @snip.id and return
139
- end
140
- end
141
- end
142
-
143
- def pages
144
16
  @page_levels = [ '' ].concat((params[:path] || session[:cms_pages_path] || '').split('/').reject { |l| l.blank? })
145
17
  @page_levels << ''
146
18
  @path = ''
147
19
  @page = nil
148
20
  end
21
+
22
+ def new
23
+ validate_user_access or return
24
+ @pg ||= CmsPage.new
25
+
26
+ edit_page
27
+ end
149
28
 
150
29
  def list_pages
151
30
  @page_level = params[:level].to_i
@@ -176,8 +55,8 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
176
55
  @pg.template ||= @parent.template
177
56
  end
178
57
 
179
- @attrs = CmsPageObject.where(obj_type: 'attribute').uniq.pluck(:name).sort
180
- @taglist = CmsPageTag.uniq.pluck(:name).sort
58
+ @attrs = CmsPageObject.where(obj_type: 'attribute').pluck(:name).uniq.sort
59
+ @taglist = CmsPageTag.pluck(:name).uniq.sort
181
60
 
182
61
  if params[:mode] == 'ajax_new' || params[:mode] == 'ajax_edit'
183
62
  @pg.published_version = -1 if params[:mode] == 'ajax_new'
@@ -227,19 +106,19 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
227
106
  tags_to_delete.each { |t| t.destroy }
228
107
 
229
108
  # now try to save page objects (just attributes in this case)
230
- objects_to_delete = @pg.objects.where("obj_type = 'attribute' or obj_type = 'option'").all
109
+ objects_to_delete = @pg.objects.where("obj_type = 'attribute' or obj_type = 'option'").to_a
231
110
 
232
- (params[:page_objects] || {}).each do |key,val|
111
+ params[:page_objects].to_unsafe_h.each do |key, val|
233
112
  next if val.blank?
234
113
 
235
114
  if key =~ /^obj-(\w+?)-(.+?)$/
236
- obj = @pg.objects.where(:name => $2, :obj_type => $1).first
237
- obj ||= @pg.objects.build(:name => $2, :obj_type => $1)
115
+ obj = @pg.objects.where(name: $2, obj_type: $1).first
116
+ obj ||= @pg.objects.build(name: $2, obj_type: $1)
238
117
  obj.content = val
239
- obj.save
240
- objects_to_delete = objects_to_delete.reject { |obj| obj.name == $2 }
118
+ obj.save!
119
+ objects_to_delete.reject! { |obj| obj.name == $2 }
241
120
  end
242
- end
121
+ end if params[:page_objects]
243
122
 
244
123
  objects_to_delete.each { |t| t.destroy }
245
124
 
@@ -252,7 +131,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
252
131
  flash[:notice] = 'Page saved.'
253
132
  session[:cms_pages_path] = @pg.path
254
133
  render :update do |page|
255
- page.redirect_to :action => 'pages'
134
+ page.redirect_to action: 'index'
256
135
  end
257
136
  end
258
137
 
@@ -284,7 +163,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
284
163
  @pg.destroy
285
164
  end
286
165
 
287
- redirect_to :action => 'pages'
166
+ redirect_to action: 'index'
288
167
  end
289
168
 
290
169
  def select_page
@@ -321,13 +200,13 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
321
200
  load_page_objects
322
201
  load_template_options
323
202
 
324
- render :partial => 'template_options'
203
+ render partial: 'template_options'
325
204
  end
326
205
 
327
206
  def page_attribute
328
207
  render :nothing => true and return unless params[:name]
329
208
 
330
- @page_objects = HashObject.new({ params[:name] => params[:value] })
209
+ @page_objects = OpenStruct.new({ params[:name] => params[:value] })
331
210
  render :partial => 'page_attribute', :locals => { :name => params[:name] }
332
211
  end
333
212
 
@@ -335,7 +214,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
335
214
  @pg = CmsPage.find(params[:id])
336
215
  validate_user_access or return
337
216
 
338
- @page_objects = HashObject.new(params[:page_objects] || {})
217
+ @page_objects = params[:page_objects] ? OpenStruct.new(params[:page_objects].to_unsafe_h) : OpenStruct.new
339
218
 
340
219
  if request.get?
341
220
  @pg.version = params[:version] if params[:version] && params[:version].to_i != @pg.version
@@ -357,7 +236,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
357
236
  @page_title = @pg.title
358
237
 
359
238
  @cms_head ||= ''
360
- @cms_head << "<script type=\"text/javascript\" src=\"#{url_for(action: 'page_tags_for_lookup')}\"></script>"
239
+ @taglist = CmsPageTag.pluck(:name).uniq.sort
361
240
 
362
241
  @template_content = substitute_placeholders(@pg.template.content, @pg)
363
242
  render layout: 'application'
@@ -365,7 +244,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
365
244
  elsif request.post?
366
245
  CmsPage.transaction do
367
246
  # need to revise this later if we implement deletion of page objects
368
- old_objs = @pg.objects.where(:cms_page_version => @pg.version).all
247
+ old_objs = @pg.objects.where(cms_page_version: @pg.version).to_a
369
248
 
370
249
  @pg.updated_by = session[:user_id]
371
250
  @pg.updated_by_username = session[:user_username]
@@ -376,7 +255,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
376
255
  end
377
256
 
378
257
  @pg.updated_on = Time.now.utc
379
- @pg.save
258
+ @pg.save # create a new version for new page objects to reference
380
259
 
381
260
  # do a little bit of classification... for now, just identify page lists
382
261
  page_lists = []
@@ -432,9 +311,9 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
432
311
  end
433
312
  end
434
313
 
435
- @page_objects.each do |key,val|
314
+ @page_objects.to_h.each do |key,val|
436
315
  key =~ /^obj-(\w+?)-(.+?)$/
437
- obj = @pg.objects.build(:name => $2, :obj_type => $1)
316
+ obj = @pg.objects.build(name: $2, obj_type: $1)
438
317
 
439
318
  # do a little bit of "censorship" to fix up Word pastes and strange things from the editor
440
319
  if val.is_a?(String)
@@ -481,13 +360,14 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
481
360
  val.gsub!(/<br>(<\/h\d>|<\/p>)/, '\1')
482
361
  end
483
362
 
484
- obj.content = val
485
- obj.save
363
+ obj.content = val.to_s.force_encoding("UTF-8")
364
+ obj.save!
486
365
  end
487
366
 
488
367
  old_objs.each do |obj|
489
368
  unless @pg.objects.where(name: obj.name, cms_page_version: @pg.version)
490
- obj = @pg.objects.build(:name => obj.name, :obj_type => obj.type, :content => obj.content)
369
+ obj = @pg.objects.build(name: obj.name, obj_type: obj.type, content: obj.content)
370
+ obj.save!
491
371
  end
492
372
  end
493
373
 
@@ -525,18 +405,6 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
525
405
  end
526
406
  end
527
407
 
528
- def page_tags_for_lookup
529
- @tags = CmsPageTag.order(:name).map { |tag| tag.name }.uniq
530
- headers['content-type'] = 'text/javascript'
531
- render :layout => false
532
- end
533
-
534
- def page_attributes_for_lookup
535
- @attrs = CmsPageObject.where(:obj_type => 'attribute').uniq.pluck(:name).sort
536
- headers['content-type'] = 'text/javascript'
537
- render :layout => false
538
- end
539
-
540
408
  def page_list_add_tag
541
409
  render :partial => 'page_list_source_tag', :locals => { :i => params[:i], :key => params[:key] }
542
410
  end
@@ -553,11 +421,10 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
553
421
  @pg.published_version = params[:pg][:published_version]
554
422
  @pg.update_index
555
423
  @pg.save_without_revision
556
- logger.debug @pg.errors.full_messages.inspect
557
424
  end
558
425
  end
559
426
 
560
- render :nothing => true
427
+ render plain: 'success'
561
428
  end
562
429
 
563
430
  def request_review
@@ -565,17 +432,24 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
565
432
  @version = params[:version].to_i
566
433
 
567
434
  # send email to request administrative review
568
- # find all users with email address set
569
- User.all.reject { |u| !u.active? || !u.can_manage_cms_publishing? || !u.cms_allowed_sections.blank? }.each do |u|
570
- next unless valid_email_address?(u.email_address)
435
+ emails = []
436
+
437
+ User.find_each do |u|
438
+ next unless u.active? && valid_email_address?(u.email_address) # must be active and have valid email address
439
+ next unless u.can_manage_cms_publishing? && u.cms_allowed_sections.blank? # and have permission to publish
440
+ emails << ImagineCmsMailer.request_review(url_for(controller: '/cms/content', action: 'show', content_path: @pg.path.split('/')), @pg.title, @version, u, @user, params[:change_description].to_s)
441
+ end
442
+
443
+ # email delivery could may fail, catch exceptions here
444
+ emails.each do |email|
571
445
  begin
572
- Mailer.deliver_cms_request_review(url_for(:controller => '/cms/content', :action => 'show', :content_path => []) + @pg.path, @pg.title, @version, u, @user, params[:change_description].to_s)
573
- rescue Exception => e
446
+ email.deliver_now
447
+ rescue StandardError => e
574
448
  logger.error(e)
575
449
  end
576
450
  end
577
451
 
578
- render :nothing => true
452
+ render nothing: true
579
453
  end
580
454
 
581
455
  #
@@ -602,8 +476,8 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
602
476
  @cms_text_editor_placed = true
603
477
  content = ''.html_safe
604
478
  content << text_area(:page_objects, key, { :dojoType => 'Editor2', :toolbarGroup => 'main', :isToolbarGroupLeader => 'false',
605
- :focusOnLoad => focusOnLoad.to_s, :style => 'border: 2px dashed gray; padding: 5px',
606
- :minHeight => '100px' }.update(html_options))
479
+ :focusOnLoad => focusOnLoad.to_s, :style => 'border: 2px dashed gray; padding: 5px',
480
+ :minHeight => '100px' }.update(html_options))
607
481
  content << content_tag(:div, ''.html_safe, :id => "page_object_config_#{key}")
608
482
  content << javascript_tag("jQuery(document).ready(function () { scanForPageObjects(#{@pg.id}, '#{key}', #{@pg.version}); });")
609
483
  content << observe_field("page_objects_#{key}", :function => "scanForPageObjects(#{@pg.id}, '#{key}', #{@pg.version});", :frequency => 2)
@@ -676,9 +550,9 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
676
550
  target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
677
551
 
678
552
  if File.exist?(target_dir)
679
- redirect_to :action => 'select_gallery', :id => @pg, :gallery_id => params[:gallery_id]
553
+ redirect_to action: 'select_gallery', id: @pg, gallery_id: params[:gallery_id]
680
554
  else
681
- render :partial => 'upload_image'
555
+ render partial: 'upload_image'
682
556
  end
683
557
  end
684
558
 
@@ -691,7 +565,14 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
691
565
  data = params[:file][:data]
692
566
  original_filename = data.original_filename.strip.gsub(/[\?\s\/\:\\]+/, '-').gsub(/^-/, '').gsub(/-$/, '')
693
567
  localfile = File.join(target_dir, original_filename)
694
- FileUtils.cp(data.tempfile, localfile)
568
+
569
+ im = MiniMagick::Image.open(data.path())
570
+ if im['dimensions'][0] > CmsImageMaxWidth || im['dimensions'][1] > CmsImageMaxHeight
571
+ im.resize "#{CmsImageMaxWidth}x#{CmsImageMaxHeight}"
572
+ im.write(localfile)
573
+ else
574
+ FileUtils.cp(data.path(), localfile)
575
+ end
695
576
 
696
577
  finish_upload_status "'#{File.basename(localfile)}'"
697
578
  end
@@ -715,7 +596,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
715
596
  testfile = File.join(target_dir, File.basename(localfile, File.extname(localfile))) + '-croptest' + File.extname(localfile)
716
597
 
717
598
  # make a smaller version to help with cropping
718
- im = MiniMagick::Image.from_file(localfile)
599
+ im = MiniMagick::Image.open(localfile)
719
600
  im.resize "500x400>"
720
601
  im.write(testfile)
721
602
  File.chmod(0644, testfile)
@@ -737,8 +618,8 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
737
618
 
738
619
  # need to scale up requested position/dimensions based on how big test image
739
620
  # is relative to original image
740
- orig_im = MiniMagick::Image.from_file(localfile)
741
- test_im = MiniMagick::Image.from_file(testfile)
621
+ orig_im = MiniMagick::Image.open(localfile)
622
+ test_im = MiniMagick::Image.open(testfile)
742
623
  scale = orig_im[:width].to_f / test_im[:width]
743
624
 
744
625
  x1 = params[:image][:x1].to_i * scale
@@ -776,460 +657,144 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
776
657
  render :partial => 'crop_results'
777
658
  end
778
659
 
779
- def receive_gallery
660
+
661
+ def upload_file
662
+ @pg = CmsPage.find_by_id(params[:id])
663
+ render :partial => 'upload_file'
664
+ end
665
+
666
+ def receive_file
780
667
  @pg = CmsPage.find_by_id(params[:id])
781
668
 
782
669
  target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
783
670
  FileUtils.mkdir_p target_dir
784
671
 
785
- count = 1
786
- localdir = File.join(target_dir, 'gallery_1')
787
- while File.exist?(localdir) && count < 100
788
- count += 1
789
- localdir = File.join(target_dir, "gallery_#{count}")
790
- end
791
- FileUtils.mkdir_p File.join(localdir, 'temp')
792
-
793
- data = params[:gallery_file][:data]
794
- # read zip file
795
-
796
- entries = []
797
- Zip::File.foreach(data.path) do |zipentry|
798
- next if ![ '.jpg', '.jpeg', '.png', '.gif' ].include?(File.extname(zipentry.name).downcase) || zipentry.size < 1000
799
- next if File.basename(zipentry.name) =~ /^\._/
800
-
801
- entries << zipentry
802
- end
803
- entries.sort! { |a,b| File.basename(a.name).downcase <=> File.basename(b.name).downcase }
804
-
805
- Zip::File.open(data.path) do |zipfile|
806
- entries.each_with_index do |zipentry, index|
807
- upload_progress.message = "Extracting #{File.basename(zipentry.name)}"
808
- ext = File.extname(zipentry.name)
809
- localfile = File.join(localdir, 'temp', (index+1).to_s + ext.downcase)
810
- jpgfile = File.join(localdir, 'temp', (index+1).to_s + '.jpg')
811
-
812
- begin
813
- zipentry.extract(localfile)
814
-
815
- im = MiniMagick::Image.from_file(localfile)
816
- im.write(jpgfile)
817
-
818
- File.unlink(localfile) if localfile != jpgfile
819
-
820
- rescue Exception => e
821
- logger.error(e)
822
- end
823
- end
824
- end
672
+ data = params[:file][:data]
673
+ original_filename = data.original_filename.strip.gsub(/[\?\s\/\:\\]+/, '-').gsub(/^-/, '').gsub(/-$/, '')
674
+ localfile = File.join(target_dir, original_filename)
675
+ FileUtils.cp(data.tempfile, localfile)
676
+ File.chmod(0644, localfile)
825
677
 
826
- finish_upload_status "'#{File.basename(localdir)}'"
678
+ finish_upload_status "'#{File.basename(localfile)}'"
827
679
  end
828
680
 
829
- def gallery_setup
681
+ def create_file_link
830
682
  @pg = CmsPage.find_by_id(params[:id])
831
- target_dir = File.join('assets', 'content', @pg.path)
832
- @dirname = File.join(target_dir, File.basename(params[:dirname]), 'temp')
833
- Dir.chdir(File.join(Rails.root, 'public'))
834
- @images = Dir.glob("#{@dirname}/*.{jpg,jpeg,png,gif}").sort
835
- Dir.chdir(Rails.root)
836
- @thumbs = []
837
-
838
- @images.each do |img|
839
- next if img.include?('-thumb')
840
-
841
- thumbfile = File.join(Rails.root, 'public', @dirname, File.basename(img, File.extname(img))) + '-thumb.jpg'
842
- @thumbs << File.join(@dirname, File.basename(img, File.extname(img))) + '-thumb.jpg'
843
-
844
- next if File.exist?(thumbfile)
845
-
846
- im = MiniMagick::Image.from_file(File.join(Rails.root, 'public', img))
847
- im.resize "80x80" # hardcoded!
848
- im.write(thumbfile)
849
- File.chmod(0644, thumbfile)
850
- end
683
+ localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
684
+ @filename = localfile.split('/').map { |s| CGI::escape(s) }.join('/') + "?#{File.mtime(localfile).to_i}"
851
685
 
852
- @thumbs.sort! { |a,b| File.basename(a, File.extname(a)).to_i <=> File.basename(b, File.extname(b)).to_i }
853
- session[:gallery_thumbs_ordered] = @thumbs.map { |thumb| File.basename(thumb, File.extname(thumb)) }
686
+ bucket = ImagineCmsConfig['amazon_s3'][Rails.env]['file_bucket'] rescue nil
687
+ prefix = ImagineCmsConfig['amazon_s3']['file_prefix'] rescue nil
688
+ upload_to_s3(localfile, @pg, bucket, prefix)
854
689
 
855
- render :partial => 'gallery_setup'
690
+ render :partial => 'create_file_link'
856
691
  end
857
692
 
858
693
 
859
- def complete_gallery
860
- @pg = CmsPage.find(params[:id])
861
- target_dir = File.join('assets', 'content', @pg.path)
862
- @dirname = File.join(target_dir, File.basename(params[:dirname]))
863
- @thumbs = session[:gallery_thumbs_ordered]
864
- max_width = params[:max_width].to_i
865
- max_width = GalleryMaxWidth unless max_width > 0
866
- max_height = params[:max_height].to_i
867
- max_height = GalleryMaxHeight unless max_height > 0
868
-
869
- create_captions_file(@pg.id, { :gallery_id => File.basename(params[:dirname]) })
870
-
871
- @thumbs.each_with_index do |thumb, index|
872
- thumb.gsub!(/-thumb/, '')
873
- tempfile = File.join(Rails.root, 'public', @dirname, 'temp', thumb + '.jpg')
874
- tempthumbfile = File.join(Rails.root, 'public', @dirname, 'temp', thumb + '-thumb.jpg')
875
-
876
- localfile = File.join(Rails.root, 'public', @dirname, (index+1).to_s + '.jpg')
877
- thumbfile = File.join(Rails.root, 'public', @dirname, (index+1).to_s + '-thumb.jpg')
878
-
879
- im = MiniMagick::Image.from_file(tempfile)
880
- if im[:width] > max_width || im[:height] > max_height
881
- im.resize("#{max_width}x#{max_height}")
882
- end
883
- im.write(localfile)
884
-
885
- small = MiniMagick::Image.from_file(tempfile)
886
- small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
887
- small.write(thumbfile)
888
-
889
- File.chmod(0644, localfile, thumbfile)
890
-
891
- begin
892
- File.unlink(tempfile)
893
- File.unlink(tempthumbfile)
894
- rescue Exception => e
895
- # not that big a deal if we can't delete
896
- end
897
- end
694
+ def upload_thumb
695
+ @pg = CmsPage.find_by_id(params[:id])
696
+ render :partial => 'upload_thumb'
697
+ end
698
+
699
+ def crop_thumb
700
+ @pg = CmsPage.find_by_id(params[:id])
701
+ origfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
702
+ newfilename = File.basename(params[:filename], File.extname(params[:filename])) + '-thumb' + File.extname(params[:filename])
703
+ localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, newfilename)
704
+ FileUtils.mv(origfile, localfile)
705
+ File.chmod(0644, localfile)
898
706
 
899
- begin
900
- Dir.rmdir(File.join(Rails.root, 'public', @dirname, 'temp'))
901
- rescue Exception => e
902
- # not that big a deal if we can't delete
707
+ # get out now if user clicked finish
708
+ if params[:next_clicked].to_i != 1
709
+ @image_file = localfile + "?#{File.mtime(localfile).to_i}"
710
+ upload_to_s3(localfile, @pg)
711
+ render :partial => 'crop_results_thumb' and return
903
712
  end
904
713
 
905
- create_preview_images
906
-
907
- render :partial => 'complete_gallery'
908
- end
909
-
910
- def gallery_management
911
- @pg = CmsPage.find_by_id(params[:id])
912
- galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
913
- @galleries = Dir.glob("#{galleries_dir}/gallery_*")
914
- gallery_dir = File.join(galleries_dir, params[:gallery_id].to_s)
915
714
 
916
- @images = Dir.glob("#{gallery_dir}/*.{jpg,jpeg,png,gif}").reject { |img| img.include?('thumb') }.map { |img| File.basename(img).split('.').first.to_i }.sort
715
+ # if we're still here... let's crop!
716
+ target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
717
+ testfile = File.join(target_dir, File.basename(localfile, File.extname(localfile))) + '-croptest' + File.extname(localfile)
917
718
 
918
- create_preview_images
719
+ # make a smaller version to help with cropping
720
+ im = MiniMagick::Image.open(localfile)
721
+ im.resize("500x400>")
722
+ im.write(testfile)
723
+ File.chmod(0644, testfile)
919
724
 
920
- if params[:gallery_id]
921
- @gallery = load_gallery_settings_from_file(params[:gallery_id])
922
- end
725
+ @width = im[:width]
726
+ @height = im[:height]
727
+ @height = 1 if @height == 0
728
+ @image_file = File.basename(testfile)
729
+ @aspect_ratio = @width.to_f/@height
923
730
 
924
- render :layout => false
925
- end
926
-
927
- def set_gallery_order
928
- session[:gallery_thumbs_ordered] = params[:image_sorter]
929
- render :nothing => true
731
+ render :partial => 'crop_thumb'
930
732
  end
931
733
 
932
- def select_gallery
734
+ def save_crop_thumb
933
735
  @pg = CmsPage.find_by_id(params[:id])
934
- @target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
935
- @galleries = Dir.glob("#{@target_dir}/gallery_*")
736
+ target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
737
+ testfile = File.join(target_dir, File.basename(params[:filename]))
738
+ localfile = testfile.split(/-croptest/).join('')
936
739
 
937
- create_preview_images
740
+ # need to scale up requested position/dimensions based on how big test image
741
+ # is relative to original image
742
+ orig_im = MiniMagick::Image.open(localfile)
743
+ test_im = MiniMagick::Image.open(testfile)
744
+ scale = orig_im[:width].to_f / test_im[:width]
938
745
 
939
- if request.post?
940
- unless params[:gallery_id].downcase == "new"
941
- redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
942
- else
943
- render :partial => 'upload_image'
944
- end
945
- else
946
- render :partial => 'select_gallery'
947
- end
948
- end
949
-
950
- def save_gallery_settings
951
- if request.post?
952
- @pg = CmsPage.find_by_id(params[:id])
953
- save_gallery_settings_to_file(params[:gallery_id], params[:gallery])
954
-
955
- render :nothing => true
956
- end
957
- end
958
-
959
- def sort_images
960
- @pg = CmsPage.find_by_id(params[:id])
961
- gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
962
-
963
- @images = Dir.glob("#{gallery_dir}/*.{jpg,jpeg,png,gif}").reject { |img| img.include?('thumb') }.map { |img| File.basename(img).split('.').first.to_i }.sort
964
-
965
- if params[:images]
966
- session[:gallery_images_sorted] = params[:images]
967
- render :nothing => true
968
- else
969
- render :partial => 'sort_images'
970
- end
971
- end
972
-
973
- def sort_images_save
974
- @pg = CmsPage.find_by_id(params[:id])
975
- gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
976
- temp_dir = File.join(gallery_dir, 'temp')
977
- sorted_images = session[:gallery_images_sorted] || []
978
-
979
- if sorted_images == []
980
- redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
981
- return
982
- end
983
-
984
- # create blank captions.yml if it doesn't already exist
985
- create_captions_file(@pg.id)
986
-
987
- original_captions = YAML.load_file(File.join(gallery_dir, 'captions.yml')).to_a
988
- new_captions = [ original_captions[0] ]
989
-
990
- Dir.glob("#{gallery_dir}/**/*.jpg").each { |img| FileUtils.touch(img); FileUtils.mv(img, img + '.tmp') }
991
-
992
- sorted_images.each_with_index do |img, i|
993
- FileUtils.mv(File.join(gallery_dir, "#{img.to_i}.jpg.tmp"), File.join(gallery_dir, "#{i+1}.jpg"))
994
- FileUtils.mv(File.join(gallery_dir, "#{img.to_i}-thumb.jpg.tmp"), File.join(gallery_dir, "#{i+1}-thumb.jpg"))
995
- FileUtils.mv(File.join(gallery_dir, 'management', "#{img.to_i}.jpg.tmp"), File.join(gallery_dir, 'management', "#{i+1}.jpg"))
996
- new_captions << original_captions[img.to_i] || ''
997
- end
998
-
999
- yaml = YAML.dump(new_captions)
1000
- File.open(File.join(gallery_dir, 'captions.yml'), 'w') { |f| f << yaml }
1001
- session[:gallery_images_sorted] = nil
1002
-
1003
- redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
1004
- end
1005
-
1006
- def image_details
1007
- @pg = CmsPage.find_by_id(params[:id])
1008
-
1009
- # create blank captions.yml if it doesn't already exist
1010
- create_captions_file(@pg.id)
1011
-
1012
- gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1013
- captions = YAML.load(File.open(File.join(gallery_dir, 'captions.yml')).read)
1014
- image_id = params[:image].split('.').first.to_i
1015
- @caption = captions[image_id]
1016
-
1017
- render partial: 'image_details'
1018
- end
1019
-
1020
- def update_caption
1021
- if request.post?
1022
- @pg = CmsPage.find_by_id(params[:id])
1023
-
1024
- # create blank captions.yml if it doesn't already exist
1025
- create_captions_file(@pg.id)
1026
-
1027
- gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1028
- captions = YAML.load_file(File.join(gallery_dir, 'captions.yml')).to_a
1029
- image_id = params[:image].split('.').first.to_i
1030
- captions[image_id] = params[:caption]
1031
-
1032
- yaml = YAML.dump(captions)
1033
- File.open(File.join(gallery_dir, 'captions.yml'), "w") { |f| f << yaml }
1034
- end
1035
-
1036
- redirect_to action: 'gallery_management', id: params[:id], gallery_id: params[:gallery_id]
1037
- end
1038
-
1039
- def add_to_gallery
1040
- @pg = CmsPage.find_by_id(params[:id])
1041
- galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1042
- @galleries = Dir.entries(galleries_dir).sort
1043
- @gallery_dir = File.join(galleries_dir, params[:gallery_id])
1044
- images = Dir.glob("#{@gallery_dir}/*-thumb.{jpg,jpeg,png,gif}")
1045
-
1046
- temp_location = File.join(@gallery_dir ,'temp')
1047
- FileUtils.rm_rf(temp_location)
1048
- Dir.mkdir(temp_location)
1049
-
1050
- data = params[:gallery_file][:data]
1051
- data_dest = File.join(temp_location, data.original_filename)
1052
- File.open(data_dest, 'wb') { |f| f.write(data.read) }
746
+ x1 = params[:image][:x1].to_i * scale
747
+ y1 = params[:image][:y1].to_i * scale
748
+ width = params[:image][:width].to_i * scale
749
+ height = params[:image][:height].to_i * scale
1053
750
 
1054
- last_id = images.size
1055
- ext = File.extname(data_dest).downcase
751
+ max_width = params[:image][:max_width].to_i
752
+ max_height = params[:image][:max_height].to_i
753
+ dirty = false
1056
754
 
1057
- if ext != '.zip'
1058
- localfile = File.join(@gallery_dir, (last_id + 1).to_s + ext)
1059
- thumbfile = File.join(@gallery_dir, (last_id + 1).to_s + '-thumb' + ext)
1060
-
1061
- File.open(localfile, "w") { |f| f.write(File.open(data_dest).read) }
1062
-
1063
- # create blank captions.yml if it doesn't already exist
1064
- create_captions_file(@pg.id)
1065
-
1066
- localfile = resize_image(localfile)
1067
-
1068
- small = MiniMagick::Image.from_file(localfile)
1069
- small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
1070
- small.write(thumbfile)
1071
-
1072
- File.chmod(0644, localfile, thumbfile)
1073
-
1074
- create_preview_images(:force => 1)
1075
-
1076
- elsif ext == '.zip'
1077
- begin
1078
- Zip::File.foreach(data.path) do |zipentry|
1079
- next if ![ '.jpg', '.jpeg', '.png', '.gif' ].include?(File.extname(zipentry.name).downcase) || zipentry.size < 1000
1080
- upload_progress.message = "Extracting #{File.basename(zipentry.name)}"
1081
- localfile = File.join(temp_location, ((last_id+1).to_s + File.extname(zipentry.name)).downcase)
1082
-
1083
- begin
1084
- zipentry.extract(localfile)
1085
- last_id += 1
1086
- rescue Exception => e
1087
- logger.error(e)
1088
- end
1089
- end
1090
- rescue Exception => e
1091
- logger.debug params.inspect
1092
- logger.error(e)
1093
- finish_upload_status "''" and return
1094
- end
1095
-
1096
- @images = Dir.glob("#{temp_location}/*.{jpg,jpeg,png,gif}")
1097
-
1098
- @images.each do |img|
1099
- localfile = File.join(@gallery_dir, File.basename(img, File.extname(img))) + '.jpg'
1100
- tempfile = File.join(temp_location, File.basename(img, File.extname(img))) + File.extname(img)
1101
- thumbfile = File.join(@gallery_dir, File.basename(img, File.extname(img))) + '-thumb.jpg'
1102
-
1103
- small = MiniMagick::Image.from_file(tempfile)
1104
- small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
1105
- small.write(thumbfile)
1106
-
1107
- FileUtils.cp(tempfile, localfile)
1108
- resize_image(localfile)
1109
-
1110
- File.chmod(0644, localfile, thumbfile)
1111
- end
1112
-
1113
- # smaller images for gallery index
1114
- management_dir = File.join(@gallery_dir, 'management')
1115
-
1116
- preview_images = []
1117
- Dir.glob("#{@gallery_dir}/*.{jpg,jpeg,png,gif}").each { |img| preview_images << img unless File.basename(img).include?('thumb') }
1118
-
1119
- preview_images.each { |img| create_preview_image(img, management_dir, 1) }
755
+ # crop if user selected something
756
+ if params[:image][:width].to_i > 0
757
+ logger.debug "cropping @ (#{x1}, #{y1}) to size #{width} x #{height}"
758
+ orig_im.crop("#{width}x#{height}+#{x1}+#{y1}")
759
+ dirty = true
1120
760
  end
1121
761
 
1122
- File.delete(data_dest)
1123
-
1124
- upload_progress.message = "File received successfully."
1125
- finish_upload_status "'#{File.basename(data_dest)}'" and return
1126
- end
1127
-
1128
- def delete_photo
1129
- if request.post?
1130
- @pg = CmsPage.find_by_id(params[:id])
1131
-
1132
- # create blank captions.yml if it doesn't already exist
1133
- create_captions_file(@pg.id)
1134
-
1135
- gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1136
- captions = YAML.load(File.open(File.join(gallery_dir, 'captions.yml')).read).to_a
1137
-
1138
- image_id = params[:image].split('.').first.to_i
1139
-
1140
- begin ; File.delete(File.join(gallery_dir, image_id.to_s + '.jpg')) ; rescue ; end
1141
- begin ; File.delete(File.join(gallery_dir, image_id.to_s + '-thumb.jpg')) ; rescue ; end
1142
- begin ; File.delete(File.join(gallery_dir, 'management', image_id.to_s + '.jpg')) ; rescue ; end
1143
-
1144
- all_images = Dir.glob(File.join(gallery_dir, '*.{jpg,jpeg,png,gif}'))
1145
- images = []
1146
- all_images.each { |img| images << img if !File.basename(img).include?('thumb') && File.basename(img).split('.').first.to_i > image_id }
1147
-
1148
- image_names = []
1149
- images.each_with_index { |img, index| image_names << File.basename(img).split('.').first.to_i }
1150
- image_names.sort!
1151
-
1152
- new_captions = []
1153
- for i in 0...image_id do
1154
- new_captions[i] = captions[i] || ''
1155
- end
1156
-
1157
- image_names.each do |img|
1158
- FileUtils.mv(File.join(gallery_dir, img.to_s + '.jpg'), File.join(gallery_dir, image_id.to_s + '.jpg'))
1159
- FileUtils.mv(File.join(gallery_dir, img.to_s + '-thumb.jpg'), File.join(gallery_dir, image_id.to_s + '-thumb.jpg'))
1160
- FileUtils.mv(File.join(gallery_dir, 'management', img.to_s + '.jpg'), File.join(gallery_dir, 'management', image_id.to_s + '.jpg'))
1161
-
1162
- new_captions[image_id] = captions[img] || ''
1163
-
1164
- image_id += 1
762
+ # resize if the resultant image is bigger than max dims
763
+ if max_width > 0 && max_height > 0
764
+ if orig_im[:width] > max_width || orig_im[:height] > max_height
765
+ logger.debug "resizing to max dims #{max_width} x #{max_height}"
766
+ orig_im.resize("#{max_width}x#{max_height}>")
767
+ dirty = true
1165
768
  end
1166
-
1167
- yaml = YAML.dump(new_captions)
1168
- File.open(File.join(gallery_dir, 'captions.yml'), "w") { |f| f << yaml }
1169
769
  end
1170
770
 
1171
- redirect_to :action => 'gallery_management', :id => params[:id], :gallery_id => params[:gallery_id]
1172
- end
1173
-
1174
- def delete_gallery
1175
- @pg = CmsPage.find_by_id(params[:id])
1176
- galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1177
- gallery_dir = File.join(galleries_dir, params[:gallery_id])
1178
-
1179
- FileUtils.rm_rf(gallery_dir)
1180
-
1181
- redirect_to :action => 'select_gallery', :id => params[:id]
1182
- end
1183
-
1184
-
1185
- def upload_file
1186
- @pg = CmsPage.find_by_id(params[:id])
1187
- render :partial => 'upload_file'
1188
- end
1189
-
1190
- def receive_file
1191
- @pg = CmsPage.find_by_id(params[:id])
1192
-
1193
- target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1194
- FileUtils.mkdir_p target_dir
1195
-
1196
- data = params[:file][:data]
1197
- original_filename = data.original_filename.strip.gsub(/[\?\s\/\:\\]+/, '-').gsub(/^-/, '').gsub(/-$/, '')
1198
- localfile = File.join(target_dir, original_filename)
1199
- FileUtils.cp(data.tempfile, localfile)
771
+ orig_im.write(localfile) if dirty
1200
772
  File.chmod(0644, localfile)
1201
773
 
1202
- finish_upload_status "'#{File.basename(localfile)}'"
1203
- end
1204
-
1205
- def create_file_link
1206
- @pg = CmsPage.find_by_id(params[:id])
1207
- localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
1208
- @filename = localfile.split('/').map { |s| CGI::escape(s) }.join('/') + "?#{File.mtime(localfile).to_i}"
1209
-
1210
- bucket = ImagineCmsConfig['amazon_s3'][Rails.env]['file_bucket'] rescue nil
1211
- prefix = ImagineCmsConfig['amazon_s3']['file_prefix'] rescue nil
1212
- upload_to_s3(localfile, @pg, bucket, prefix)
774
+ @image_file = localfile + "?#{File.mtime(localfile).to_i}"
775
+ File.unlink testfile
776
+ upload_to_s3(localfile, @pg)
1213
777
 
1214
- render :partial => 'create_file_link'
778
+ render :partial => 'crop_results_thumb'
1215
779
  end
1216
780
 
1217
-
1218
- def upload_thumb
781
+ def upload_feature_image
1219
782
  @pg = CmsPage.find_by_id(params[:id])
1220
- render :partial => 'upload_thumb'
783
+ render :partial => 'upload_feature_image'
1221
784
  end
1222
785
 
1223
- def crop_thumb
786
+ def crop_feature_image
1224
787
  @pg = CmsPage.find_by_id(params[:id])
1225
- localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
1226
- File.chmod(0644, localfile)
788
+ origfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
789
+ newfilename = File.basename(params[:filename], File.extname(params[:filename])) + '-feature' + File.extname(params[:filename])
790
+ localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, newfilename)
791
+ FileUtils.mv(origfile, localfile)
1227
792
 
1228
793
  # get out now if user clicked finish
1229
794
  if params[:next_clicked].to_i != 1
1230
795
  @image_file = localfile + "?#{File.mtime(localfile).to_i}"
1231
796
  upload_to_s3(localfile, @pg)
1232
- render :partial => 'crop_results_thumb' and return
797
+ render :partial => 'crop_results_feature_image' and return
1233
798
  end
1234
799
 
1235
800
 
@@ -1238,7 +803,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1238
803
  testfile = File.join(target_dir, File.basename(localfile, File.extname(localfile))) + '-croptest' + File.extname(localfile)
1239
804
 
1240
805
  # make a smaller version to help with cropping
1241
- im = MiniMagick::Image.from_file(localfile)
806
+ im = MiniMagick::Image.open(localfile)
1242
807
  im.resize("500x400>")
1243
808
  im.write(testfile)
1244
809
  File.chmod(0644, testfile)
@@ -1249,10 +814,10 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1249
814
  @image_file = File.basename(testfile)
1250
815
  @aspect_ratio = @width.to_f/@height
1251
816
 
1252
- render :partial => 'crop_thumb'
817
+ render :partial => 'crop_feature_image'
1253
818
  end
1254
819
 
1255
- def save_crop_thumb
820
+ def save_crop_feature_image
1256
821
  @pg = CmsPage.find_by_id(params[:id])
1257
822
  target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1258
823
  testfile = File.join(target_dir, File.basename(params[:filename]))
@@ -1260,8 +825,8 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1260
825
 
1261
826
  # need to scale up requested position/dimensions based on how big test image
1262
827
  # is relative to original image
1263
- orig_im = MiniMagick::Image.from_file(localfile)
1264
- test_im = MiniMagick::Image::from_file(testfile)
828
+ orig_im = MiniMagick::Image.open(localfile)
829
+ test_im = MiniMagick::Image.open(testfile)
1265
830
  scale = orig_im[:width].to_f / test_im[:width]
1266
831
 
1267
832
  x1 = params[:image][:x1].to_i * scale
@@ -1296,131 +861,481 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1296
861
  File.unlink testfile
1297
862
  upload_to_s3(localfile, @pg)
1298
863
 
1299
- render :partial => 'crop_results_thumb'
864
+ render :partial => 'crop_results_feature_image'
1300
865
  end
1301
866
 
1302
- def upload_feature_image
867
+
868
+ def receive_gallery
1303
869
  @pg = CmsPage.find_by_id(params[:id])
1304
- render :partial => 'upload_feature_image'
870
+
871
+ target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
872
+ FileUtils.mkdir_p target_dir
873
+
874
+ count = 1
875
+ localdir = File.join(target_dir, 'gallery_1')
876
+ while File.exist?(localdir) && count < 100
877
+ count += 1
878
+ localdir = File.join(target_dir, "gallery_#{count}")
879
+ end
880
+ FileUtils.mkdir_p File.join(localdir, 'temp')
881
+
882
+ data = params[:gallery_file][:data]
883
+ # read zip file
884
+
885
+ entries = []
886
+ Zip::File.foreach(data.path) do |zipentry|
887
+ next if ![ '.jpg', '.jpeg', '.png', '.gif' ].include?(File.extname(zipentry.name).downcase) || zipentry.size < 1000
888
+ next if File.basename(zipentry.name) =~ /^\._/
889
+
890
+ entries << zipentry
891
+ end
892
+ entries.sort! { |a,b| File.basename(a.name).downcase <=> File.basename(b.name).downcase }
893
+
894
+ Zip::File.open(data.path) do |zipfile|
895
+ entries.each_with_index do |zipentry, index|
896
+ upload_progress.message = "Extracting #{File.basename(zipentry.name)}"
897
+ ext = File.extname(zipentry.name)
898
+ localfile = File.join(localdir, 'temp', (index+1).to_s + ext.downcase)
899
+ jpgfile = File.join(localdir, 'temp', (index+1).to_s + '.jpg')
900
+
901
+ begin
902
+ zipentry.extract(localfile)
903
+
904
+ im = MiniMagick::Image.open(localfile)
905
+ im.write(jpgfile)
906
+
907
+ File.unlink(localfile) if localfile != jpgfile
908
+
909
+ rescue StandardError => e
910
+ logger.error(e)
911
+ end
912
+ end
913
+ end
914
+
915
+ finish_upload_status "'#{File.basename(localdir)}'"
1305
916
  end
1306
917
 
1307
- def crop_feature_image
918
+ def gallery_setup
1308
919
  @pg = CmsPage.find_by_id(params[:id])
1309
- localfile = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, File.basename(params[:filename]))
1310
- File.chmod(0644, localfile)
920
+ target_dir = File.join('assets', 'content', @pg.path)
921
+ @dirname = File.join(target_dir, File.basename(params[:dirname]), 'temp')
922
+ Dir.chdir(File.join(Rails.root, 'public'))
923
+ @images = Dir.glob("#{@dirname}/*.{jpg,jpeg,png,gif}").sort
924
+ Dir.chdir(Rails.root)
925
+ @thumbs = []
1311
926
 
1312
- # get out now if user clicked finish
1313
- if params[:next_clicked].to_i != 1
1314
- @image_file = localfile + "?#{File.mtime(localfile).to_i}"
1315
- upload_to_s3(localfile, @pg)
1316
- render :partial => 'crop_results_feature_image' and return
927
+ @images.each do |img|
928
+ next if img.include?('-thumb')
929
+
930
+ thumbfile = File.join(Rails.root, 'public', @dirname, File.basename(img, File.extname(img))) + '-thumb.jpg'
931
+ @thumbs << File.join(@dirname, File.basename(img, File.extname(img))) + '-thumb.jpg'
932
+
933
+ next if File.exist?(thumbfile)
934
+
935
+ im = MiniMagick::Image.open(File.join(Rails.root, 'public', img))
936
+ im.resize "80x80" # hardcoded!
937
+ im.write(thumbfile)
938
+ File.chmod(0644, thumbfile)
1317
939
  end
1318
940
 
941
+ @thumbs.sort! { |a,b| File.basename(a, File.extname(a)).to_i <=> File.basename(b, File.extname(b)).to_i }
942
+ session[:gallery_thumbs_ordered] = @thumbs.map { |thumb| File.basename(thumb, File.extname(thumb)) }
1319
943
 
1320
- # if we're still here... let's crop!
1321
- target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1322
- testfile = File.join(target_dir, File.basename(localfile, File.extname(localfile))) + '-croptest' + File.extname(localfile)
944
+ render :partial => 'gallery_setup'
945
+ end
946
+
947
+
948
+ def complete_gallery
949
+ @pg = CmsPage.find(params[:id])
950
+ target_dir = File.join('assets', 'content', @pg.path)
951
+ @dirname = File.join(target_dir, File.basename(params[:dirname]))
952
+ @thumbs = session[:gallery_thumbs_ordered]
953
+ max_width = params[:max_width].to_i
954
+ max_width = GalleryMaxWidth unless max_width > 0
955
+ max_height = params[:max_height].to_i
956
+ max_height = GalleryMaxHeight unless max_height > 0
1323
957
 
1324
- # make a smaller version to help with cropping
1325
- im = MiniMagick::Image.from_file(localfile)
1326
- im.resize("500x400>")
1327
- im.write(testfile)
1328
- File.chmod(0644, testfile)
958
+ create_captions_file(@pg.id, { :gallery_id => File.basename(params[:dirname]) })
1329
959
 
1330
- @width = im[:width]
1331
- @height = im[:height]
1332
- @height = 1 if @height == 0
1333
- @image_file = File.basename(testfile)
1334
- @aspect_ratio = @width.to_f/@height
960
+ @thumbs.each_with_index do |thumb, index|
961
+ thumb.gsub!(/-thumb/, '')
962
+ tempfile = File.join(Rails.root, 'public', @dirname, 'temp', thumb + '.jpg')
963
+ tempthumbfile = File.join(Rails.root, 'public', @dirname, 'temp', thumb + '-thumb.jpg')
964
+
965
+ localfile = File.join(Rails.root, 'public', @dirname, (index+1).to_s + '.jpg')
966
+ thumbfile = File.join(Rails.root, 'public', @dirname, (index+1).to_s + '-thumb.jpg')
967
+
968
+ im = MiniMagick::Image.open(tempfile)
969
+ if im[:width] > max_width || im[:height] > max_height
970
+ im.resize("#{max_width}x#{max_height}")
971
+ end
972
+ im.write(localfile)
973
+
974
+ small = MiniMagick::Image.open(tempfile)
975
+ small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
976
+ small.write(thumbfile)
977
+
978
+ File.chmod(0644, localfile, thumbfile)
979
+
980
+ begin
981
+ File.unlink(tempfile)
982
+ File.unlink(tempthumbfile)
983
+ rescue StandardError => e
984
+ # not that big a deal if we can't delete
985
+ end
986
+ end
987
+
988
+ begin
989
+ Dir.rmdir(File.join(Rails.root, 'public', @dirname, 'temp'))
990
+ rescue StandardError => e
991
+ # not that big a deal if we can't delete
992
+ end
993
+
994
+ create_preview_images
995
+
996
+ render :partial => 'complete_gallery'
997
+ end
998
+
999
+ def gallery_management
1000
+ @pg = CmsPage.find_by_id(params[:id])
1001
+ galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1002
+ @galleries = Dir.glob("#{galleries_dir}/gallery_*")
1003
+ gallery_dir = File.join(galleries_dir, params[:gallery_id].to_s)
1004
+
1005
+ @images = Dir.glob("#{gallery_dir}/*.{jpg,jpeg,png,gif}").reject { |img| img.include?('thumb') }.map { |img| File.basename(img).split('.').first.to_i }.sort
1006
+
1007
+ create_preview_images
1008
+
1009
+ if params[:gallery_id]
1010
+ @gallery = load_gallery_settings_from_file(params[:gallery_id])
1011
+ end
1012
+
1013
+ render :layout => false
1014
+ end
1015
+
1016
+ def set_gallery_order
1017
+ session[:gallery_thumbs_ordered] = params[:image_sorter]
1018
+ render :nothing => true
1019
+ end
1020
+
1021
+ def select_gallery
1022
+ @pg = CmsPage.find_by_id(params[:id])
1023
+ @target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1024
+ @galleries = Dir.glob("#{@target_dir}/gallery_*")
1025
+
1026
+ create_preview_images
1027
+
1028
+ if request.post?
1029
+ unless params[:gallery_id].downcase == "new"
1030
+ redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
1031
+ else
1032
+ render :partial => 'upload_image'
1033
+ end
1034
+ else
1035
+ render :partial => 'select_gallery'
1036
+ end
1037
+ end
1038
+
1039
+ def save_gallery_settings
1040
+ if request.post?
1041
+ @pg = CmsPage.find_by_id(params[:id])
1042
+ save_gallery_settings_to_file(params[:gallery_id], params[:gallery])
1043
+
1044
+ render :nothing => true
1045
+ end
1046
+ end
1047
+
1048
+ def sort_images
1049
+ @pg = CmsPage.find_by_id(params[:id])
1050
+ gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1051
+
1052
+ @images = Dir.glob("#{gallery_dir}/*.{jpg,jpeg,png,gif}").reject { |img| img.include?('thumb') }.map { |img| File.basename(img).split('.').first.to_i }.sort
1053
+
1054
+ if params[:images]
1055
+ session[:gallery_images_sorted] = params[:images]
1056
+ render :nothing => true
1057
+ else
1058
+ render :partial => 'sort_images'
1059
+ end
1060
+ end
1061
+
1062
+ def sort_images_save
1063
+ @pg = CmsPage.find_by_id(params[:id])
1064
+ gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1065
+ temp_dir = File.join(gallery_dir, 'temp')
1066
+ sorted_images = session[:gallery_images_sorted] || []
1067
+
1068
+ if sorted_images == []
1069
+ redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
1070
+ return
1071
+ end
1072
+
1073
+ # create blank captions.yml if it doesn't already exist
1074
+ create_captions_file(@pg.id)
1075
+
1076
+ original_captions = YAML.load_file(File.join(gallery_dir, 'captions.yml')).to_a
1077
+ new_captions = [ original_captions[0] ]
1078
+
1079
+ Dir.glob("#{gallery_dir}/**/*.jpg").each { |img| FileUtils.touch(img); FileUtils.mv(img, img + '.tmp') }
1080
+
1081
+ sorted_images.each_with_index do |img, i|
1082
+ FileUtils.mv(File.join(gallery_dir, "#{img.to_i}.jpg.tmp"), File.join(gallery_dir, "#{i+1}.jpg"))
1083
+ FileUtils.mv(File.join(gallery_dir, "#{img.to_i}-thumb.jpg.tmp"), File.join(gallery_dir, "#{i+1}-thumb.jpg"))
1084
+ FileUtils.mv(File.join(gallery_dir, 'management', "#{img.to_i}.jpg.tmp"), File.join(gallery_dir, 'management', "#{i+1}.jpg"))
1085
+ new_captions << original_captions[img.to_i] || ''
1086
+ end
1087
+
1088
+ yaml = YAML.dump(new_captions)
1089
+ File.open(File.join(gallery_dir, 'captions.yml'), 'w') { |f| f << yaml }
1090
+ session[:gallery_images_sorted] = nil
1091
+
1092
+ redirect_to :action => 'gallery_management', :id => @pg, :gallery_id => params[:gallery_id]
1093
+ end
1094
+
1095
+ def image_details
1096
+ @pg = CmsPage.find_by_id(params[:id])
1097
+
1098
+ # create blank captions.yml if it doesn't already exist
1099
+ create_captions_file(@pg.id)
1100
+
1101
+ gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1102
+ captions = YAML.load(File.open(File.join(gallery_dir, 'captions.yml')).read)
1103
+ image_id = params[:image].split('.').first.to_i
1104
+ @caption = captions[image_id]
1105
+
1106
+ render partial: 'image_details'
1107
+ end
1108
+
1109
+ def update_caption
1110
+ if request.post?
1111
+ @pg = CmsPage.find_by_id(params[:id])
1112
+
1113
+ # create blank captions.yml if it doesn't already exist
1114
+ create_captions_file(@pg.id)
1115
+
1116
+ gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1117
+ captions = YAML.load_file(File.join(gallery_dir, 'captions.yml')).to_a
1118
+ image_id = params[:image].split('.').first.to_i
1119
+ captions[image_id] = params[:caption]
1120
+
1121
+ yaml = YAML.dump(captions)
1122
+ File.open(File.join(gallery_dir, 'captions.yml'), "w") { |f| f << yaml }
1123
+ end
1335
1124
 
1336
- render :partial => 'crop_feature_image'
1125
+ redirect_to action: 'gallery_management', id: params[:id], gallery_id: params[:gallery_id]
1337
1126
  end
1338
1127
 
1339
- def save_crop_feature_image
1128
+ def add_to_gallery
1340
1129
  @pg = CmsPage.find_by_id(params[:id])
1341
- target_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1342
- testfile = File.join(target_dir, File.basename(params[:filename]))
1343
- localfile = testfile.split(/-croptest/).join('')
1130
+ galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1131
+ @galleries = Dir.entries(galleries_dir).sort
1132
+ @gallery_dir = File.join(galleries_dir, params[:gallery_id])
1133
+ images = Dir.glob("#{@gallery_dir}/*-thumb.{jpg,jpeg,png,gif}")
1344
1134
 
1345
- # need to scale up requested position/dimensions based on how big test image
1346
- # is relative to original image
1347
- orig_im = MiniMagick::Image.from_file(localfile)
1348
- test_im = MiniMagick::Image::from_file(testfile)
1349
- scale = orig_im[:width].to_f / test_im[:width]
1135
+ temp_location = File.join(@gallery_dir ,'temp')
1136
+ FileUtils.rm_rf(temp_location)
1137
+ Dir.mkdir(temp_location)
1350
1138
 
1351
- x1 = params[:image][:x1].to_i * scale
1352
- y1 = params[:image][:y1].to_i * scale
1353
- width = params[:image][:width].to_i * scale
1354
- height = params[:image][:height].to_i * scale
1139
+ data = params[:gallery_file][:data]
1140
+ data_dest = File.join(temp_location, data.original_filename)
1141
+ File.open(data_dest, 'wb') { |f| f.write(data.read) }
1355
1142
 
1356
- max_width = params[:image][:max_width].to_i
1357
- max_height = params[:image][:max_height].to_i
1358
- dirty = false
1143
+ last_id = images.size
1144
+ ext = File.extname(data_dest).downcase
1359
1145
 
1360
- # crop if user selected something
1361
- if params[:image][:width].to_i > 0
1362
- logger.debug "cropping @ (#{x1}, #{y1}) to size #{width} x #{height}"
1363
- orig_im.crop("#{width}x#{height}+#{x1}+#{y1}")
1364
- dirty = true
1146
+ if ext != '.zip'
1147
+ localfile = File.join(@gallery_dir, (last_id + 1).to_s + ext)
1148
+ thumbfile = File.join(@gallery_dir, (last_id + 1).to_s + '-thumb' + ext)
1149
+
1150
+ File.open(localfile, "w") { |f| f.write(File.open(data_dest).read) }
1151
+
1152
+ # create blank captions.yml if it doesn't already exist
1153
+ create_captions_file(@pg.id)
1154
+
1155
+ localfile = resize_image(localfile)
1156
+
1157
+ small = MiniMagick::Image.open(localfile)
1158
+ small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
1159
+ small.write(thumbfile)
1160
+
1161
+ File.chmod(0644, localfile, thumbfile)
1162
+
1163
+ create_preview_images(force: true)
1164
+
1165
+ elsif ext == '.zip'
1166
+ begin
1167
+ Zip::File.foreach(data.path) do |zipentry|
1168
+ next if ![ '.jpg', '.jpeg', '.png', '.gif' ].include?(File.extname(zipentry.name).downcase) || zipentry.size < 1000
1169
+ upload_progress.message = "Extracting #{File.basename(zipentry.name)}"
1170
+ localfile = File.join(temp_location, ((last_id+1).to_s + File.extname(zipentry.name)).downcase)
1171
+
1172
+ begin
1173
+ zipentry.extract(localfile)
1174
+ last_id += 1
1175
+ rescue StandardError => e
1176
+ logger.error(e)
1177
+ end
1178
+ end
1179
+ rescue StandardError => e
1180
+ logger.debug params.inspect
1181
+ logger.error(e)
1182
+ finish_upload_status "''" and return
1183
+ end
1184
+
1185
+ @images = Dir.glob("#{temp_location}/*.{jpg,jpeg,png,gif}")
1186
+
1187
+ @images.each do |img|
1188
+ localfile = File.join(@gallery_dir, File.basename(img, File.extname(img))) + '.jpg'
1189
+ tempfile = File.join(temp_location, File.basename(img, File.extname(img))) + File.extname(img)
1190
+ thumbfile = File.join(@gallery_dir, File.basename(img, File.extname(img))) + '-thumb.jpg'
1191
+
1192
+ small = MiniMagick::Image.open(tempfile)
1193
+ small.crop_resized(GalleryThumbWidth, GalleryThumbHeight)
1194
+ small.write(thumbfile)
1195
+
1196
+ FileUtils.cp(tempfile, localfile)
1197
+ resize_image(localfile)
1198
+
1199
+ File.chmod(0644, localfile, thumbfile)
1200
+ end
1201
+
1202
+ # smaller images for gallery index
1203
+ management_dir = File.join(@gallery_dir, 'management')
1204
+
1205
+ preview_images = []
1206
+ Dir.glob("#{@gallery_dir}/*.{jpg,jpeg,png,gif}").each { |img| preview_images << img unless File.basename(img).include?('thumb') }
1207
+
1208
+ preview_images.each { |img| create_preview_image(img, management_dir, true) }
1365
1209
  end
1366
1210
 
1367
- # resize if the resultant image is bigger than max dims
1368
- if max_width > 0 && max_height > 0
1369
- if orig_im[:width] > max_width || orig_im[:height] > max_height
1370
- logger.debug "resizing to max dims #{max_width} x #{max_height}"
1371
- orig_im.resize("#{max_width}x#{max_height}>")
1372
- dirty = true
1211
+ File.delete(data_dest)
1212
+
1213
+ upload_progress.message = "File received successfully."
1214
+ finish_upload_status "'#{File.basename(data_dest)}'" and return
1215
+ end
1216
+
1217
+ def delete_photo
1218
+ if request.post?
1219
+ @pg = CmsPage.find_by_id(params[:id])
1220
+
1221
+ # create blank captions.yml if it doesn't already exist
1222
+ create_captions_file(@pg.id)
1223
+
1224
+ gallery_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path, params[:gallery_id])
1225
+ captions = YAML.load(File.open(File.join(gallery_dir, 'captions.yml')).read).to_a
1226
+
1227
+ image_id = params[:image].split('.').first.to_i
1228
+
1229
+ begin ; File.delete(File.join(gallery_dir, image_id.to_s + '.jpg')) ; rescue ; end
1230
+ begin ; File.delete(File.join(gallery_dir, image_id.to_s + '-thumb.jpg')) ; rescue ; end
1231
+ begin ; File.delete(File.join(gallery_dir, 'management', image_id.to_s + '.jpg')) ; rescue ; end
1232
+
1233
+ all_images = Dir.glob(File.join(gallery_dir, '*.{jpg,jpeg,png,gif}'))
1234
+ images = []
1235
+ all_images.each { |img| images << img if !File.basename(img).include?('thumb') && File.basename(img).split('.').first.to_i > image_id }
1236
+
1237
+ image_names = []
1238
+ images.each_with_index { |img, index| image_names << File.basename(img).split('.').first.to_i }
1239
+ image_names.sort!
1240
+
1241
+ new_captions = []
1242
+ for i in 0...image_id do
1243
+ new_captions[i] = captions[i] || ''
1373
1244
  end
1245
+
1246
+ image_names.each do |img|
1247
+ FileUtils.mv(File.join(gallery_dir, img.to_s + '.jpg'), File.join(gallery_dir, image_id.to_s + '.jpg'))
1248
+ FileUtils.mv(File.join(gallery_dir, img.to_s + '-thumb.jpg'), File.join(gallery_dir, image_id.to_s + '-thumb.jpg'))
1249
+ FileUtils.mv(File.join(gallery_dir, 'management', img.to_s + '.jpg'), File.join(gallery_dir, 'management', image_id.to_s + '.jpg'))
1250
+
1251
+ new_captions[image_id] = captions[img] || ''
1252
+
1253
+ image_id += 1
1254
+ end
1255
+
1256
+ yaml = YAML.dump(new_captions)
1257
+ File.open(File.join(gallery_dir, 'captions.yml'), "w") { |f| f << yaml }
1374
1258
  end
1375
1259
 
1376
- orig_im.write(localfile) if dirty
1377
- File.chmod(0644, localfile)
1260
+ redirect_to :action => 'gallery_management', :id => params[:id], :gallery_id => params[:gallery_id]
1261
+ end
1262
+
1263
+ def delete_gallery
1264
+ @pg = CmsPage.find_by_id(params[:id])
1265
+ galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1266
+ gallery_dir = File.join(galleries_dir, params[:gallery_id])
1378
1267
 
1379
- @image_file = localfile + "?#{File.mtime(localfile).to_i}"
1380
- File.unlink testfile
1381
- upload_to_s3(localfile, @pg)
1268
+ FileUtils.rm_rf(gallery_dir)
1382
1269
 
1383
- render :partial => 'crop_results_feature_image'
1270
+ redirect_to :action => 'select_gallery', :id => params[:id]
1384
1271
  end
1385
-
1386
-
1272
+
1273
+
1274
+
1387
1275
  protected
1388
1276
 
1389
- def cms_page_params
1390
- params.require(:pg).permit(:cms_template_id, :cms_template_version, :parent_id, :published_version,
1391
- :name, :title, :path, :html_head, :summary, :position,
1392
- :article_date, :article_end_date, :published_date, :expiration_date, :expires,
1393
- :thumbnail_path, :feature_image_path, :redirect_enabled, :redirect_to)
1277
+ def check_permissions
1278
+ if !user_has_permission?(:manage_cms)
1279
+ render '/imagine_cms/errors/permission_denied', :layout => false
1280
+ return false
1281
+ end
1394
1282
  end
1395
-
1396
- def cms_template_params
1397
- params.require(:temp).permit(:name, :content)
1283
+
1284
+ def validate_user_access
1285
+ unless @user.cms_allowed_sections.to_s.strip.blank?
1286
+ allowed_sections = @user.cms_allowed_sections.split(',').map { |s| s.strip }.reject { |s| s.blank? }
1287
+ if @pg
1288
+ path = '/' + @pg.path
1289
+ else
1290
+ parent = CmsPage.find_by_id(params[:parent_id] || params[:pg][:parent_id]) rescue nil
1291
+ return false if !parent
1292
+ path = '/' + parent.path
1293
+ end
1294
+
1295
+ allowed = false
1296
+ allowed_sections.each { |s| allowed ||= (path =~ /^#{s}/) }
1297
+
1298
+ if !allowed
1299
+ respond_to do |wants|
1300
+ wants.js { render :text => "Sorry, you don't have permission to edit this page." }
1301
+ wants.html { redirect_to "/#{@pg.path}#{@pg.path == '' ? '' : '/'}version/#{@pg.version}" }
1302
+ end
1303
+ return false
1304
+ end
1305
+ end
1306
+
1307
+ true
1398
1308
  end
1399
-
1400
- def cms_snippet_params
1401
- params.require(:snip).permit(:name, :content)
1309
+
1310
+ def cms_page_params
1311
+ params.require(:pg).permit(:cms_template_id, :cms_template_version, :parent_id, :published_version,
1312
+ :name, :title, :path, :html_head, :summary, :position,
1313
+ :article_date, :article_end_date, :published_date, :expiration_date, :expires,
1314
+ :thumbnail_path, :feature_image_path, :redirect_enabled, :redirect_to)
1402
1315
  end
1403
1316
 
1404
1317
  def load_page_objects
1405
- @page_objects = HashObject.new
1406
- @template_options = HashObject.new
1318
+ @page_objects = OpenStruct.new
1319
+ @template_options = OpenStruct.new
1407
1320
 
1408
1321
  if @pg.new_record? && @parent
1409
- @parent.objects.where(:obj_type => 'attribute').each do |obj|
1322
+ @parent.objects.where(obj_type: 'attribute').each do |obj|
1323
+ next if defined?(CmsNewPagesDoNotInherit) && CmsNewPagesDoNotInherit['attributes'] && CmsNewPagesDoNotInherit['attributes'].include?(obj.name)
1410
1324
  key = "obj-#{obj.obj_type.to_s}-#{obj.name}"
1411
1325
  @page_objects[key] = obj.content
1412
1326
  end
1413
- @parent.objects.where(:obj_type => 'option').each do |obj|
1327
+ @parent.objects.where(obj_type: 'option').each do |obj|
1328
+ next if defined?(CmsNewPagesDoNotInherit) && CmsNewPagesDoNotInherit['options'] && CmsNewPagesDoNotInherit['options'].include?(obj.name)
1414
1329
  key = "obj-#{obj.obj_type.to_s}-#{obj.name}"
1415
1330
  @page_objects[key] = obj.content
1416
1331
  end
1417
1332
  else
1418
1333
  @tags = @pg.tags.map { |t| t.name }.join(', ')
1419
- @pg.objects.where(:obj_type => 'attribute').each do |obj|
1334
+ @pg.objects.where(obj_type: 'attribute').each do |obj|
1420
1335
  key = "obj-#{obj.obj_type.to_s}-#{obj.name}"
1421
1336
  @page_objects[key] = obj.content
1422
1337
  end
1423
- @pg.objects.where(:obj_type => 'option').each do |obj|
1338
+ @pg.objects.where(obj_type: 'option').each do |obj|
1424
1339
  key = "obj-#{obj.obj_type.to_s}-#{obj.name}"
1425
1340
  @page_objects[key] = obj.content
1426
1341
  end
@@ -1429,16 +1344,43 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1429
1344
 
1430
1345
  def load_template_options
1431
1346
  begin
1432
- render_to_string :inline => @pg.template.content
1433
- rescue Exception => e
1347
+ render_to_string inline: @pg.template.content
1348
+ rescue StandardError => e
1434
1349
  logger.debug e
1435
1350
  end
1436
1351
  end
1437
1352
 
1438
- def garbage_collect
1439
- GC.start
1353
+ def upload_to_s3(filename, page, bucket = nil, prefix = nil)
1354
+ s3retries = 0
1355
+ s3success = false
1356
+
1357
+ if ImagineCmsConfig['amazon_s3'] && ImagineCmsConfig['amazon_s3']['enabled']
1358
+ s3 = Aws::S3::Client.new
1359
+
1360
+ bucket ||= ImagineCmsConfig['amazon_s3'][Rails.env]['image_bucket']
1361
+ prefix ||= ImagineCmsConfig['amazon_s3']['image_prefix']
1362
+
1363
+ params = {}
1364
+ params[:bucket] = bucket
1365
+ params[:key] = "#{prefix}/#{page.path.blank? ? 'index' : page.path}/#{File.basename(filename)}"
1366
+ params[:body] = open(filename)
1367
+ params[:acl] = 'public-read'
1368
+ params[:metadata] = ImagineCmsConfig['amazon_s3']['metadata']
1369
+
1370
+ while !s3success && s3retries < 2
1371
+ response = s3.put_object(params)
1372
+ s3success = response.successful?
1373
+ s3retries += 1
1374
+ end
1375
+ File.unlink(filename) if s3success
1376
+ end
1377
+
1378
+ s3success
1440
1379
  end
1441
1380
 
1381
+
1382
+
1383
+
1442
1384
  def create_captions_file(pg_id, options = {})
1443
1385
  @pg ||= CmsPage.find_by_id(pg_id)
1444
1386
  gallery_id = options[:gallery_id] || params[:gallery_id]
@@ -1467,12 +1409,12 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1467
1409
  ret['autoplay'] ||= true
1468
1410
  ret['show_thumbs'] ||= true
1469
1411
 
1470
- return HashObject.new(ret)
1412
+ return OpenStruct.new(ret)
1471
1413
  end
1472
1414
 
1473
1415
  # prerequisites: @pg (CmsPage)
1474
1416
  def save_gallery_settings_to_file(gallery_id, settings_hash, options = {})
1475
- settings_hash = settings_hash.hash if settings_hash.kind_of?(HashObject)
1417
+ settings_hash = settings_hash.hash if settings_hash.kind_of?(OpenStruct)
1476
1418
 
1477
1419
  galleries_dir = File.join(Rails.root, 'public', 'assets', 'content', @pg.path)
1478
1420
  gallery_dir = File.join(galleries_dir, gallery_id)
@@ -1482,7 +1424,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1482
1424
  end
1483
1425
 
1484
1426
  def resize_image(localfile)
1485
- im = MiniMagick::Image::from_file(localfile)
1427
+ im = MiniMagick::Image.open(localfile)
1486
1428
 
1487
1429
  if im[:width] > GalleryMaxWidth || im[:height] > GalleryMaxHeight
1488
1430
  im.resize("#{GalleryMaxWidth}x#{GalleryMaxHeight}")
@@ -1492,11 +1434,11 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1492
1434
  localfile
1493
1435
  end
1494
1436
 
1495
- def create_preview_image(src_file, dest, force = 0, overlay = 'gallery_small_overlay.png', thumb_size = 90)
1437
+ def create_preview_image(src_file, dest, force = false, overlay = 'gallery_small_overlay.png', thumb_size = 90)
1496
1438
  raise "Source file required" if src_file.blank?
1497
1439
 
1498
1440
  dest = File.join(dest, File.basename(src_file)) if File.directory?(dest)
1499
- if !File.exist?(dest) || force == 1
1441
+ if !File.exist?(dest) || force
1500
1442
  logger.debug "Reading source file #{src_file}"
1501
1443
  im = Magick::Image::read(src_file)[0]
1502
1444
  im_overlay = Magick::Image::read(File.join(ImagineCms::Engine.root, 'app', 'assets', 'images', 'management', overlay))[0]
@@ -1539,7 +1481,7 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1539
1481
  # photo preview images
1540
1482
  preview_images.each { |img| create_preview_image(img, management_dir, options[:force]) }
1541
1483
 
1542
- rescue Exception => e
1484
+ rescue StandardError => e
1543
1485
  # some error handling here
1544
1486
  session[:broken_galleries] << File.basename(g)
1545
1487
 
@@ -1547,37 +1489,9 @@ class Management::CmsController < Management::ApplicationController # :nodoc:
1547
1489
  end
1548
1490
  end
1549
1491
  end
1550
-
1551
- def upload_to_s3(filename, page, bucket = nil, prefix = nil)
1552
- s3retries = 0
1553
- s3success = false
1554
-
1555
- if ImagineCmsConfig['amazon_s3'] && ImagineCmsConfig['amazon_s3']['enabled']
1556
- s3 = Aws::S3::Client.new
1557
-
1558
- bucket ||= ImagineCmsConfig['amazon_s3'][Rails.env]['image_bucket']
1559
- prefix ||= ImagineCmsConfig['amazon_s3']['image_prefix']
1560
-
1561
- params = {}
1562
- params[:bucket] = bucket
1563
- params[:key] = "#{prefix}/#{page.path.blank? ? 'index' : page.path}/#{File.basename(filename)}"
1564
- params[:body] = open(filename)
1565
- params[:acl] = 'public-read'
1566
- params[:metadata] = ImagineCmsConfig['amazon_s3']['metadata']
1567
-
1568
- while !s3success && s3retries < 2
1569
- response = s3.put_object(params)
1570
- s3success = response.successful?
1571
- s3retries += 1
1572
- end
1573
- File.unlink(filename) if s3success
1574
- end
1575
-
1576
- s3success
1577
- end
1578
-
1579
- end
1580
1492
 
1493
+ end
1494
+
1581
1495
  module MiniMagick
1582
1496
  class Image
1583
1497
  def crop_resized(ncols, nrows, gravity='Center')