//
// The Antville Project
// http://code.google.com/p/antville
//
// Copyright 2001-2007 by The Antville People
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Revision$
// $LastChangedBy$
// $LastChangedDate$
// $URL$
//

app.addRepository("modules/helma/Aspects.js");

var aspects = {
   setTopics: function(args, func, obj) {
      var param = args[0];
      //res.debug("aspects before: "+param.tags.toSource());
      var topic = param.topic || param.addToTopic;
      if (!param.tags && topic) {
         param.tags = [topic];
      }
      //res.debug("aspects after: " +param.tags.toSource());
      return args;
   },
   
   fixMacroParams: function(args) {
      var param = args[0];
      var id = args[1] || param.name || param.id;
      var mode = args[2] || param.as;
      var url = param.linkto;
      !param.skin && (param.skin = param.useskin);
      delete(param.name);
      delete(param.as);
      delete(param.linkto);
      return [param, id, mode, url];
   },
   
   fixStoryEditorParams: function(args, func, story) {
      if (req.isPost() && (req.postParams.save != 1)) {
         if (req.postParams.publish) {
            req.postParams.save = 1;
         }
         if (req.postParams.save != 1) {
            req.postParams.save = 1;
            req.postParams.status = Story.CLOSED;
         } else if (req.postParams.editableby) {
            req.postParams.status = req.postParams.editableby
         }
         req.postParams.mode = (req.postParams.addToFront ? 
               Story.FEATURED : Story.HIDDEN);
         req.postParams.commentMode = (req.postParams.discussions ? 
               Story.OPEN : Story.CLOSED);
      }
      //aspects.setTextAndTitle(args, func, story);
      return args;
   },
   
   fixPager: function(args, func, obj) {
      var archive = res.handlers.day = obj.archive || obj;
      res.push();
      archive.stories_macro();
      res.data.storylist = res.pop();
      res.push();
      archive.link_macro({}, "previous", gettext("Previous page"));
      res.data.prevpage = res.pop();
      res.push();
      archive.link_macro({}, "next", gettext("Next page"));
      res.data.nextpage = res.pop();
      return args;
   },
   
   // Obsolete?
   setTextAndTitle: function(args) {
      if (req.postParams.metadata_title && !req.postParams.title) {
         req.postParams.title = req.postParams.metadata_title;
         delete req.postParams.metadata_title;
      }
      if (req.postParams.metadata_text && !req.postParams.text) {
         req.postParams.text = req.postParams.metadata_text;
         delete req.postParams.metadata_text;
      }
      return args;
   }
}

helma.aspects.addAround(global, "image_macro", function(args, func) {
   args = aspects.fixMacroParams(args);
   var url = args[3];
   url && res.push();
   func.apply(global, args);
   url && link_filter(res.pop(), {}, url);
   return;
});

helma.aspects.addAround(global, "poll_macro", function(args, func) {
   return func.apply(global, aspects.fixMacroParams(args));
});

helma.aspects.addAround(global, "file_macro", function(args, func) {
   return func.apply(global, aspects.fixMacroParams(args));
});

helma.aspects.addBefore(global, "story_macro", function(args, func) {
   args = aspects.fixMacroParams(args);
   if (args[0].skin == "display") {
      args[0].skin = "content";
   }
   return args;
});

HopObject.prototype.onCodeUpdate = function() {
   helma.aspects.addAfter(this, "onRequest", function(args, func, obj) {
      res.handlers.members = res.handlers.site.members;
      res.handlers.membermgr = res.handlers.site.members;
      return args;
   });
   helma.aspects.addBefore(this, "skin_macro", function(args, func, obj) {
      param = args[0];
      return [param, args[1] || param.name];
   });
   return helma.aspects.addBefore(this, "link_macro", function(args, func, obj) {
      var param = args[0];
      return [param, args[1] || param.to, args[2] || param.text];
   });
}

// Call onCodeUpdate() to be sure that aspects apply
HopObject.prototype.onCodeUpdate();

Archive.prototype.onCodeUpdate = function() {
   return helma.aspects.addBefore(this, "main_action", aspects.fixPager);
}

/* Comment.prototype.onCodeUpdate = function() {
   helma.aspects.addBefore(this, "update", aspects.setTextAndTitle);
   return;
} */

Image.prototype.onCodeUpdate = function() {
   return helma.aspects.addBefore(this, "update", aspects.setTopics);
}

Images.prototype.onCodeUpdate = function() {
   return helma.aspects.addAround(this, "getPermission", function(args, func, images) {
      var permission = func.apply(images, args);
      if (!permission) {
         switch (args[0]) {
            case "topics":
            return true;
         }
      }
      return permission;
   });
}

Layout.prototype.onCodeUpdate = function() {
   return helma.aspects.addAround(this, "image_macro", function(args, func, layout) {
      args = aspects.fixMacroParams(args);
      var url = args[3];
      url && res.push();
      func.apply(layout, args);
      url && link_filter(res.pop(), {}, url);
      return;
   });
}

Members.prototype.onCodeUpdate = function() {
   helma.aspects.addAround(this, "getPermission", function(args, func, members) {
      var permission = func.apply(members, args);
      if (!permission) {
         switch(args[0]) {
            case "sendpwd":
            return true;
         }
      }
      return permission;
   });
}

Site.prototype.onCodeUpdate = function() {
   helma.aspects.addAround(this, "getPermission", function(args, func, site) {
      var permission = func.apply(site, args);
      if (!permission) {
         switch(args[0]) {
            case "rss":
            case "feeds":
            case "mostread":
            return true;
         }
      }
      return permission;
   });
   
   return helma.aspects.addBefore(this, "main_action", aspects.fixPager);
}

Story.prototype.onCodeUpdate = function() {
   helma.aspects.addBefore(this, "edit_action", aspects.fixStoryEditorParams);   
   return helma.aspects.addBefore(this, "update", aspects.setTopics);
}

Stories.prototype.onCodeUpdate = function() {
   return helma.aspects.addBefore(this, "create_action", 
         aspects.fixStoryEditorParams);
}
