antville/code/Root/Root.js

319 lines
9.7 KiB
JavaScript
Raw Normal View History

2007-06-22 19:07:28 +00:00
//
// The Antville Project
// http://code.google.com/p/antville
2007-06-22 19:07:28 +00:00
//
// 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$
2007-06-22 19:07:28 +00:00
//
* Fixed reference to parent site in Archive * Fixed _children.filter in Archive * Added missing permission checks * Modified global defineConstants() method to return the getter function instead of automatically defining it with given argument * Added HopObject.macro_macro() method to display userland macro code * Removed colorpicker (will be replaced by third-party library) * Removed obsolete global constants and functions * Overhauled and tested global userland macros like story_macro(), image_macro() etc. * Implemented global list_macro() to replace any special listFoobar_macro() methods * Moved global autoLogin() method into User prototype * Overhauled global randomize_macro() * Renamed global evalURL() method to validateUrl() as well as evalEmail() to validateEmail() * Re-added accidentally removed subskins to Members.skin * Fixed some skin names which were changed recently * Remove delete_action() from Membership * Fixed foreign key of images collection in Membership * Removed global username_macro() and replaced it with appropriate membership macros * Moved contents of systemscripts.skin into javascript.skin in Root prototype * Removed main_css_action(), main_js_action() and sitecounter_macro() methods from Root * Added accessname to sites collection in Root * Upgraded jQuery to version 1.2.1 * Replaced call for global history_macro() with corresponding list_macro() call * Renamed "public" collection of Stories prototype to "featured" * Moved a lot of styles from Root's style.skin to the one in Site * Added comments collection to Site * Moved embed.skin as subskin #embed into Site.skin * Fixed some minor issues in Story.js (removed check for creator before setting the story's mode) * Defined cookie names as constants of User which can be overriden via app.properties userCookie and hashCookie * Moved a lot of code into compatibility module
2007-10-11 23:03:17 +00:00
Root.getScopes = defineConstants(Root, markgettext("every site"),
markgettext("public sites"), markgettext("trusted sites"),
markgettext("no site"));
this.handleMetadata("notificationScope");
this.handleMetadata("quota");
this.handleMetadata("creationScope");
this.handleMetadata("creationDelay");
this.handleMetadata("qualifyingPeriod");
this.handleMetadata("qualifyingDate");
this.handleMetadata("autoCleanupEnabled");
this.handleMetadata("autoCleanupStartTime");
this.handleMetadata("phaseOutPrivateSites");
this.handleMetadata("phaseOutInactiveSites");
this.handleMetadata("phaseOutNotificationPeriod");
this.handleMetadata("phaseOutGracePeriod");
Root.prototype.getPermission = function(action) {
if (action.contains("admin")) {
return User.require(User.PRIVILEGED);
}
switch (action) {
case "create":
return this.getCreationPermission();
case "list":
case "updates.xml":
return this.mode !== Site.CLOSED;
}
return Site.prototype.getPermission.apply(this, arguments);
};
Root.prototype.getMacroHandler = function(name) {
switch (name) {
case "sites":
case "admin":
return this[name];
}
return Site.prototype.getMacroHandler.apply(this, arguments);
};
Root.prototype.main_action = function() {
return Site.prototype.main_action.apply(this);
//log();
flushLog();
res.debug(root.admin.log.cache.size());
return;
/* FIXME: setup routine needs to be rewritten
2007-06-22 19:07:28 +00:00
// check if this installation is already configured
// if not, we display the welcome-page as frontpage
if (!root.sys_issetup) {
if (!root.users.size()) {
res.data.body = this.renderSkinAsString("welcome");
root.renderSkin("page");
return;
} else
res.redirect(this.manage.href("setup"));
} else if (!root.size())
res.redirect(this.href("new"));
*/
2007-06-22 19:07:28 +00:00
res.data.body = root.renderSkinAsString("main");
root.renderSkin("page");
2007-06-22 19:07:28 +00:00
return;
};
2007-06-22 19:07:28 +00:00
Root.prototype.getFormOptions = function(name) {
switch (name) {
case "notificationScope":
return Root.getScopes();
case "creationScope":
return User.getScopes();
case "autoCleanupStartTime":
return Admin.getHours();
return;
}
return Site.prototype.getFormOptions.apply(this, arguments);
};
Root.prototype.error_action = function() {
res.data.title = root.getTitle() + " - Error";
res.data.body = root.renderSkinAsString("sysError");
res.data.body += "<p>"+res.error+"</p>";
(path.Site && path.Site.online ? path.Site : root).renderSkin("page");
return;
};
Root.prototype.notfound_action = function() {
res.data.title = root.title + " - 404 - not found";
req.data.path = req.path;
res.data.body = root.renderSkinAsString("notfound");
(path.Site && path.Site.online ? path.Site : root).renderSkin("page");
return;
};
Root.prototype.create_action = function() {
var site = new Site;
if (req.postParams.create) {
2007-06-22 19:07:28 +00:00
try {
site.update(req.postParams);
this.add(site);
site.members.add(new Membership(session.user, Membership.OWNER));
logAction(site, "added");
res.message = gettext("Successfully created your site.");
res.redirect(site.href());
} catch (ex) {
res.message = ex;
app.log(ex);
2007-06-22 19:07:28 +00:00
}
}
res.data.action = this.href(req.action);
res.data.title = gettext("Create a new site");
res.data.body = site.renderSkinAsString("Site#create");
2007-06-22 19:07:28 +00:00
root.renderSkin("page");
return;
};
2007-06-22 19:07:28 +00:00
Root.prototype.list_action = function() {
// FIXME:
2007-06-22 19:07:28 +00:00
// preparing res.data.sitelist and prev/next links
// ("all" shows all sites, "yes" is for scrolling)
this.renderSitelist(25, "all", "yes");
res.data.title = getMessage("SysMgr.listTitle", {serverTitle: root.getTitle()});
res.data.body = this.renderSkinAsString("list");
root.renderSkin("page");
return;
};
2007-06-22 19:07:28 +00:00
Root.prototype.updates_xml_action = function() {
var now = new Date;
var feed = new rome.SyndFeedImpl();
feed.setFeedType("rss_2.0");
feed.setLink(root.href());
feed.setTitle(root.title);
feed.setDescription(root.tagline);
feed.setLanguage(root.language.replace("_", "-"));
feed.setPublishedDate(now);
var entries = new java.util.ArrayList();
var entry, description;
for each (var site in root.sites.list(0, 25)) {
entry = new rome.SyndEntryImpl();
entry.setTitle(site.title);
entry.setLink(site.href());
entry.setAuthor(site.creator.name);
entry.setPublishedDate(site.created);
description = new rome.SyndContentImpl();
description.setType("text/plain");
description.setValue(site.tagline);
entry.setDescription(description);
entries.add(entry);
2007-06-22 19:07:28 +00:00
}
feed.setEntries(entries);
var output = new rome.SyndFeedOutput();
//output.output(feed, res.servletResponse.writer); return;
var xml = output.outputString(feed);
res.contentType = "text/xml";
res.write(injectXslDeclaration(xml));
2007-06-22 19:07:28 +00:00
return;
};
2007-06-22 19:07:28 +00:00
Root.prototype.getCreationPermission = function() {
var user;
if (!(user = session.user)) {
return false;
} if (User.require(User.PRIVILEGED)) {
return true;
}
switch (root.creationScope) {
case User.PRIVILEGEDUSERS:
return false;
case User.TRUSTEDUSERS:
return User.require(User.TRUSTED);
default:
case User.ALLUSERS:
if (root.qualifyingPeriod) {
var days = Math.floor((new Date - user.created) / Date.ONEDAY);
if (days < root.qualifyingPeriod) {
//throw Error(gettext("Sorry, you have to be a member for at " +
// "least {0} days to create a new site.",
// formatDate(root.qualifyingPeriod));
return false;
}
} else if (root.qualifyingDate) {
if (user.created > root.qualifyingDate) {
//throw Error(gettext("Sorry, only members who have registered " +
// "before {0} are allowed to create a new site.",
// formatDate(root.qualifyingDate));
return false;
}
}
if (user.sites.count() > 0) {
var days = Math.floor((new Date - user.sites.get(0).created) /
Date.ONEDAY);
if (days < root.creationDelay) {
//throw Error(gettext("Sorry, you still have to wait {0} days " +
// "before you can create another site.",
// root.creationDelay - days));
return false;
}
}
}
return true;
};
2007-06-22 19:07:28 +00:00
Root.prototype.searchSites = function(query, sid) {
2007-06-22 19:07:28 +00:00
// result array
var result = new Array();
// break up search string
var unquote = new RegExp("\\\\", "g");
query = query.replace(unquote, "\\\\");
unquote = new RegExp("\'", "g");
query = query.replace(unquote, "\'\'");
var qarr = query.split(" ");
// construct query
var where = "select AV_TEXT.TEXT_ID, site.name from AV_TEXT, site "+
"where AV_TEXT.TEXT_F_SITE = site.id " +
"and AV_TEXT.TEXT_ISONLINE > 0 and ";
2007-06-22 19:07:28 +00:00
for (var i in qarr) {
where += "(AV_TEXT.TEXT_RAWCONTENT like '%" + qarr[i].toLowerCase() +
"%') ";
2007-06-22 19:07:28 +00:00
if (i < qarr.length-1)
where += "and ";
}
// search only in the specified site
if (sid)
where += "and site.id = " + sid + " ";
2007-06-22 19:07:28 +00:00
else
where += "and site.mode = 'online' ";
2007-06-22 19:07:28 +00:00
where += "order by AV_TEXT.TEXT_CREATETIME desc";
var dbcon = getDBConnection ("antville");
var dbres = dbcon.executeRetrieval(where);
if (dbres) {
while (dbres.next()) {
var item = new Object();
item.sid = dbres.getColumnItem (1).toString();
item.sitealias = dbres.getColumnItem (2);
result[result.length] = item;
}
}
dbres.release();
return result;
};
2007-06-22 19:07:28 +00:00
Root.prototype.processHref = function(href) {
2007-06-22 19:07:28 +00:00
return app.properties.defaulthost + href;
};
2007-06-22 19:07:28 +00:00
Root.prototype.renderSitelist = function(limit, show, scroll) {
2007-06-22 19:07:28 +00:00
if (show && show == "all")
var collection = root.publicSites;
else
var collection = root;
var size = collection.size();
if (!size)
return;
var idx = parseInt (req.data.start, 10);
var scroll = (!scroll || scroll == "no" ? false : true);
if (isNaN(idx) || idx > size-1 || idx < 0)
idx = 0;
if (scroll && idx > 0) {
var sp = new Object();
sp.url = root.href("list") + "?start=" + Math.max(0, idx-limit);
sp.text = getMessage("Site.previousPage");
res.data.prevpage = renderSkinAsString("prevpagelink", sp);
}
var cnt = 0;
collection.prefetchChildren(idx, limit);
res.push();
while (cnt < limit && idx < size) {
var s = collection.get(idx++);
if (!s.blocked && s.online) {
* Fixed reference to parent site in Archive * Fixed _children.filter in Archive * Added missing permission checks * Modified global defineConstants() method to return the getter function instead of automatically defining it with given argument * Added HopObject.macro_macro() method to display userland macro code * Removed colorpicker (will be replaced by third-party library) * Removed obsolete global constants and functions * Overhauled and tested global userland macros like story_macro(), image_macro() etc. * Implemented global list_macro() to replace any special listFoobar_macro() methods * Moved global autoLogin() method into User prototype * Overhauled global randomize_macro() * Renamed global evalURL() method to validateUrl() as well as evalEmail() to validateEmail() * Re-added accidentally removed subskins to Members.skin * Fixed some skin names which were changed recently * Remove delete_action() from Membership * Fixed foreign key of images collection in Membership * Removed global username_macro() and replaced it with appropriate membership macros * Moved contents of systemscripts.skin into javascript.skin in Root prototype * Removed main_css_action(), main_js_action() and sitecounter_macro() methods from Root * Added accessname to sites collection in Root * Upgraded jQuery to version 1.2.1 * Replaced call for global history_macro() with corresponding list_macro() call * Renamed "public" collection of Stories prototype to "featured" * Moved a lot of styles from Root's style.skin to the one in Site * Added comments collection to Site * Moved embed.skin as subskin #embed into Site.skin * Fixed some minor issues in Story.js (removed check for creator before setting the story's mode) * Defined cookie names as constants of User which can be overriden via app.properties userCookie and hashCookie * Moved a lot of code into compatibility module
2007-10-11 23:03:17 +00:00
s.renderSkin("Site#list");
2007-06-22 19:07:28 +00:00
cnt++;
}
}
res.data.sitelist = res.pop();
if (scroll && idx < size) {
var sp = new Object();
sp.url = root.href("list") + "?start=" + idx;
sp.text = getMessage("Site.nextPage");
res.data.nextpage = renderSkinAsString("nextpagelink", sp);
}
return;
};