Passed
Pull Request — master (#14)
by Michael
04:13
created

admin/actions/newsletter.php (1 issue)

Severity
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link https://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
use XoopsModules\Oledrion;
21
22
/**
23
 * Création du contenu d'une newsletter (dans l'administration)
24
 */
25
if (!defined('OLEDRION_ADMIN')) {
26
    exit();
27
}
28
switch ($action) {
29
    // ****************************************************************************************************************
30
    case 'default': // Création de la newsletter
31
32
        // ****************************************************************************************************************
33
        xoops_cp_header();
34
        $adminObject = \Xmf\Module\Admin::getInstance();
35
        $adminObject->displayNavigation('index.php?op=newsletter');
36
        //        Oledrion\Utility::htitle(_MI_OLEDRION_ADMENU7, 4);
37
38
        // require_once OLEDRION_PATH . 'class/XoopsObjectTree.php';
39
        $sform     = new \XoopsThemeForm(_MI_OLEDRION_ADMENU7, 'frmnewsletter', $baseurl);
40
        $datesTray = new \XoopsFormElementTray(_AM_OLEDRION_NEWSLETTER_BETWEEN);
41
        $minDate   = $maxDate = 0;
42
        $productsHandler->getMinMaxPublishedDate($minDate, $maxDate);
43
        $date1 = new \XoopsFormTextDateSelect('', 'date1', 15, $minDate);
44
        $date2 = new \XoopsFormTextDateSelect(_AM_OLEDRION_EXPORT_AND, 'date2', 15, $maxDate);
45
        $datesTray->addElement($date1);
46
        $datesTray->addElement($date2);
47
        $sform->addElement($datesTray);
48
49
        $categories = $categoryHandler->getAllCategories(new Oledrion\Parameters());
50
        $mytree     = new Oledrion\XoopsObjectTree($categories, 'cat_cid', 'cat_pid');
51
52
        $htmlSelect = $mytree->makeSelectElement('cat_cid', 'cat_title', '-', 0, true, 0, _AM_OLEDRION_ALL, _AM_OLEDRION_IN_CATEGORY);
53
        $sform->addElement($htmlSelect);
54
55
        $sform->addElement(new \XoopsFormHidden('op', 'newsletter'), false);
56
        $sform->addElement(new \XoopsFormHidden('action', 'launch'), false);
57
        $sform->addElement(new \XoopsFormRadioYN(_AM_OLEDRION_REMOVE_BR, 'removebr', 1), false);
58
        $sform->addElement(new \XoopsFormRadioYN(_AM_OLEDRION_NEWSLETTER_HTML_TAGS, 'removehtml', 0), false);
59
        $sform->addElement(new \XoopsFormTextArea(_AM_OLEDRION_NEWSLETTER_HEADER, 'header', '', 4, 70), false);
60
        $sform->addElement(new \XoopsFormTextArea(_AM_OLEDRION_NEWSLETTER_FOOTER, 'footer', '', 4, 70), false);
61
        $buttonTray = new \XoopsFormElementTray('', '');
62
        $submit_btn = new \XoopsFormButton('', 'post', _SUBMIT, 'submit');
63
        $buttonTray->addElement($submit_btn);
64
        $sform->addElement($buttonTray);
65
        $sform = Oledrion\Utility::formMarkRequiredFields($sform);
66
        $sform->display();
67
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
68
69
        break;
70
    // ****************************************************************************************************************
71
    case 'launch': // Création effective de la newsletter
72
73
        // ****************************************************************************************************************
74
        xoops_cp_header();
75
        $adminObject = \Xmf\Module\Admin::getInstance();
76
        $adminObject->displayNavigation('index.php?op=newsletter');
77
        //        Oledrion\Utility::htitle(_MI_OLEDRION_ADMENU7, 4);
78
79
        $newsletterTemplate = '';
80
        /** @var Oledrion\Helper $helper */
81
        $helper = Oledrion\Helper::getInstance();
82
        $helper->loadLanguage('newsletter');
83
        echo '<br>';
84
        $removeBr   = \Xmf\Request::getInt('removebr', 0, 'POST');
85
        $removeHtml = \Xmf\Request::getInt('removehtml', 0, 'POST');
86
        $header     = \Xmf\Request::getString('header', '', 'POST');
87
        $footer     = \Xmf\Request::getString('footer', '', 'POST');
88
        $date1      = strtotime($_POST['date1']);
89
        $date2      = strtotime($_POST['date2']);
90
        $cat_id     = \Xmf\Request::getInt('cat_cid', 0, 'POST');
91
        $products   = $categories = [];
92
        $products   = $productsHandler->getProductsForNewsletter(new Oledrion\Parameters([
93
                                                                                             'startingDate' => $date1,
94
                                                                                             'endingDate'   => $date2,
95
                                                                                             'category'     => $cat_id,
96
                                                                                         ]));
97
        $newsfile   = OLEDRION_NEWSLETTER_PATH;
98
        $categories = $categoryHandler->getAllCategories(new Oledrion\Parameters([
99
                                                                                     'start'   => 0,
100
                                                                                     'limit'   => 0,
101
                                                                                     'sort'    => 'cat_title',
102
                                                                                     'order'   => 'ASC',
103
                                                                                     'idaskey' => true,
104
                                                                                 ]));
105
        $vats       = $vatHandler->getAllVats(new Oledrion\Parameters());
106
107
        $fp = fopen($newsfile, 'wb');
108
        if (!$fp) {
0 ignored issues
show
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
109
            Oledrion\Utility::redirect(_AM_OLEDRION_ERROR_7, $baseurl . '?op=newsletter', 5);
110
        }
111
        if ('' !== xoops_trim($header)) {
112
            fwrite($fp, $header);
113
        }
114
        foreach ($products as $item) {
115
            $content  = $newsletterTemplate;
116
            $tblTmp   = $tblTmp2 = [];
117
            $criteria = new \CriteriaCompo();
118
            $criteria->add(new \Criteria('pm_product_id', $item->getVar('product_id'), '='));
119
            $tblTmp = $productsmanuHandler->getObjects($criteria);
120
            foreach ($tblTmp as $productManufacturer) {
121
                $tblTmp2[] = $productManufacturer->getVar('pm_manu_id');
122
            }
123
            $manufacturers = $manufacturerHandler->getObjects(new \Criteria('manu_id', '(' . implode(',', $tblTmp2) . ')', 'IN'), true);
124
            $tblTmp        = [];
125
            foreach ($manufacturers as $manufacturer) {
126
                $tblTmp[] = $manufacturer->getVar('manu_commercialname') . ' ' . $manufacturer->getVar('manu_name');
127
            }
128
129
            $search  = [
130
                '%title%',
131
                '%category%',
132
                '%author%',
133
                '%published%',
134
                '%price%',
135
                '%money%',
136
                '%hometext%',
137
                '%fulltext%',
138
                '%discountprice%',
139
                '%link%',
140
                '%product_sku%',
141
                '%product_extraid%',
142
                '%product_width%',
143
                '%product_date%',
144
                '%product_shipping_price%',
145
                '%product_stock%',
146
                '%product_unitmeasure1%',
147
                '%product_weight%',
148
                '%product_unitmeasure2%',
149
                '%product_download_url%',
150
                '%product_length%',
151
            ];
152
            $replace = [
153
                $item->getVar('product_title'),
154
                $categories[$item->getVar('product_cid')]->getVar('cat_title'),
155
                implode(', ', $tblTmp),
156
                formatTimestamp($item->getVar('product_submitted'), 's'),
157
                Oledrion\Utility::getTTC($item->getVar('product_price'), $vats[$item->getVar('product_vat_id')]->getVar('vat_rate')),
158
                Oledrion\Utility::getModuleOption('money_short'),
159
                $item->getVar('product_summary'),
160
                $item->getVar('product_description'),
161
                Oledrion\Utility::getTTC($item->getVar('product_discount_price'), $vats[$item->getVar('product_vat_id')]->getVar('vat_rate')),
162
                $item->getLink(),
163
                $item->getVar('product_sku'),
164
                $item->getVar('product_extraid'),
165
                $item->getVar('product_width'),
166
                $item->getVar('product_date'),
167
                $item->getVar('product_shipping_price'),
168
                $item->getVar('product_stock'),
169
                $item->getVar('product_unitmeasure1'),
170
                $item->getVar('product_weight'),
171
                $item->getVar('product_unitmeasure2'),
172
                $item->getVar('product_download_url'),
173
                $item->getVar('product_length'),
174
            ];
175
            $content = str_replace($search, $replace, $content);
176
            if ($removeBr) {
177
                $content = str_replace('<br>', "\r\n", $content);
178
            }
179
            if ($removeHtml) {
180
                $content = strip_tags($content);
181
            }
182
            fwrite($fp, $content);
183
        }
184
        if ('' !== xoops_trim($footer)) {
185
            fwrite($fp, $footer);
186
        }
187
        fclose($fp);
188
        $newsfile = OLEDRION_NEWSLETTER_URL;
189
        echo "<a href='$newsfile' target='_blank'>" . _AM_OLEDRION_NEWSLETTER_READY . '</a>';
190
        require_once OLEDRION_ADMIN_PATH . 'admin_footer.php';
191
192
        break;
193
}
194