      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    var poll = new Poll;
     78    if (req.postParams.save) {
     79       try {
     80          poll.update(req.postParams);
     81          this.add(poll);
     82          poll.notify(req.action);
     83          res.message = gettext("The poll was created successfully.");
     84          res.redirect(poll.href());
     85       } catch (err) {
     86          res.message = err.toString();
     87       }
     88    } else {
     89       req.postParams.title_array = [,,];
     90    }
     91    res.data.action = this.href(req.action);
     92    res.data.title = gettext("Add Poll");
     93    res.data.body = poll.renderSkinAsString("$Poll#edit");
     94    this._parent.renderSkin("Site#page");
     95    return;
     96 }
     97
     98 Polls.prototype.running_action = function() {
     99    res.data.list = renderList(this.running,
    100          "$Poll#listItem", 10, req.queryParams.page);
    101    res.data.pager = renderPager(this.running,
    102          this.href(req.action), 10, req.queryParams.page);
    103    res.data.title = gettext("Running Polls");
    104    res.data.body = this.renderSkinAsString("$Polls#main");
    105    this._parent.renderSkin("Site#page");
    106    return;
    107 }
    108
    109 Polls.prototype.all_action = function() {
    110    res.data.list = renderList(this, "$Poll#listItem", 10, req.queryParams.page);
    111    res.data.pager = renderPager(this,
    112          this.href(), 10, req.queryParams.page);
    113    res.data.title = gettext("All Polls");
    114    res.data.body = this.renderSkinAsString("$Polls#main");
    115    this._parent.renderSkin("Site#page");
    116    return;
    117 }
    118