1 // The Antville Project
  2 // http://code.google.com/p/antville
  3 //
  4 // Copyright 2007-2011 by Tobi Schäfer.
  5 //
  6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
  7 // Matthias & Michael Platzer, Christoph Lincke.
  8 //
  9 // Licensed under the Apache License, Version 2.0 (the ``License'');
 10 // you may not use this file except in compliance with the License.
 11 // You may obtain a copy of the License at
 12 //
 13 //    http://www.apache.org/licenses/LICENSE-2.0
 14 //
 15 // Unless required by applicable law or agreed to in writing, software
 16 // distributed under the License is distributed on an ``AS IS'' BASIS,
 17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18 // See the License for the specific language governing permissions and
 19 // limitations under the License.
 20 //
 21 // $Revision$
 22 // $Author$
 23 // $Date$
 24 // $URL$
 25 
 26 /**
 27  * @fileOverview Defines the Images prototype
 28  */
 29 
 30 markgettext("Images");
 31 markgettext("images");
 32 
 33 /**
 34  * @name Images
 35  * @constructor
 36  * @property {Image} _children
 37  * @property {Tag[]} alphabeticalGalleries 
 38  * @property {Tag[]} galleries
 39  * @property {Tag[]} otherGalleries
 40  * @extends HopObject
 41  */
 42 
 43 /**
 44  * 
 45  * @param {String} action
 46  * @returns {Boolean}
 47  */
 48 Images.prototype.getPermission = function(action) {
 49    if (!this._parent.getPermission("main")) {
 50       return false;
 51    }
 52    switch (action) {
 53       case ".":
 54       case "main":
 55       case "create":
 56       // FIXME: case "tags":
 57       return Site.require(Site.OPEN) && session.user ||
 58             Membership.require(Membership.CONTRIBUTOR) ||
 59             User.require(User.PRIVILEGED);
 60       case "all":
 61       return this._parent.constructor !== Layout &&
 62             (Membership.require(Membership.MANAGER) ||
 63             User.require(User.PRIVILEGED));
 64    }
 65    return false;
 66 }
 67 
 68 Images.prototype.main_action = function() {
 69    var images, skin;
 70    switch (this._parent.constructor) {
 71       case Root:
 72       case Site:
 73       images = User.getMembership().images;
 74       skin = "$Images#main";
 75       res.data.title = gettext("Member Images");
 76       break;
 77       
 78       case Layout:
 79       images = res.handlers.layout.images;
 80       skin = "$Images#layout";
 81       res.data.title = gettext("Layout Images");
 82       break;
 83    }
 84    res.data.list = renderList(images, "$Image#listItem", 
 85          10, req.queryParams.page);
 86    res.data.pager = renderPager(images, 
 87          this.href(req.action), 10, req.queryParams.page);
 88    res.data.body = this.renderSkinAsString(skin);
 89    res.handlers.site.renderSkin("Site#page");
 90    return;
 91 }
 92 
 93 Images.prototype.create_action = function() {
 94    File.redirectOnUploadError(this.href(req.action));
 95    File.redirectOnExceededQuota(this.href());
 96 
 97    if (req.data.save) {
 98       try {
 99          var image = Image.add(req.params, this._parent);
100          image.notify(req.action);
101          JSON.sendPaddedResponse(image._id);
102          res.message = gettext('The image was successfully added.');
103          res.redirect(image.href());
104       } catch (ex) {
105          JSON.sendPaddedResponse(null);
106          res.status = 400;
107          res.message = ex.toString();
108          app.log(ex);
109       }
110    }
111 
112    res.data.action = this.href(req.action);
113    res.data.title = gettext("Add Image");
114    res.data.body = (new Image).renderSkinAsString("$Image#edit");
115    res.handlers.site.renderSkin("Site#page");
116    return;
117 }
118 
119 Images.prototype.all_action = function() {
120    res.data.pager = renderPager(this, this.href(req.action), 
121          10, req.queryParams.page);
122    res.data.list = renderList(this, "$Image#listItem", 
123          10, req.queryParams.page);
124    res.data.title = gettext("All Images");
125    res.data.body = this.renderSkinAsString("$Images#main");
126    res.handlers.site.renderSkin("Site#page");
127    return;
128 }
129 
130 /**
131  * @namespace
132  * @field
133  */
134 Images.Default = new function() {
135    var Image = function(name, description) {
136       var dir = new helma.File(app.appsProperties['static'], "www");
137       var image = new helma.Image(new helma.File(dir, name));
138       this.name = name;
139       this.description = description;
140       this.width = image.width;
141       this.height = image.height;
142       this.getUrl = function() {
143          return app.appsProperties.staticMountpoint + "/www/" + name;
144       }
145       this.render_macro = global.Image.prototype.render_macro;
146       this.thumbnail_macro = global.Image.prototype.thumbnail_macro;
147       return this;
148    }
149 
150    var images = {};
151    var add = function(name, description) {
152       images[name] = new Image(name, description);
153       return;
154    }
155    add("ant.png", "Ant");
156    add("ant-icon.png", "Tiny Ant");
157    add("big.gif", String.EMPTY);
158    add("bullet.gif", "*");
159    add("dot.gif", String.EMPTY);
160    add("headbg.gif", String.EMPTY);
161    add("helma.png", "Helma Object Publisher");
162    add("hop.gif", "Helma Object Publisher");
163    add("manage.gif", "manage");
164    add("marquee.gif", String.EMPTY);
165    add("menu.gif", "menu");
166    add("pixel.gif", String.EMPTY);
167    add("recent.gif", "recent");
168    add("rss.png", "RSS feed");
169    add("smallanim.gif", "Made with Antville");
170    add("smallchaos.gif", "Made with Antville");
171    add("smallstraight.gif", "Made with Antville");
172    add("smalltrans.gif", "Made with Antville");
173    add("status.gif", "status");
174    add("webloghead.gif", "Antville");
175    add("xmlbutton.gif", "XML version of this page");
176    return images;
177 }
178 
179 /**
180  * @returns {Image[]}
181  */
182 Images.prototype.mergeImages = function() {
183    var images = [];
184    var layout = this._parent;
185    while (layout) {
186       layout.images.forEach(function() {
187          if (images.indexOf(this) < 0) {
188             images.push(this);
189          }
190          return;
191       });
192       layout = layout.parent;
193    }
194    return images.sort(Number.Sorter("created", Number.Sorter.DESC));
195 }
196 
197 /**
198  * 
199  * @param {String} group
200  * @returns {Tag[]}
201  * @see Site#getTags
202  */
203 Images.prototype.getTags = function(group) {
204    return this._parent.getTags("galleries", group);
205 }
206