00001 <?php
00002 require_once 'Smarty.class.php';
00003 require_once 'sql.php';
00004 require_once 'error.php';
00005 require_once 'util.php';
00006 require_once 'redirect.php';
00007
00010
00019
00020 function extend_loan($coll_id, $db) {
00021
00022 # $param = array (
00023 # "tables" => "collection",
00024 # "cond" => "expiry_date <= NOW() AND id = " . $coll_id
00025 # );
00026 #
00027 # $ans = sql_query('select', $param, $db);
00028 #
00029 # # not expired? --> do nothing
00030 #
00031 # if (empty($ans)) {
00032 # return;
00033 # }
00034
00035 # set a new expiry date
00036
00037 $param = array (
00038 "tables" => "collection",
00039 "id" => $coll_id,
00040 "data" => array ("expiry_date" => get_new_expiry_date())
00041 );
00042
00043 sql_query('update', $param, $db);
00044
00045 }
00046
00057
00058
00059 function coll_set_state($smarty, $collection_id, $states, $db) {
00060
00061 # set state of documents in collection
00062 $param = array (
00063
00064 "tables" => "document,state,doc_type",
00065 "columns" => "document.*, state.name AS state_name, doc_type.name as doc_type_name",
00066 "cond" => "document.collection_id = " . $collection_id .
00067 " AND document.state_id = state.id AND document.doc_type_id = doc_type.id",
00068
00069 );
00070
00071
00072 $docs = sql_query('select', $param, $db);
00073
00074 $param = array ( "table" => "document");
00075
00076 foreach ($docs as $d) {
00077
00078 $param['id'] = $d['id'];
00079
00080 $cur_state = $d['state_name'];
00081 $cur_doctype = $d['doc_type_name'];
00082
00083 $keys = array(
00084 $cur_doctype . '-' . $cur_state,
00085 $cur_state,
00086 'DEFAULT'
00087 );
00088
00089 # print_r($keys);
00090
00091 unset($param['state']);
00092
00093 foreach ($keys as $k) {
00094 if (isset($states[$k])) {
00095 $param['state'] = $states[$k];
00096 break;
00097 }
00098 }
00099
00100 # print_r($param);
00101 if (isset($param['state'])) {
00102 sql_query('set_state', $param, $db);
00103 }
00104 }
00105
00106 }
00107
00108
00126
00127 function set_item_state($smarty, $INPUT, $db) {
00128
00129 global $_SESSION, $validation_info;
00130
00131 $default = array(
00132 );
00133
00134 ## check user input
00135
00136 # $INPUT = array_merge($default, $_GET, $_POST);
00137
00138 $INPUT = array_merge($default, $INPUT);
00139
00140 # $errors = check_input($INPUT, $validation_info);
00141
00142
00143 #if ($debug_level > 10) {
00144 # print "<hr><pre>Input: ";
00145 # print_r($INPUT);
00146 # print "</pre><hr>";
00147 #}
00148
00149 #if (!empty($errors)) {
00150 # user_error("Missing or malformed input parameter(s): " . join($errors, ", "),
00151 # E_USER_ERROR);
00152 #}
00153
00154 $item_tables = array(
00155 "DEFAULT" => "document",
00156 "collection" => "collection",
00157 "user" => "user",
00158 );
00159
00160
00161 $action = $INPUT['item'] . "-" . $INPUT['state'];
00162
00163 ## special cases
00164
00165 switch($action) {
00166
00167 case "collection-inactive":
00168
00169 $new_states = array(
00170 "DEFAULT" => "inactive",
00171 "open" => "obsolete",
00172 "active" => "obsolete",
00173 "obsolete" => "obsolete",
00174 "file-active" => "inactive",
00175 "url-active" => "inactive",
00176 );
00177
00178 coll_set_state($smarty, $INPUT['id'], $new_states, $db);
00179
00180 break;
00181
00182 case "collection-active":
00183
00184 # $new_states = array (
00185 # "file-inactive" => "active",
00186 # "url-inactive" => "active",
00187 # );
00188
00189 coll_set_state($smarty, $INPUT['id'], $new_states, $db);
00190 extend_loan($INPUT['id'], $db);
00191
00192 break;
00193
00194
00195 case "user-open":
00196
00197 # create random password and mail it to the user
00198
00199 $passwd = set_random_pw($INPUT['id'], $db);
00200
00201 $user = get_item_owner("user", $INPUT['id'], $db);
00202
00203 $email_to = $user['degree_name'] . " ";
00204 $email_to .= $user['forename'] . " ";
00205 $email_to .= $user['surname'] . " ";
00206 $email_to .= "<" . $user['email'] . ">";
00207
00208 $tpl_vars = array();
00209 $tpl_vars['user_info'] = $user;
00210 $tpl_vars['user_info']['password'] = $passwd;
00211
00212 send_email($smarty,'msg_new_account.tpl',$tpl_vars,
00213 $email_to);
00214
00215 # set state to active
00216 $INPUT['state'] = "active";
00217
00218 break;
00219 }
00220
00221 $tbl = $item_tables['DEFAULT'];
00222
00223 if (isset($item_tables[$INPUT['item']])) {
00224 $tbl = $item_tables[$INPUT['item']];
00225 }
00226
00227 $param = array ( "table" => $tbl,
00228 "state" => $INPUT['state'],
00229 "id" => $INPUT['id'],
00230 );
00231
00232 sql_query('set_state', $param, $db);
00233
00234 if ($action == "collection-active") {
00235 # show message for user
00236
00237 $tpl_vars = array (
00238 "expiry_date" => get_new_expiry_date() ,
00239 "id" => $INPUT['id']
00240 );
00241
00242 do_template($smarty,'extend_loan.tpl', $tpl_vars);
00243 exit(0);
00244 }
00245
00246 }
00247
00248
00297
00298 function expire($smarty, $db) {
00299
00300 $param = array (
00301 "tables" => "collection as c,state as s" ,
00302 "columns" => "c.id" ,
00303 cond => "c.state_id = s.id AND
00304 s.name = 'active' AND
00305 c.expiry_date <= NOW()",
00306 );
00307
00308 $colls = sql_query('select', $param, $db);
00309
00310 $input = array ("item" => "collection", "state" => "obsolete" );
00311
00312 foreach ($colls as $c) {
00313 $input['id'] = $c['id'];
00314 set_item_state($smarty, $input, $db);
00315 }
00316
00317
00318 $param = array (
00319 "tables" => "collection AS c,state AS s" ,
00320 "columns" => "c.id AS id",
00321 cond => "(c.state_id = s.id) AND
00322 (s.name = 'obsolete') AND
00323 DATE_ADD(c.expiry_date, INTERVAL 14 DAY) <= NOW()"
00324 );
00325
00326 $colls = sql_query('select', $param, $db);
00327
00328 $input = array ("item" => "collection", "state" => "inactive" );
00329
00330 foreach ($colls as $c) {
00331 $input['id'] = $c['id'];
00332 set_item_state($smarty, $input, $db);
00333 }
00334 }
00335
00336 ?>