      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 // $LastChangedBy$
     23 // $LastChangedDate$
     24 // $URL$
     25
     26 /**
     27  * @fileOverview Defines the Polls prototype
     28  */
     29
     30 markgettext("Polls");
     31 markgettext("polls");
     32
     33 /**
     34  * @name Polls
     35  * @constructor
     36  * @property {Poll[]} _children
     37  * @property {Poll[]} open
     38  * @extends HopObject
     39  */
     40
     41 /**
     42  *
     43  * @param {String} action
     44  * @returns {Boolean}
     45  */
     46 Polls.prototype.getPermission = function(action) {
     47    if (!this._parent.getPermission("main")) {
     48       return false;
     49    }
     50    switch (action) {
     51       case ".":
     52       case "main":
     53       case "create":
     54       case "running":
     55       return Site.require(Site.OPEN) && session.user ||
     56             Membership.require(Membership.CONTRIBUTOR) ||
     57             User.require(User.PRIVILEGED);
     58       case "all":
     59       return Membership.require(Membership.MANAGER) ||
     60             User.require(User.PRIVILEGED);
     61    }
     62    return false;
     63 }
     64
     65 Polls.prototype.main_action = function() {
     66    var polls = User.getMembership().polls;
     67    res.data.list = renderList(polls, "$Poll#listItem", 10, req.queryParams.page);
     68    res.data.pager = renderPager(polls, this.href(req.action),
     69          10, req.queryParams.page);
     70    res.data.title = gettext("Member Polls");
     71    res.data.body = this.renderSkinAsString("$Polls#main");
     72    this._parent.renderSkin("Site#page");
     73    return;
     74 }
     75
     76 Polls.prototype.create_action = function() {
     77    if (req.postParams.save) {
     78       try {
     79          var poll = Poll.add(req.postParams, this._parent);
     80          poll.notify(req.action);
     81          res.message = gettext("The poll was created successfully.");
     82          res.redirect(poll.href());
     83       } catch (err) {
     84          res.message = err.toString();
     85       }
     86    } else {
     87       req.postParams.title_array = [null, null];
     88    }
     89    res.data.action = this.href(req.action);
     90    res.data.title = gettext("Add Poll");
     91    HopObject.confirmConstructor(Poll);
     92    res.data.body = (new Poll).renderSkinAsString("$Poll#edit");
     93    this._parent.renderSkin("Site#page");
     94    return;
     95 }
     96
     97 Polls.prototype.running_action = function() {
     98    res.data.list = renderList(this.running,
     99          "$Poll#listItem", 10, req.queryParams.page);
    100    res.data.pager = renderPager(this.running,
    101          this.href(req.action), 10, req.queryParams.page);
    102    res.data.title = gettext("Running Polls");
    103    res.data.body = this.renderSkinAsString("$Polls#main");
    104    this._parent.renderSkin("Site#page");
    105    return;
    106 }
    107
    108 Polls.prototype.all_action = function() {
    109    res.data.list = renderList(this, "$Poll#listItem", 10, req.queryParams.page);
    110    res.data.pager = renderPager(this,
    111          this.href(), 10, req.queryParams.page);
    112    res.data.title = gettext("All Polls");
    113    res.data.body = this.renderSkinAsString("$Polls#main");
    114    this._parent.renderSkin("Site#page");
    115    return;
    116 }
    117