diff options
| author | bitweaver.org <bitweaver@users.sourceforge.net> | 2005-12-09 06:59:54 +0000 |
|---|---|---|
| committer | bitweaver.org <bitweaver@users.sourceforge.net> | 2005-12-09 06:59:54 +0000 |
| commit | e686c2762faae2d487381aede0ddcc9662ebbd52 (patch) | |
| tree | 8b30cde0c76c96e8f74c462244930979f1559548 /nl_lib.php | |
| download | newsletters-e686c2762faae2d487381aede0ddcc9662ebbd52.tar.gz newsletters-e686c2762faae2d487381aede0ddcc9662ebbd52.tar.bz2 newsletters-e686c2762faae2d487381aede0ddcc9662ebbd52.zip | |
IMPORT from TikiPro HEAD _p_tp_newsletters
Diffstat (limited to 'nl_lib.php')
| -rw-r--r-- | nl_lib.php | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/nl_lib.php b/nl_lib.php new file mode 100644 index 0000000..e27b635 --- /dev/null +++ b/nl_lib.php @@ -0,0 +1,280 @@ +<?php + +class NlLib extends TikiLib { + function NlLib() { + parent::TikiLib(); + } + + function replace_newsletter($nl_id, $name, $description, $allow_user_sub, $allow_any_sub, $unsub_msg, $validate_addr) { + if ($nl_id) { + $query = "update `".TIKI_DB_PREFIX."tiki_newsletters` set `name`=?, `description`=?, `allow_user_sub`=?, `allow_any_sub`=?, `unsub_msg`=?, `validate_addr`=? where `nl_id`=?"; + $result = $this->query($query, array($name,$description,$allow_user_sub,$allow_any_sub,$unsub_msg,$validate_addr,(int)$nl_id)); + } else { + $now = date("U"); + $query = "insert into `".TIKI_DB_PREFIX."tiki_newsletters`(`name`,`description`,`allow_user_sub`,`allow_any_sub`,`unsub_msg`,`validate_addr`,`last_sent`,`editions`,`users`,`created`) "; + $query.= " values(?,?,?,?,?,?,?,?,?,?)"; + $result = $this->query($query, array($name,$description,$allow_user_sub,$allow_any_sub,$unsub_msg,$validate_addr,(int)$now,0,0,(int)$now)); + $queryid = "select max(`nl_id`) from `".TIKI_DB_PREFIX."tiki_newsletters` where `created`=?"; + $nl_id = $this->getOne($queryid, array((int)$now)); + } + return $nl_id; + } + + function replace_edition($nl_id, $subject, $data, $users) { + $now = date("U"); + $query = "insert into `".TIKI_DB_PREFIX."tiki_sent_newsletters`(`nl_id`,`subject`,`data`,`sent`,`users`) values(?,?,?,?,?)"; + $result = $this->query($query,array((int)$nl_id,$subject,$data,(int)$now,$users)); + } + + function get_subscribers($nl_id) { + $query = "select email from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `valid`=? and `nl_id`=?"; + $result = $this->query($query, array('y',(int)$nl_id)); + $ret = array(); + while ($res = $result->fetchRow()) { + $ret[] = $res["email"]; + } + return $ret; + } + + function remove_newsletter_subscription($nl_id, $email) { + $valid = $this->getOne("select `valid` from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=? and `email`=?", array((int)$nl_id,$email)); + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=? and `email`=?"; + $result = $this->query($query, array((int)$nl_id,$email)); + $this->update_users($nl_id); + } + + function newsletter_subscribe($nl_id, $email) { + global $smarty; + global $user; + global $sender_email; + $info = $this->get_newsletter($nl_id); + $smarty->assign('info', $info); + $code = md5($this->genPass()); + $now = date("U"); + if ($info["validate_addr"] == 'y') { + // Generate a code and store it and send an email with the + // URL to confirm the subscription put valid as 'n' + $foo = parse_url($_SERVER["REQUEST_URI"]); + $foopath = preg_replace('/tiki-admin_newsletter_subscriptions.php/', 'tiki-newsletters.php', $foo["path"]); + $url_subscribe = httpPrefix(). $foopath; + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=? and `email`=?"; + $result = $this->query($query,array((int)$nl_id,$email)); + $query = "insert into `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions`(`nl_id`,`email`,`code`,`valid`,`subscribed`) values(?,?,?,?,?)"; + $result = $this->query($query,array((int)$nl_id,$email,$code,'n',(int)$now)); + // Now send an email to the address with the confirmation instructions + $smarty->assign('mail_date', date("U")); + $smarty->assign('mail_user', $user); + $smarty->assign('code', $code); + $smarty->assign('url_subscribe', $url_subscribe); + $smarty->assign('server_name', $_SERVER["SERVER_NAME"]); + $mail_data = $smarty->fetch('tikipackage:newsletters/confirm_newsletter_subscription.tpl'); + @mail($email, tra('Newsletter subscription information at '). $_SERVER["SERVER_NAME"], $mail_data, + "From: $sender_email\r\nContent-type: text/plain;charset=utf-8\r\n"); + } else { + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=? and `email`=?"; + $result = $this->query($query,array((int)$nl_id,$email)); + $query = "insert into `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions`(`nl_id`,`email`,`code`,`valid`,`subscribed`) values(?,?,?,?,?)"; + $result = $this->query($query,array((int)$nl_id,$email,$code,'y',(int)$now)); + } + $this->update_users($nl_id); + } + + function confirm_subscription($code) { + global $smarty; + global $user; + global $sender_email; + $foo = parse_url($_SERVER["REQUEST_URI"]); + $url_subscribe = httpPrefix(). $foo["path"]; + $query = "select * from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `code`=?"; + $result = $this->query($query,array($code)); + + if (!$result->numRows()) return false; + + $res = $result->fetchRow(); + $info = $this->get_newsletter($res["nl_id"]); + $smarty->assign('info', $info); + $query = "update `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` set `valid`=? where `code`=?"; + $result = $this->query($query,array('y',$code)); + // Now send a welcome email + $smarty->assign('mail_date', date("U")); + $smarty->assign('mail_user', $user); + $smarty->assign('code', $res["code"]); + $smarty->assign('url_subscribe', $url_subscribe); + $mail_data = $smarty->fetch('tikipackage:newsletters/newsletter_welcome.tpl'); + @mail($res["email"], tra('Welcome to '). $info["name"] . tra(' at '). $_SERVER["SERVER_NAME"], $mail_data, + "From: $sender_email\r\nContent-type: text/plain;charset=utf-8\r\n"); + return $this->get_newsletter($res["nl_id"]); + } + + function unsubscribe($code) { + global $smarty; + global $user; + global $sender_email; + $foo = parse_url($_SERVER["REQUEST_URI"]); + $url_subscribe = httpPrefix(). $foo["path"]; + $query = "select * from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `code`=?"; + $result = $this->query($query,array($code)); + + if (!$result->numRows()) return false; + + $res = $result->fetchRow(); + $info = $this->get_newsletter($res["nl_id"]); + $smarty->assign('info', $info); + $smarty->assign('code', $res["code"]); + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `code`=?"; + $result = $this->query($query,array($code)); + // Now send a bye bye email + $smarty->assign('mail_date', date("U")); + $smarty->assign('mail_user', $user); + $smarty->assign('url_subscribe', $url_subscribe); + $mail_data = $smarty->fetch('tikipackage:newsletters/newsletter_byebye.tpl'); + @mail($res["email"], tra('Bye bye from '). $info["name"] . tra(' at '). $_SERVER["SERVER_NAME"], $mail_data, + "From: $sender_email\r\nContent-type: text/plain;charset=utf-8\r\n"); + $this->update_users($res["nl_id"]); + return $this->get_newsletter($res["nl_id"]); + } + + function add_all_users($nl_id) { + $query = "select `email` from `".TIKI_DB_PREFIX."users_users`"; + $result = $this->query($query,array()); + while ($res = $result->fetchRow()) { + $email = $res["email"]; + if (!empty($email)) { + $this->newsletter_subscribe($nl_id, $email); + } + } + } + + function get_newsletter($nl_id) { + $query = "select * from `".TIKI_DB_PREFIX."tiki_newsletters` where `nl_id`=?"; + $result = $this->query($query,array((int)$nl_id)); + if (!$result->numRows()) return false; + $res = $result->fetchRow(); + return $res; + } + + function get_edition($edition_id) { + $query = "select * from `".TIKI_DB_PREFIX."tiki_sent_newsletters` where `edition_id`=?"; + $result = $this->query($query,array((int)$edition_id)); + if (!$result->numRows()) return false; + $res = $result->fetchRow(); + return $res; + } + + function update_users($nl_id) { + $users = $this->getOne("select count(*) from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=?",array((int)$nl_id)); + $query = "update `".TIKI_DB_PREFIX."tiki_newsletters` set `users`=? where `nl_id`=?"; + $result = $this->query($query,array($users,(int)$nl_id)); + } + + function list_newsletters($offset, $maxRecords, $sort_mode, $find) { + $bindvars = array(); + if ($find) { + $findesc = '%' . $find . '%'; + $mid = " where (`name` like ? or `description` like ?)"; + $bindvars[] = $findesc; + $bindvars[] = $findesc; + } else { + $mid = " "; + } + + $query = "select * from `".TIKI_DB_PREFIX."tiki_newsletters` $mid order by ".$this->convert_sortmode("$sort_mode"); + $query_cant = "select count(*) from `".TIKI_DB_PREFIX."tiki_newsletters` $mid"; + $result = $this->query($query,$bindvars,$maxRecords,$offset); + $cant = $this->getOne($query_cant,$bindvars); + $ret = array(); + + while ($res = $result->fetchRow()) { + $res["confirmed"] = $this->getOne("select count(*) from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `valid`=? and `nl_id`=?",array('y',(int)$res["nl_id"])); + $ret[] = $res; + } + $retval = array(); + $retval["data"] = $ret; + $retval["cant"] = $cant; + return $retval; + } + + function list_editions($offset, $maxRecords, $sort_mode, $find) { + $bindvars = array(); + if ($find) { + $findesc = '%' . $find . '%'; + $mid = " and (`subject` like ? or `data` like ?)"; + $bindvars[] = $findesc; + $bindvars[] = $findesc; + } else { + $mid = " "; + } + + $query = "select tsn.`edition_id`,tn.`nl_id`,`subject`,`data`,tsn.`users`,`sent`,`name` from `".TIKI_DB_PREFIX."tiki_newsletters` tn, `".TIKI_DB_PREFIX."tiki_sent_newsletters` tsn "; + $query.= " where tn.`nl_id`=tsn.`nl_id` $mid order by ".$this->convert_sortmode("$sort_mode"); + $query_cant = "select count(*) from `".TIKI_DB_PREFIX."tiki_newsletters` tn, `".TIKI_DB_PREFIX."tiki_sent_newsletters` tsn where tn.`nl_id`=tsn.`nl_id` $mid"; + $result = $this->query($query,$bindvars,$maxRecords,$offset); + $cant = $this->getOne($query_cant,$bindvars); + $ret = array(); + + while ($res = $result->fetchRow()) { + $ret[] = $res; + } + + $retval = array(); + $retval["data"] = $ret; + $retval["cant"] = $cant; + return $retval; + } + + function list_newsletter_subscriptions($nl_id, $offset, $maxRecords, $sort_mode, $find) { + $bindvars = array((int)$nl_id); + if ($find) { + $findesc = '%' . $find . '%'; + $mid = " where `nl_id`=? and (`name` like ? or `description` like ?)"; + $bindvars[] = $findesc; + $bindvars[] = $findesc; + } else { + $mid = " where `nl_id`=? "; + } + + $query = "select * from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` $mid order by ".$this->convert_sortmode("$sort_mode"); + $query_cant = "select count(*) from tiki_newsletter_subscriptions $mid"; + $result = $this->query($query,$bindvars,$maxRecords,$offset); + $cant = $this->getOne($query_cant,$bindvars); + $ret = array(); + + while ($res = $result->fetchRow()) { + $ret[] = $res; + } + $retval = array(); + $retval["data"] = $ret; + $retval["cant"] = $cant; + return $retval; + } + + function get_unsub_msg($nl_id, $email) { + $foo = parse_url($_SERVER["REQUEST_URI"]); + + $foo = str_replace('send_newsletters', 'newsletters', $foo); + $url_subscribe = httpPrefix(). $foo["path"]; + $code = $this->getOne("select `code` from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=? and `email`=?",array((int)$nl_id,$email)); + $url_unsub = $url_subscribe . '?unsubscribe=' . $code; + $msg = '<br/><br/>' . tra( 'You can unsubscribe from this newsletter following this link'). ": <a href='$url_unsub'>$url_unsub</a>"; + return $msg; + } + + function remove_newsletter($nl_id) { + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletters` where `nl_id`=?"; + $result = $this->query($query,array((int)$nl_id)); + $query = "delete from `".TIKI_DB_PREFIX."tiki_newsletter_subscriptions` where `nl_id`=?"; + $result = $this->query($query,array((int)$nl_id)); + $this->remove_object('newsletter', $nl_id); + return true; + } + + function remove_edition($edition_id) { + $query = "delete from `".TIKI_DB_PREFIX."tiki_sent_newsletters` where `edition_id`=$edition_id"; + $result = $this->query($query,array((int)$edition_id)); + } + +} + +$nllib = new NlLib(); + +?> |
