📄 setupagendas.htm
字号:
</code></div>
</blockquote>
<h4>How to resolve conflicts between events and holidays? </h4>
<p>By default, the <code>fHoliday()</code> in <code>agenda.js</code> will favor events over holidays, which means if you use <code>fAddEvent()</code> against a holiday day, all the holiday effects will be totally replaced by the event ones. But what if you only want to replace partial effects and keep other effects unchanged? Let's show you how to do it with an example.</p>
<p>Suppose we want to keep the font color of the date number as well as the background image from the holiday and all other effects from the conflicted event. To make it work, we need to modify the <code>fHoliday()</code> a little, as following:</p>
<blockquote>
<div class="dottedBox" style="width:95%">
<p><code>function fHoliday(y,m,d) {<br>
var rE=fGetEvent(y,m,d), r=null;<br>
<br>
if (m==1&&d==1)<br>
r=[" Jan 1, "+y+" \n Happy New Year! ",gsAction,"skyblue","red"];<br>
... <br>
<br>
if (r&&rE) { rE[3]=r[3]; rE[4]=r[4]; return rE;} <br>
else return rE?rE:r;<br>
} <br>
</code></p>
</div>
</blockquote>
<p>The above modification interprets as "get the agenda for event of day (y,m,d) and store it in <code>rE</code>; also get agenda into <code>r</code> if (y,m,d) is a holiday; if event conflicts with holiday, then replace the 3rd and 4th element (fgcolor and bgimg) of the event agenda with the values from holiday; or return the existing agenda if nothing conflicts."</p>
<h4>Additional date calculations</h4>
<p>In addition to do the simple comparison of year and month, you could use
much more sophisticated formulas to check out the non-regular holidays like
Easter, Thanksgiving or whatever holiday in your culture. There are so many
standard calculations on the internet that can be used directly here.</p>
<p>You may just create some helper functions in plugins.js and then call them from
within the fHoliday function to verify against the date passed in. For example, we have a function that calculates the Easter Day and want to use it in the <code>fHoliday()</code> call. We first add the following code to the <code>plugins.js</code>:</p>
<blockquote>
<div class="dottedBox">
<p><code>function getEaster(year) {<br>
var a=year%19, b=Math.floor(year/100), c=year%100, d=Math.floor(b/4), e=b%4, f=Math.floor((b+8)/25), g=Math.floor((b-f+1)/3), h=(19*a+b-d-g+15)%30, i=Math.floor(c/4), k=c%4, l=(32+2*e+2*i-h-k)%7, m=Math.floor((a+11*h+22*l)/451), n=h+l-7*m+114;<br>
return [Math.floor(n/31),n%31+1];<br>
}</code></p>
</div>
</blockquote>
<p>then append the following code to the <code>fHoliday()</code> in <code>agenda.js</code>:</p>
<blockquote>
<div class="dottedBox">
<p><code>function fHoliday(y,m,d) {<br>
...<br>
var easter=getEaster(y);<br>
if (m==easter[0]&&d==easter[1]) r=[gMonths[m-1].substring(0,3)+" "+d+", "+y+" Easter Day ", gsAction,"skyblue","red"];<br>
<br>
return rE?rE:r; // favor events over holidays</code></p>
<p><code>} </code></p>
</div>
</blockquote>
<h4>Retrieving agenda events from backend data source</h4>
<p>Ever thought about making your agenda events manageable by using a backend
server/database? No kidding, it's not only possible but always simple with
CalendarXP.</p>
<p>Here we'll show you an example of how to generate a JavaScript file via JSP, which is capable of retrieve data from backend database. It's almost the same if you work with
ASP, PHP or other cgi techniques, you may easily figure it out provided you know how to code in that language.</p>
<ol>
<li> Suppose that you have got a table named "MY_AGENDAS"
in your database, and you have already defined a datasource named "jdbc/myDataSource" (you should have set up db user/password in the datasource).</li>
<li>Suppose there are 10 columns in table "MY_AGENDAS" in accordance
with the agenda format, as following: <code>ag_year,ag_month,ag_day,ag_message,ag_action,ag_bgcolor,ag_fgcolor,ag_bgimg,ag_boxit,ag_html</code>.</li>
<li>Build a JSP file named <code>agenda.jsp</code> as following:
<div class="dottedBox">
<p><code> <%@ page contentType="text/javascript" %><br>
<% // The above JSP code set the generated page to be recognized as
a javascript source. %><br>
<br>
<% java.sql.Connection conn=null;<br>
try {<br>
// Establish database connection<br>
javax.naming.Context ctx = new javax.naming.InitialContext();<br>
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/myDataSource");<br>
conn = ds.getConnection();<br>
<br>
// Execute SQL and get a result set<br>
java.sql.Statement stmt = conn.createStatement();<br>
java.sql.ResultSet rs = stmt.executeQuery("SELECT * FROM MY_AGENDAS");<br>
<br>
// Loop through the result set to generate multiple fAddEvent() functions<br>
while (rs.next()) {<br>
%><br>
<br>
fAddEvent(<%=rs.getString("ag_year")%>,<%=rs.getString("ag_month")%>,<%=rs.getString("ag_day")%>,<br>
'<%=rs.getString("ag_message")%>','<%=rs.getString("ag_action")%>','<%=rs.getString("ag_bgcolor")%>',<br>
'<%=rs.getString("ag_fgcolor")%>','<%=rs.getString("ag_bgimg")%>',<%=rs.getString("ag_boxit")%>,<br>
'<%=rs.getString("ag_html")%>');<br>
<br>
<% }<br>
<br>
// Close db connection and error handling<br>
rs.close();<br>
stmt.close();<br>
} catch (Exception e) {<br>
System.out.pringln("Service Error: "+e);<br>
} finally {<br>
if (conn!=null)<br>
try { conn.close() } catch (Exception ignore) {};<br>
}<br>
%><br>
<br>
function fHoliday(y,m,d) {<br>
... the javascript code for fHoliday ... <br>
}<br>
</code></p>
</div>
</li>
<li>Place <code>agenda.jsp</code> in your JSP server, suppose the absolute URL
path is <code>/myweb/agenda.jsp</code>. Change the 3rd parameter in the name&id
of the calendar tag, as following:
<div class="dottedBox"><code><iframe width=174 height=189 name="gToday:normal:/myweb/agenda.jsp"
id="gToday:normal:/myweb/agenda.jsp" src="iflateng.htm"
scrolling="no" frameborder="0"><br>
...<br>
</iframe><br>
...<br>
<layer name="gToday:normal:/myweb/agenda.jsp" src="nflateng.htm"> </layer></code></div>
</li>
</ol>
<p>That's all. Now all the agenda events should be loaded directly from your backend
database table MY_AGENDAS. <strong>If you come across any problem, be sure to point your browser to /myweb/agenda.jsp first and retrieve the generated page source. Open it with a notepad and check carefully to see if anything were wrong in the JavaScript syntax. </strong></p>
<p><span class="style1"><strong>Note:</strong></span> You should pay attention to using the quote char in ag_html,
be sure to escape it if needed.<br>
<span class="style1"><strong>Note:</strong></span> The <code><%@ page contentType="text/javascript" %></code> is JSP coding. If you are using other CGI programming language,
like php or asp, you should reference to its own way for how to set the content-type in the http header.<br>
<span class="style1"><strong>Note:</strong></span> The JSP code will print out "null" when the column value is NULL in DB, but other cgi like ASP and PHP might not work the same way and you should pay attention to dealing with the null value. The calendar engine will convert a "null" string into a null value automatically, also please remember that the boxit param is required to be a boolean value thus shouldn't be surrounded by the quotes. </p>
<h4>Another data source example using PHP and MySQL</h4>
<p> 1st, create a database table in MySQL with the following scripts: <br>
</p>
<table cellspacing="1" cellpadding="3">
<tr>
<td><strong>Code: </strong></td>
</tr>
<tr>
<td><div class="dottedBox">CREATE TABLE `my_agenda` ( <br>
`ag_year` smallint NOT NULL, <br>
`ag_month` smallint NOT NULL, <br>
`ag_day` smallint NOT NULL, <br>
`ag_message` varchar(255) default '', <br>
`ag_action` varchar(255) default '', <br>
`ag_bgcolor` varchar(20) default 'null', <br>
`ag_fgcolor` varchar(20) default 'null', <br>
`ag_bgimg` varchar(255) default 'null', <br>
`ag_boxit` varchar(20) default 'null', <br>
`ag_html` varchar(255) default 'null', <br>
PRIMARY KEY (`ag_year`,`ag_month`,`ag_day`) <br>
) </div></td>
</tr>
</table>
<br>
2nd, create a file named agenda.php with the following contents: <br>
<table cellspacing="1" cellpadding="3">
<tr>
<td><strong>Code: </strong></td>
</tr>
<tr>
<td><div class="dottedBox"><? <br>
header("Content-Type: text/javascript"); <br>
<br>
$lnk=mysql_connect("<db_hostname>", "<db_user>", "<db_password>"); <br>
mysql_select_db("<db_name>",$lnk) or die( "Unable to select database"); <br>
$query="SELECT * FROM my_agenda"; <br>
$result=mysql_query($query); <br>
<br>
$num=mysql_numrows($result); <br>
mysql_close(); <br>
<br>
for ($i=0; $i < $num; $i++) { <br>
$year=mysql_result($result,$i,"ag_year"); <br>
$month=mysql_result($result,$i,"ag_month"); <br>
$day=mysql_result($result,$i,"ag_day"); <br>
$msg=mysql_result($result,$i,"ag_message"); <br>
$action=mysql_result($result,$i,"ag_action"); <br>
$bgcolor=mysql_result($result,$i,"ag_bgcolor"); <br>
$fgcolor=mysql_result($result,$i,"ag_fgcolor"); <br>
$bgimg=mysql_result($result,$i,"ag_bgimg"); <br>
$boxit=mysql_result($result,$i,"ag_boxit"); <br>
$html=mysql_result($result,$i,"ag_html"); <br>
?> <br>
<br>
fAddEvent(<?=$year?>,<?=$month?>,<?=$day?>,"<?=$msg?>","<?=$action?>", <br>
"<?=$bgcolor?>","<?=$fgcolor?>","<?=$bgimg?>","<?=$boxit?>","<?=$html?>"); <br>
<br>
<? <br>
} <br>
?> <br>
// -- you may append additional javascript here, e.g. fHoliday() -- <br></div></td>
</tr>
</table>
Please remember to use the real db name, user id and password to replace the placeholders in above code. <br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -