1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Kernel\Handlers\XoopsUser;
13: use Xoops\Core\Kernel\XoopsObject;
14: use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
15: use Xoops\Core\Database\Connection;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28:
29: 30: 31: 32: 33: 34:
35: class PmMessage extends XoopsObject
36: {
37: 38: 39:
40: public function __construct()
41: {
42: $this->initVar('msg_id', XOBJ_DTYPE_INT, null, false);
43: $this->initVar('msg_image', XOBJ_DTYPE_OTHER, 'icon1.gif', false, 100);
44: $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, true, 255);
45: $this->initVar('from_userid', XOBJ_DTYPE_INT, null, true);
46: $this->initVar('to_userid', XOBJ_DTYPE_INT, null, true);
47: $this->initVar('msg_time', XOBJ_DTYPE_INT, time(), false);
48: $this->initVar('msg_text', XOBJ_DTYPE_TXTAREA, null, true);
49: $this->initVar('read_msg', XOBJ_DTYPE_INT, 0, false);
50: $this->initVar('from_delete', XOBJ_DTYPE_INT, 1, false);
51: $this->initVar('to_delete', XOBJ_DTYPE_INT, 0, false);
52: $this->initVar('from_save', XOBJ_DTYPE_INT, 0, false);
53: $this->initVar('to_save', XOBJ_DTYPE_INT, 0, false);
54: }
55:
56: }
57:
58: class PmMessageHandler extends XoopsPersistableObjectHandler
59: {
60: 61: 62:
63: public function __construct(Connection $db = null)
64: {
65: parent::__construct($db, 'system_privatemessage', 'PmMessage', 'msg_id', 'subject');
66: }
67:
68: 69: 70: 71: 72: 73: 74:
75: public function setRead(PmMessage $pm, $val = 1)
76: {
77: return $this->updateAll('read_msg', (int)($val), new Criteria('msg_id', $pm->getVar('msg_id')), true);
78: }
79:
80: 81: 82: 83: 84: 85: 86:
87: public function setFromDelete(PmMessage $pm, $val = 1)
88: {
89: if ($pm->getVar('to_delete') == 0) {
90: return $this->updateAll('from_delete', (int)($val), new Criteria('msg_id', $pm->getVar('msg_id')));
91: } else {
92: return parent::delete($pm);
93: }
94: }
95:
96: 97: 98: 99: 100: 101: 102:
103: public function setTodelete(PmMessage $pm, $val = 1)
104: {
105: if ($pm->getVar('from_delete') == 0 && $pm->getVar('from_userid') == 0) {
106: return $this->updateAll('to_delete', (int)($val), new Criteria('msg_id', $pm->getVar('msg_id')));
107: } else {
108: return parent::delete($pm);
109: }
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: public function setFromsave(PmMessage $pm, $val = 1)
120: {
121: return $this->updateAll('from_save', (int)($val), new Criteria('msg_id', $pm->getVar('msg_id')));
122: }
123:
124: 125: 126: 127: 128: 129: 130:
131: public function setTosave(PmMessage $pm, $val = 1)
132: {
133: return $this->updateAll('to_save', (int)($val), new Criteria('msg_id', $pm->getVar('msg_id')));
134: }
135:
136: 137: 138: 139: 140: 141:
142: public function getSavecount(XoopsUser $user = null)
143: {
144: $xoops = Xoops::getInstance();
145: if (!is_object($user)) {
146: $user = $xoops->user;
147: }
148: $crit_to = new CriteriaCompo(new Criteria('to_delete', 0));
149: $crit_to->add(new Criteria('to_save', 1));
150: $crit_to->add(new Criteria('to_userid', $user->getVar('uid')));
151: $crit_from = new CriteriaCompo(new Criteria('from_delete', 0));
152: $crit_from->add(new Criteria('from_save', 1));
153: $crit_from->add(new Criteria('from_userid', $user->getVar('uid')));
154: $criteria = new CriteriaCompo($crit_to);
155: $criteria->add($crit_from, "OR");
156: return $this->getCount($criteria);
157: }
158:
159: 160: 161: 162: 163: 164: 165:
166: public function sendEmail(PmMessage $pm, XoopsUser $user = null)
167: {
168: $xoops = Xoops::getInstance();
169: if (!is_object($user)) {
170: $user = $xoops->user;
171: }
172: $msg = sprintf(_PM_EMAIL_DESC, $user->getVar("uname"));
173: $msg .= "\n\n";
174: $msg .= XoopsLocale::formatTimestamp($pm->getVar("msg_time"));
175: $msg .= "\n";
176: $from = new XoopsUser($pm->getVar("from_userid"));
177: $to = new XoopsUser($pm->getVar("to_userid"));
178: $msg .= sprintf(_PM_EMAIL_FROM, $from->getVar("uname") . " (" . \XoopsBaseConfig::get('url') . "/userinfo.php?uid=" . $pm->getVar("from_userid") . ")");
179: $msg .= "\n";
180: $msg .= sprintf(_PM_EMAIL_TO, $to->getVar("uname") . " (" . \XoopsBaseConfig::get('url') . "/userinfo.php?uid=" . $pm->getVar("to_userid") . ")");
181: $msg .= "\n";
182: $msg .= _PM_EMAIL_MESSAGE . ":\n";
183: $msg .= "\n" . $pm->getVar("subject") . "\n";
184: $msg .= "\n" . strip_tags(str_replace(array(
185: "<p>", "</p>", "<br />", "<br />"
186: ), "\n", $pm->getVar("msg_text"))) . "\n\n";
187: $msg .= "--------------\n";
188: $msg .= $xoops->getConfig('sitename') . ": " . \XoopsBaseConfig::get('url') . "\n";
189:
190: $xoopsMailer = $xoops->getMailer();
191: $xoopsMailer->useMail();
192: $xoopsMailer->setToEmails($user->getVar("email"));
193: $xoopsMailer->setFromEmail($xoops->getConfig('adminmail'));
194: $xoopsMailer->setFromName($xoops->getConfig('sitename'));
195: $xoopsMailer->setSubject(sprintf(_PM_EMAIL_SUBJECT, $pm->getVar("subject")));
196: $xoopsMailer->setBody($msg);
197: return $xoopsMailer->send();
198: }
199:
200: 201: 202: 203: 204:
205: public function getPruneForm()
206: {
207: $form = new Xoops\Form\ThemeForm(_PM_AM_PRUNE, 'form', 'prune.php', 'post', true);
208:
209: $form->addElement(new Xoops\Form\DateTime(_PM_AM_PRUNEAFTER, 'after'));
210: $form->addElement(new Xoops\Form\DateTime(_PM_AM_PRUNEBEFORE, 'before'));
211: $form->addElement(new Xoops\Form\RadioYesNo(_PM_AM_ONLYREADMESSAGES, 'onlyread', 1));
212: $form->addElement(new Xoops\Form\RadioYesNo(_PM_AM_INCLUDESAVE, 'includesave', 0));
213: $form->addElement(new Xoops\Form\RadioYesNo(_PM_AM_NOTIFYUSERS, 'notifyusers', 0));
214: $form->addElement(new Xoops\Form\Hidden('op', 'prune'));
215: $form->addElement(new Xoops\Form\Button('', 'submit', XoopsLocale::A_SUBMIT, 'submit'));
216:
217: return $form;
218: }
219: }
220: