XoopsModules25x /
xnewsletter
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace XoopsModules\Xnewsletter; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * **************************************************************************** |
||
| 7 | * - A Project by Developers TEAM For Xoops - ( https://xoops.org ) |
||
| 8 | * **************************************************************************** |
||
| 9 | * XNEWSLETTER - MODULE FOR XOOPS |
||
| 10 | * Copyright (c) 2007 - 2012 |
||
| 11 | * Goffy ( wedega.com ) |
||
| 12 | * |
||
| 13 | * You may not change or alter any portion of this comment or credits |
||
| 14 | * of supporting developers from this source code or any supporting |
||
| 15 | * source code which is considered copyrighted (c) material of the |
||
| 16 | * original comment or credit authors. |
||
| 17 | * |
||
| 18 | * This program is distributed in the hope that it will be useful, |
||
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 21 | * GNU General Public License for more details. |
||
| 22 | * --------------------------------------------------------------------------- |
||
| 23 | * @copyright Goffy ( wedega.com ) |
||
| 24 | * @license GPL 2.0 |
||
| 25 | * @package xnewsletter |
||
| 26 | * @author Goffy ( [email protected] ) |
||
| 27 | * |
||
| 28 | * **************************************************************************** |
||
| 29 | */ |
||
| 30 | |||
| 31 | //use XoopsModules\Xnewsletter; |
||
| 32 | |||
| 33 | require_once dirname(__DIR__) . '/include/common.php'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Class Subscr |
||
| 37 | */ |
||
| 38 | class Subscr extends \XoopsObject |
||
| 39 | { |
||
| 40 | public $helper = null; |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | //Constructor |
||
| 44 | |||
| 45 | public function __construct() |
||
| 46 | { |
||
| 47 | $this->helper = Helper::getInstance(); |
||
| 48 | $this->db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 49 | $this->initVar('subscr_id', XOBJ_DTYPE_INT, null, false); |
||
| 50 | $this->initVar('subscr_email', XOBJ_DTYPE_TXTBOX, '', false, 100); |
||
| 51 | $this->initVar('subscr_firstname', XOBJ_DTYPE_TXTBOX, '', true, 100); |
||
| 52 | $this->initVar('subscr_lastname', XOBJ_DTYPE_TXTBOX, '', false, 100); |
||
| 53 | $this->initVar('subscr_uid', XOBJ_DTYPE_INT, null, false); |
||
| 54 | $this->initVar('subscr_sex', XOBJ_DTYPE_TXTBOX, '', false, 100); |
||
| 55 | $this->initVar('subscr_submitter', XOBJ_DTYPE_INT, null, false); |
||
| 56 | $this->initVar('subscr_created', XOBJ_DTYPE_INT, time(), false); |
||
| 57 | $this->initVar('subscr_actkey', XOBJ_DTYPE_TXTBOX, '', false, 255); |
||
| 58 | $this->initVar('subscr_ip', XOBJ_DTYPE_TXTBOX, xoops_getenv('REMOTE_ADDR'), false, 32); |
||
| 59 | $this->initVar('subscr_activated', XOBJ_DTYPE_INT, 0, false); // IN PROGRESS: should be false or timestamp |
||
| 60 | $this->initVar('subscr_actoptions', XOBJ_DTYPE_ARRAY, [], false); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param bool $action |
||
| 65 | * |
||
| 66 | * @return \XoopsThemeForm |
||
| 67 | */ |
||
| 68 | public function getSearchForm($action = false) |
||
| 69 | { |
||
| 70 | global $xoopsDB; |
||
| 71 | |||
| 72 | if (false === $action) { |
||
| 73 | $action = $_SERVER['REQUEST_URI']; |
||
| 74 | } |
||
| 75 | |||
| 76 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 77 | $form = new \XoopsThemeForm(_MA_XNEWSLETTER_SUBSCRIPTION_SEARCH, 'formsearch', $action, 'post', true); |
||
| 78 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 79 | |||
| 80 | // subscr_email |
||
| 81 | $email_field = new \XoopsFormText(_MA_XNEWSLETTER_SUBSCRIPTION_SEARCH_EMAIL, 'subscr_email', 50, 100, $this->getVar('subscr_email')); |
||
| 82 | if ('' != $this->getVar('subscr_email')) { |
||
| 83 | //$email_field->setExtra('disabled="disabled"'); |
||
| 84 | } |
||
| 85 | $form->addElement($email_field, true); |
||
| 86 | |||
| 87 | // captcha |
||
| 88 | xoops_load('xoopscaptcha'); |
||
| 89 | $form->addElement(new \XoopsFormCaptcha('', 'xoopscaptcha', true)); |
||
| 90 | |||
| 91 | // op |
||
| 92 | $form->addElement(new \XoopsFormHidden('op', 'list_subscriptions')); |
||
| 93 | // buttons |
||
| 94 | $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); |
||
| 95 | |||
| 96 | |||
| 97 | return $form; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param bool $action |
||
| 102 | * |
||
| 103 | * @return \XoopsThemeForm |
||
| 104 | */ |
||
| 105 | public function getForm($action = false) |
||
| 106 | { |
||
| 107 | global $xoopsDB, $xoopsUser; |
||
| 108 | |||
| 109 | if (false === $action) { |
||
| 110 | $action = $_SERVER['REQUEST_URI']; |
||
| 111 | } |
||
| 112 | |||
| 113 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 114 | $title = $this->isNew() ? sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_ADD) : sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_EDIT); |
||
| 115 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 116 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 117 | |||
| 118 | $form->addElement(new \XoopsFormLabel("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_INFO_PERS . '</span>', '')); |
||
| 119 | $subscr_id = $this->isNew() ? 0 : $this->getVar('subscr_id'); |
||
| 120 | |||
| 121 | // subscr_email |
||
| 122 | if ($subscr_id > 0 || '' != $this->getVar('subscr_email')) { |
||
| 123 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_EMAIL, $this->getVar('subscr_email'))); |
||
| 124 | $form->addElement(new \XoopsFormHidden('subscr_email', $this->getVar('subscr_email'))); |
||
| 125 | } else { |
||
| 126 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 50, 255, $this->getVar('subscr_email')), true); |
||
| 127 | } |
||
| 128 | |||
| 129 | // subscr_sex |
||
| 130 | if (1 == $this->helper->getConfig('xn_use_salutation')) { |
||
| 131 | $select_subscr_sex = new \XoopsFormSelect(_AM_XNEWSLETTER_SUBSCR_SEX, 'subscr_sex', $this->getVar('subscr_sex')); |
||
| 132 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_EMPTY, _AM_XNEWSLETTER_SUBSCR_SEX_EMPTY); |
||
| 133 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FEMALE, _AM_XNEWSLETTER_SUBSCR_SEX_FEMALE); |
||
| 134 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_MALE, _AM_XNEWSLETTER_SUBSCR_SEX_MALE); |
||
| 135 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_COMP, _AM_XNEWSLETTER_SUBSCR_SEX_COMP); |
||
| 136 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FAMILY, _AM_XNEWSLETTER_SUBSCR_SEX_FAMILY); |
||
| 137 | $form->addElement($select_subscr_sex); |
||
| 138 | } |
||
| 139 | |||
| 140 | // subscr_firstname |
||
| 141 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_FIRSTNAME, 'subscr_firstname', 50, 255, $this->getVar('subscr_firstname')), true); |
||
| 142 | |||
| 143 | // subscr_lastname |
||
| 144 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_LASTNAME, 'subscr_lastname', 50, 255, $this->getVar('subscr_lastname')), false); |
||
| 145 | |||
| 146 | $form->addElement(new \XoopsFormLabel('<br><br>', '')); |
||
| 147 | |||
| 148 | // get newsletters available for current user |
||
| 149 | $opt_cat = []; |
||
| 150 | $opt_tray = new \XoopsFormElementTray("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL . '</span>', '<br>'); |
||
| 151 | $opt_tray->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC); |
||
| 152 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 153 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
| 154 | $uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0; |
||
| 155 | $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS]; |
||
| 156 | |||
| 157 | // cats[], existing_catsubcr_id_{$cat_id}, existing_catsubscr_quited_{$cat_id} |
||
| 158 | $catCriteria = new \CriteriaCompo(); |
||
| 159 | $catCriteria->setSort('cat_id'); |
||
| 160 | $catCriteria->setOrder('ASC'); |
||
| 161 | $catObjs = $this->helper->getHandler('Cat')->getAll($catCriteria); |
||
| 162 | // $cat_checkbox = new \XoopsFormCheckBox(_MA_XNEWSLETTER_SUBSCRIPTION_SELECT_CATS, 'cats', null, '<br>'); |
||
| 163 | // $cat_checkbox->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC); |
||
| 164 | // |
||
| 165 | // |
||
| 166 | // $cat_tray = new \XoopsFormElementTray(_MA_XNEWSLETTER_SUBSCRIPTION_SELECT_CATS, '<br>'); |
||
| 167 | $values = []; |
||
| 168 | foreach ($catObjs as $cat_id => $catObj) { |
||
| 169 | // if anonymous user or Xoops user can read cat... |
||
| 170 | if ($grouppermHandler->checkRight('newsletter_read_cat', $cat_id, XOOPS_GROUP_ANONYMOUS, $this->helper->getModule()->mid()) |
||
| 171 | || $grouppermHandler->checkRight('newsletter_read_cat', $cat_id, $groups, $this->helper->getModule()->mid())) { |
||
| 172 | // get existing catsubscr |
||
| 173 | $catsubscrCriteria = new \CriteriaCompo(); |
||
| 174 | $catsubscrCriteria->add(new \Criteria('catsubscr_catid', $cat_id)); |
||
| 175 | $catsubscrCriteria->add(new \Criteria('catsubscr_subscrid', $subscr_id)); |
||
| 176 | $catsubscrCriteria->setLimit(1); |
||
| 177 | $catsubscrObjs = $this->helper->getHandler('Catsubscr')->getObjects($catsubscrCriteria); |
||
| 178 | if (isset($catsubscrObjs[0])) { |
||
| 179 | $values[] = $cat_id; |
||
| 180 | $catsubscr_quited = $catsubscrObjs[0]->getVar('catsubscr_quited'); |
||
| 181 | $catsubscr_id = $catsubscrObjs[0]->getVar('catsubscr_id'); |
||
| 182 | } else { |
||
| 183 | $catsubscr_quited = 0; |
||
| 184 | $catsubscr_id = 0; |
||
| 185 | } |
||
| 186 | |||
| 187 | $cat_checkbox[$cat_id] = new \XoopsFormCheckBox('', 'cats[]', null, ''); |
||
|
0 ignored issues
–
show
|
|||
| 188 | $name = $catObj->getVar('cat_name'); |
||
| 189 | if ('' !== $catObj->getVar('cat_info')) { |
||
| 190 | $name .= '<br><span class="xnewsletter-cat_info">' . $catObj->getVar('cat_info', 's') . '</span>'; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (0 == $catsubscr_quited) { |
||
| 194 | // NOP |
||
| 195 | } else { |
||
| 196 | $name .= '<span>'; |
||
| 197 | $name .= str_replace('%q', formatTimestamp($catsubscr_quited, $this->helper->getConfig('dateformat')), _MA_XNEWSLETTER_SUBSCRIPTION_QUITED_DETAIL); |
||
| 198 | $name .= '</span>'; |
||
| 199 | } |
||
| 200 | // $name .= "<div style='clear:both'></div>"; |
||
| 201 | $cat_checkbox[$cat_id]->addOption($cat_id, $name); |
||
| 202 | $form->addElement(new \XoopsFormHidden("existing_catsubcr_id_{$cat_id}", $catsubscr_id)); |
||
| 203 | $form->addElement(new \XoopsFormHidden("existing_catsubscr_quited_{$cat_id}", $catsubscr_quited)); |
||
| 204 | $cat_checkbox[$cat_id]->setValue($values); |
||
| 205 | $opt_tray->addElement($cat_checkbox[$cat_id]); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | $form->addElement($opt_tray); |
||
| 209 | |||
| 210 | // op |
||
| 211 | $form->addElement(new \XoopsFormHidden('op', 'save_subscription')); |
||
| 212 | // button |
||
| 213 | $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); |
||
| 214 | |||
| 215 | return $form; |
||
| 216 | } |
||
| 217 | |||
| 218 | //********************************************************************************************** |
||
| 219 | // form for admin aerea ******************************************************************* |
||
| 220 | //********************************************************************************************** |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param bool $action |
||
| 224 | * |
||
| 225 | * @return \XoopsThemeForm |
||
| 226 | */ |
||
| 227 | public function getFormAdmin($action = false) |
||
| 228 | { |
||
| 229 | global $xoopsDB; |
||
| 230 | |||
| 231 | if (false === $action) { |
||
| 232 | $action = $_SERVER['REQUEST_URI']; |
||
| 233 | } |
||
| 234 | |||
| 235 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_SUBSCR_ADD) : sprintf(_AM_XNEWSLETTER_SUBSCR_EDIT); |
||
| 236 | |||
| 237 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 238 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 239 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 240 | |||
| 241 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 50, 255, $this->getVar('subscr_email')), true); |
||
| 242 | $select_subscr_sex = new \XoopsFormSelect(_AM_XNEWSLETTER_SUBSCR_SEX, 'subscr_sex', $this->getVar('subscr_sex')); |
||
| 243 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_EMPTY, _AM_XNEWSLETTER_SUBSCR_SEX_EMPTY); |
||
| 244 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FEMALE, _AM_XNEWSLETTER_SUBSCR_SEX_FEMALE); |
||
| 245 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_MALE, _AM_XNEWSLETTER_SUBSCR_SEX_MALE); |
||
| 246 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_COMP, _AM_XNEWSLETTER_SUBSCR_SEX_COMP); |
||
| 247 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FAMILY, _AM_XNEWSLETTER_SUBSCR_SEX_FAMILY); |
||
| 248 | $form->addElement($select_subscr_sex); |
||
| 249 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_FIRSTNAME, 'subscr_firstname', 50, 255, $this->getVar('subscr_firstname')), true); |
||
| 250 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_LASTNAME, 'subscr_lastname', 50, 255, $this->getVar('subscr_lastname')), false); |
||
| 251 | |||
| 252 | $form->addElement(new \XoopsFormSelectUser(_AM_XNEWSLETTER_SUBSCR_UID, 'subscr_uid', true, $this->getVar('subscr_uid'), 1, false), false); |
||
| 253 | |||
| 254 | $form->addElement(new \XoopsFormHidden('subscr_submitter', $GLOBALS['xoopsUser']->uid())); |
||
| 255 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname())); |
||
| 256 | //$form->addElement(new \XoopsFormSelectUser(_AM_XNEWSLETTER_SUBMITTER, 'subscr_submitter', false, $this->getVar('subscr_submitter'), 1, false), true); |
||
| 257 | |||
| 258 | if ($this->getVar('subscr_id') > 0) { |
||
| 259 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($this->getVar('subscr_created'), $this->helper->getConfig('dateformat')) . ' [' . $this->getVar('subscr_ip') . ']')); |
||
| 260 | $form->addElement(new \XoopsFormHidden('subscr_created', $this->getVar('subscr_created'))); |
||
| 261 | $form->addElement(new \XoopsFormHidden('subscr_ip', $this->getVar('subscr_ip'))); |
||
| 262 | } else { |
||
| 263 | $time = time(); |
||
| 264 | $ip = xoops_getenv('REMOTE_ADDR'); |
||
| 265 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's') . " [{$ip}]")); |
||
| 266 | $form->addElement(new \XoopsFormHidden('subscr_created', $time)); |
||
| 267 | $form->addElement(new \XoopsFormHidden('subscr_ip', $ip)); |
||
| 268 | } |
||
| 269 | $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_SUBSCR_ACTIVATED, 'subscr_activated', $this->getVar('subscr_activated'))); |
||
| 270 | $form->addElement(new \XoopsFormHidden('subscr_actkey', '')); |
||
| 271 | $form->addElement(new \XoopsFormHidden('op', 'save_subscr')); |
||
| 272 | $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); |
||
| 273 | |||
| 274 | return $form; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get Values |
||
| 279 | * @param null $keys |
||
| 280 | * @param string|null $format |
||
| 281 | * @param int|null $maxDepth |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | public function getValuesSubscr($keys = null, $format = null, $maxDepth = null) |
||
| 285 | { |
||
| 286 | $ret = $this->getValues($keys, $format, $maxDepth); |
||
| 287 | $ret['id'] = $this->getVar('subscr_id'); |
||
| 288 | $ret['email'] = $this->getVar('subscr_email'); |
||
| 289 | $ret['firstname'] = $this->getVar('subscr_firstname'); |
||
| 290 | $ret['lastname'] = $this->getVar('subscr_lastname'); |
||
| 291 | $ret['uid'] = $this->getVar('subscr_uid'); |
||
| 292 | $ret['sex'] = $this->getVar('subscr_sex'); |
||
| 293 | $ret['actkey'] = $this->getVar('subscr_actkey'); |
||
| 294 | $ret['ip'] = $this->getVar('subscr_ip'); |
||
| 295 | $ret['activated'] = $this->getVar('subscr_activated'); |
||
| 296 | $ret['actoptions'] = $this->getVar('subscr_actoptions'); |
||
| 297 | $ret['created'] = formatTimestamp($this->getVar('subscr_created'), 's'); |
||
| 298 | $ret['submitter'] = \XoopsUser::getUnameFromId($this->getVar('subscr_submitter')); |
||
| 299 | return $ret; |
||
| 300 | } |
||
| 301 | } |
||
| 302 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.