Completed
Push — master ( 15a86c...ff0242 )
by Michael
03:23
created

Barticle::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 23
rs 9.0856
1
<?php
2
//
3
// ------------------------------------------------------------------------ //
4
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: http://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
/**
28
 * @package   module::article
29
 * @copyright copyright &copy; 2005 XoopsForge.com
30
 */
31
32
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
include_once dirname(__DIR__) . '/include/vars.php';
34
mod_loadFunctions('', $GLOBALS['moddirname']);
35
36
/**
37
 * Article
38
 *
39
 * @author    D.J. (phppp)
40
 * @copyright copyright &copy; 2005 XoopsForge.com
41
 * @package   module::article
42
 *
43
 * {@link XoopsObject}
44
 **/
45
if (!class_exists('Barticle')):
46
47
    /**
48
     * Class Barticle
49
     */
50
    class Barticle extends XoopsObject
51
    {
52
        /**
53
         * Constructor
54
         *
55
         * @param int $id ID of the article
56
         */
57
        public function __construct($id = null) {
58
            //            $this->ArtObject();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
            $this->table = planet_DB_prefix('article');
60
            $this->initVar('art_id', XOBJ_DTYPE_INT, null, false);
61
            $this->initVar('blog_id', XOBJ_DTYPE_INT, 0, true);
62
63
            $this->initVar('art_title', XOBJ_DTYPE_TXTBOX, '');
64
            $this->initVar('art_content', XOBJ_DTYPE_TXTAREA, '');
65
            $this->initVar('art_link', XOBJ_DTYPE_TXTBOX, '');
66
            $this->initVar('art_time', XOBJ_DTYPE_INT, 0);
67
            $this->initVar('art_author', XOBJ_DTYPE_TXTBOX, '');
68
69
            $this->initVar('art_views', XOBJ_DTYPE_INT, 0);
70
            $this->initVar('art_rating', XOBJ_DTYPE_INT, 0);
71
            $this->initVar('art_rates', XOBJ_DTYPE_INT, 0);
72
            $this->initVar('art_comments', XOBJ_DTYPE_INT, 0);
73
74
            $this->initVar('dohtml', XOBJ_DTYPE_INT, 1);
75
            $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1);
76
            $this->initVar('doxcode', XOBJ_DTYPE_INT, 1);
77
            $this->initVar('doimage', XOBJ_DTYPE_INT, 1);
78
            $this->initVar('dobr', XOBJ_DTYPE_INT, 1);
79
        }
80
81
        /**
82
         * get title of the article
83
         *
84
         * @return string
85
         */
86
        public function getTitle() {
87
            $title = $this->getVar('art_title');
88
89
            return $title;
90
        }
91
92
        /**
93
         * get formatted publish time of the article
94
         *
95
         * {@link Config}
96
         *
97
         * @param  string $format format of time
98
         * @return string
99
         */
100
        public function getTime($format = 'c') {
101
            $time = $this->getVar('art_time');
102
            if (empty($time)) {
103
                $time = time();
104
            }
105
            $time = planet_formatTimestamp($time, $format);
106
107
            return $time;
108
        }
109
110
        /**
111
         * get summary of the article
112
         *
113
         * @param int $length
114
         * @return string $summary
115
         */
116
        public function &getSummary($length = 0) {
117
            $content = $this->getVar('art_content');
118
            $summary =& planet_html2text($content);
119
            if (empty($length)) {
120
                $length = $GLOBALS['xoopsModuleConfig']['display_summary'];
121
            }
122
            if (!empty($length)) {
123
                $summary = xoops_substr($summary, 0, $length);
124
            }
125
126
            return $summary;
127
        }
128
129
        /**
130
         * get rating average of the article
131
         *
132
         * @param  int $decimals decimal length
133
         * @return numeric
134
         */
135 View Code Duplication
        public function getRatingAverage($decimals = 1) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
            $ave = 3;
137
            if ($this->getVar('art_rates')) {
138
                $ave = number_format($this->getVar('art_rating') / $this->getVar('art_rates'), $decimals);
139
            }
140
141
            return $ave;
142
        }
143
144
        /**
145
         * @return numeric
146
         */
147
        public function getStar() {
148
            return $this->getRatingAverage(0);
149
        }
150
    }
151
endif;
152
153
/**
154
 * Article object handler class.
155
 * @package   module::article
156
 *
157
 * @author    D.J. (phppp)
158
 * @copyright copyright &copy; 2000 XOOPS Project
159
 *
160
 * {@link XoopsPersistableObjectHandler}
161
 *
162
 * @param CLASS_PREFIX variable prefix for the class name
163
 */
164
165
planet_parse_class('
166
class [CLASS_PREFIX]ArticleHandler extends XoopsPersistableObjectHandler
167
{
168
    /**
169
     * Constructor
170
     *
171
     * @param object $db reference to the {@link XoopsDatabase} object
172
     **/
173
    public function __construct(XoopsDatabase $db) {
174
        parent::__construct($db, planet_DB_prefix("article", true), "Barticle", "art_id", "art_title");
175
    }
176
177
    /**
178
     * insert new articles if content is changed
179
     *
180
     * @param  array $articles
181
     * @param  int   $blog_id
182
     * @return int   inserted article count
183
     */
184
    public function do_update(&$articles, $blog_id = 0)
185
    {
186
        if ($blog_id>0) {
187
            $crit_blog = new Criteria("blog_id", $blog_id);
188
        } else {
189
            $crit_blog = null;
190
        }
191
        $count = 0;
192
        foreach ($articles as $article) {
193
            $criteria = new CriteriaCompo();
194
            if ($article["blog_id"]) {
195
                $criteria->add(new Criteria("blog_id", $article["blog_id"]));
196
            } else {
197
                $criteria->add($crit_blog);
198
            }
199
            $criteria->add(new Criteria("art_link", $article["art_link"]));
200
            $_count = $this->getCount($criteria);
201
            unset($criteria);
202
            if($_count>0) continue;
203
            $article_obj = $this->create();
204
            $article_obj->setVars($article, true);
205
            $this->insert($article_obj, true);
206
            unset($article_obj);
207
            ++$count ;
208
        }
209
210
        return $count;
211
    }
212
213
    /**
214
     * get a list of articles matching a condition of a category
215
     *
216
     * @param  object $criteria {@link CriteriaElement} to match
217
     * @param  array  $tags     variables to fetch
218
     * @param  bool   $asObject flag indicating as object, otherwise as array
219
     * @return array  of articles {@link Barticle}
220
     */
221
       public function &getByCategory($criteria = null, $tags = null, $asObject=true)
222
    {
223
        if (is_array($tags) && count($tags)>0) {
224
            if(!in_array($this->keyName, $tags)) $tags[] = $this->keyName;
225
            $select = implode(",", $tags);
226
        } else $select = "*";
227
        $limit = null;
228
        $start = null;
229
        $sql = "SELECT $select".
230
                " FROM " . $this->table. " AS a".
231
                //" LEFT JOIN (SELECT blog_id, cat_id FROM ".planet_DB_prefix("blogcat").") AS bc ON a.blog_id = bc.blog_id";
232
                " LEFT JOIN ".planet_DB_prefix("blogcat")." AS bc ON a.blog_id = bc.blog_id";
233
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
234
            $sql .= " ".$criteria->renderWhere();
235
            if ($criteria->getSort() != "") {
236
                $sql .= " ORDER BY ".$criteria->getSort()." ".$criteria->getOrder();
237
                $orderSet = true;
238
            }
239
            $limit = $criteria->getLimit();
240
            $start = $criteria->getStart();
241
        }
242
        if(empty($orderSet)) $sql .= " ORDER BY ".$this->keyName." DESC";
243
        $result = $this->db->query($sql, $limit, $start);
244
        $ret = array();
245
        while ($myrow = $this->db->fetchArray($result)) {
246
            $object = $this->create(false);
247
            $object->assignVars($myrow);
248
            if ($asObject) {
249
                $ret[$myrow[$this->keyName]] = $object;
250
            } else {
251
                foreach ($myrow as $key=>$val) {
252
                    $ret[$myrow[$this->keyName]][$key] = ($object->vars[$key]["changed"])?$object->getVar($key):$val;
253
                }
254
            }
255
            unset($object);
256
        }
257
258
        return $ret;
259
    }
260
261
    /**
262
     * count articles matching a condition of a category (categories)
263
     *
264
     * @param  object $criteria {@link CriteriaElement} to match
265
     * @return int    count of articles
266
     */
267
       public function getCountByCategory($criteria = null)
268
    {
269
        $sql = "SELECT COUNT(DISTINCT a.art_id) AS count".
270
                " FROM " . $this->table. " AS a".
271
                //" LEFT JOIN (SELECT blog_id, cat_id FROM ".planet_DB_prefix("blogcat").") AS bc ON a.blog_id = bc.blog_id";
272
                " LEFT JOIN ".planet_DB_prefix("blogcat")." AS bc ON a.blog_id = bc.blog_id";
273
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
274
            $sql .= " ".$criteria->renderWhere();
275
        }
276
        if (!$result = $this->db->query($sql)) {
277
            return false;
278
        }
279
        $myrow = $this->db->fetchArray($result);
280
281
        return (int)($myrow["count"]);
282
    }
283
284
       public function getCountsByCategory($criteria = null)
285
    {
286
        $sql = "SELECT bc.cat_id, COUNT(*)".
287
                " FROM " . $this->table. " AS a".
288
                //" LEFT JOIN (SELECT blog_id, cat_id FROM ".planet_DB_prefix("blogcat").") AS bc ON a.blog_id = bc.blog_id";
289
                " LEFT JOIN ".planet_DB_prefix("blogcat")." AS bc ON a.blog_id = bc.blog_id";
290
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
291
            $sql .= " ".$criteria->renderWhere();
292
        }
293
        $sql .=" GROUP BY bc.cat_id";
294
        if (!$result = $this->db->query($sql)) {
295
            return false;
296
        }
297
        $ret = array();
298
        while (list($id, $count) = $this->db->fetchRow($result)) {
299
            $ret[$id] = $count;
300
        }
301
302
        return $ret;
303
    }
304
305
    /**
306
     * get a list of articles matching a condition of user bookmark
307
     *
308
     * @param  object $criteria {@link CriteriaElement} to match
309
     * @param  array  $tags     variables to fetch
310
     * @param  bool   $asObject flag indicating as object, otherwise as array
311
     * @return array  of articles {@link Barticle}
312
     */
313
       public function &getByBookmark($criteria = null, $tags = null, $asObject=true)
314
    {
315
        if (is_array($tags) && count($tags)>0) {
316
            if(!in_array($this->keyName, $tags)) $tags[] = $this->keyName;
317
            $select = implode(",", $tags);
318
        } else $select = "*";
319
        $limit = null;
320
        $start = null;
321
        $sql = "SELECT $select".
322
                " FROM " . $this->table. " AS a".
323
                //" LEFT JOIN (SELECT blog_id, bm_uid FROM ".planet_DB_prefix("bookmark").") AS bm ON a.blog_id = bm.blog_id";
324
                " LEFT JOIN ".planet_DB_prefix("bookmark")." AS bm ON a.blog_id = bm.blog_id";
325
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
326
            $sql .= " ".$criteria->renderWhere();
327
            if ($criteria->getSort() != "") {
328
                $sql .= " ORDER BY ".$criteria->getSort()." ".$criteria->getOrder();
329
                $orderSet = true;
330
            }
331
            $limit = $criteria->getLimit();
332
            $start = $criteria->getStart();
333
        }
334
        if(empty($orderSet)) $sql .= " ORDER BY ".$this->keyName." DESC";
335
        $result = $this->db->query($sql, $limit, $start);
336
        $ret = array();
337
        while ($myrow = $this->db->fetchArray($result)) {
338
            $object = $this->create(false);
339
            $object->assignVars($myrow);
340
            if ($asObject) {
341
                $ret[$myrow[$this->keyName]] = $object;
342
            } else {
343
                foreach ($myrow as $key=>$val) {
344
                    $ret[$myrow[$this->keyName]][$key] = ($object->vars[$key]["changed"])?$object->getVar($key):$val;
345
                }
346
            }
347
            unset($object);
348
        }
349
350
        return $ret;
351
    }
352
353
    /**
354
     * count blogs matching a condition of user bookmark
355
     *
356
     * @param  object $criteria {@link CriteriaElement} to match
357
     * @return int    count of blogs
358
     */
359
       public function getCountByBookmark($criteria = null)
360
    {
361
        $sql = "SELECT COUNT(DISTINCT a.art_id) AS count".
362
                " FROM " . $this->table. " AS a".
363
                //" LEFT JOIN (SELECT blog_id, bm_uid FROM ".planet_DB_prefix("bookmark").") AS bm ON a.blog_id = bm.blog_id";
364
                " LEFT JOIN ".planet_DB_prefix("bookmark")." AS bm ON a.blog_id = bm.blog_id";
365
        if (isset($criteria) && is_subclass_of($criteria, "criteriaelement")) {
366
            $sql .= " ".$criteria->renderWhere();
367
        }
368
        if (!$result = $this->db->query($sql)) {
369
            return false;
370
        }
371
        $myrow = $this->db->fetchArray($result);
372
373
        return (int)($myrow["count"]);
374
    }
375
    /**
376
     * Get the previous article and the next article of an article
377
     *
378
     * @param  object $article reference to the article
379
     * @param  int    $blog    blog ID
380
     * @return array
381
     **/
382
    // To be optimized
383
    public function &getSibling(&$article, $blog = 0)
384
    {
385
        $ret = array();
386
387
        $crit_prev = new CriteriaCompo(new Criteria("art_id", $article->getVar("art_id"), "<"));
388
        if ($blog>0) {
389
            $crit_prev->add(new Criteria("blog_id", $blog));
390
        }
391
        $crit_prev->setSort("art_id");
392
        $crit_prev->setOrder("DESC");
393
        $crit_prev->setLimit(1);
394
        $art_prev = $this->getObjects($crit_prev);
395
        if (count($art_prev)>0) {
396
            $ret["previous"] = array("id"=>$art_prev[0]->getVar("art_id"),"title"=>$art_prev[0]->getVar("art_title"));
397
            unset($art_prev);
398
        }
399
400
        $crit_next = new CriteriaCompo(new Criteria("art_id", $article->getVar("art_id"), ">"));
401
        if ($blog>0) {
402
            $crit_next->add(new Criteria("blog_id", $blog));
403
        }
404
        $crit_next->setSort("art_id");
405
        $crit_next->setOrder("DESC");
406
        $crit_next->setLimit(1);
407
        $art_next = $this->getObjects($crit_next);
408
        if (count($art_next)>0) {
409
            $ret["next"] = array("id"=>$art_next[0]->getVar("art_id"),"title"=>$art_next[0]->getVar("art_title"));
410
            unset($art_next);
411
        }
412
413
        return $ret;
414
    }
415
416
    /**
417
     * Update comment count of the article
418
     *
419
     * @param  object $article {@link Article} reference to Article
420
     * @param  int    $total   total comment count
421
     * @return bool   true on success
422
     */
423
   public function updateComments(&$article, $total)
424
    {
425
        $article->setVar("art_comments", (int)($total), true);
426
427
        return $this->insert($article, true);
428
    }
429
430
    /**
431
     * delete an article from the database
432
     *
433
     * {@link Text}
434
     *
435
     * @param  object $article {@link Article} reference to Article
436
     * @param  bool   $force   flag to force the query execution despite security settings
437
     * @return bool   true on success
438
     */
439
    public function delete(XoopsObject $article, $force = true)
440
    {
441
        $rate_handler = xoops_getModuleHandler("rate", $GLOBALS["moddirname"]);
442
        $rate_handler->deleteAll(new Criteria("art_id", $article->getVar("art_id")));
443
444
        xoops_comment_delete($GLOBALS["xoopsModule"]->getVar("mid"), $article->getVar("art_id"));
445
        xoops_notification_deletebyitem($GLOBALS["xoopsModule"]->getVar("mid"), "article", $article->getVar("art_id"));
446
447
        parent::delete($article, $force);
448
449
        $article = null;
450
        unset($article);
451
452
        return true;
453
    }
454
}
455
');
456