Dotclear


Ignore:
Timestamp:
06/15/18 18:31:29 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
sql-statement
Message:

Apply SQL Statement in DC code, work in progress

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.blog.php

    r3731 r3761  
    209209        $cur->blog_upddt = date('Y-m-d H:i:s'); 
    210210 
    211         $cur->update("WHERE blog_id = '" . $this->con->escape($this->id) . "' "); 
     211        $sql = new dcUpdateStatement($this->core, 'coreTriggerBlog'); 
     212        $sql 
     213            ->where('blog_id = ' . $sql->quote($this->id)); 
     214 
     215        $cur->update($sql->whereStatement()); 
    212216 
    213217        # --BEHAVIOR-- coreBlogAfterTriggerBlog 
     
    241245        # Get posts affected by comments edition 
    242246        if (empty($affected_posts)) { 
    243             $strReq = 
    244             'SELECT post_id ' . 
    245             'FROM ' . $this->prefix . 'comment ' . 
    246             'WHERE comment_id' . $this->con->in($comments_ids) . 
    247                 'GROUP BY post_id'; 
    248  
    249             $rs = $this->con->select($strReq); 
     247            $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsScope'); 
     248            $sql 
     249                ->columns('post_id') 
     250                ->from($this->prefix . 'comment') 
     251                ->where('comment_id' . $sql->in($comments_ids)) 
     252                ->group('post_id'); 
     253 
     254            $rs = $this->con->select($sql->statement()); 
    250255 
    251256            $affected_posts = array(); 
     
    253258                $affected_posts[] = (integer) $rs->post_id; 
    254259            } 
     260            unset($sql); 
    255261        } 
    256262 
     
    260266 
    261267        # Count number of comments if exists for affected posts 
    262         $strReq = 
    263         'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback ' . 
    264         'FROM ' . $this->prefix . 'comment ' . 
    265         'WHERE comment_status = 1 ' . 
    266         'AND post_id' . $this->con->in($affected_posts) . 
    267             'GROUP BY post_id,comment_trackback'; 
    268  
    269         $rs = $this->con->select($strReq); 
     268        $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsCount'); 
     269        $sql 
     270            ->columns(array('post_id', 'COUNT(post_id) AS nb_comment', 'comment_trackback')) 
     271            ->from($this->prefix . 'comment') 
     272            ->where(array( 
     273                'comment_status = 1', 
     274                'post_id' . $sql->in($affected_posts) 
     275            )) 
     276            ->group(array('post_id', 'comment_trackback')); 
     277 
     278        $rs = $this->con->select($sql->statement()); 
    270279 
    271280        $posts = array(); 
     
    461470    private function getCategoriesCounter($params = array()) 
    462471    { 
    463         $strReq = 
    464         'SELECT  C.cat_id, COUNT(P.post_id) AS nb_post ' . 
    465         'FROM ' . $this->prefix . 'category AS C ' . 
    466         'JOIN ' . $this->prefix . "post P ON (C.cat_id = P.cat_id AND P.blog_id = '" . $this->con->escape($this->id) . "' ) " . 
    467         "WHERE C.blog_id = '" . $this->con->escape($this->id) . "' "; 
     472        $sql = new dcSelectStatement($this->core, 'coreGetCategoriesCounter'); 
     473        $sql 
     474            ->columns(array('C.cat_id', 'COUNT(P.post_id) AS nb_post')) 
     475            ->from($this->prefix . 'category AS C') 
     476            ->join('JOIN ' . $this->prefix . 'post P ' . 
     477                'ON (C.cat_id = P.cat_id AND P.blog_id = ' . $sql->quote($this->id) . ')') 
     478            ->where('C.blog_id = ' . $sql->quote($this->id)); 
    468479 
    469480        if (!$this->core->auth->userID()) { 
    470             $strReq .= 'AND P.post_status = 1 '; 
     481            $sql->where('P.post_status = 1'); 
    471482        } 
    472483 
    473484        if (!empty($params['post_type'])) { 
    474             $strReq .= 'AND P.post_type ' . $this->con->in($params['post_type']); 
    475         } 
    476  
    477         $strReq .= 'GROUP BY C.cat_id '; 
    478  
    479         $rs       = $this->con->select($strReq); 
     485            $sql->where('P.post_type ' . $sql->in($params['post_type'])); 
     486        } 
     487 
     488        $sql->group('C.cat_id'); 
     489 
     490        $rs       = $this->con->select($sql->statement()); 
    480491        $counters = array(); 
    481492        while ($rs->fetch()) { 
     
    568579        $this->core->callBehavior('coreBeforeCategoryUpdate', $this, $cur); 
    569580 
    570         $cur->update( 
    571             'WHERE cat_id = ' . (integer) $id . ' ' . 
    572             "AND blog_id = '" . $this->con->escape($this->id) . "' "); 
     581        $sql = new dcUpdateStatement($this->core, 'coreCategoryUpdate'); 
     582        $sql 
     583            ->where(array( 
     584                'cat_id = ' . (integer) $id, 
     585                'blog_id = ' . $sql->quote($this->id) 
     586            )); 
     587        $cur->update($sql->whereStatement()); 
    573588 
    574589        # --BEHAVIOR-- coreAfterCategoryUpdate 
     
    639654        } 
    640655 
    641         $strReq = 'SELECT COUNT(post_id) AS nb_post ' . 
    642         'FROM ' . $this->prefix . 'post ' . 
    643         'WHERE cat_id = ' . (integer) $id . ' ' . 
    644         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
    645  
    646         $rs = $this->con->select($strReq); 
     656        $sql = new dcSelectStatement($this->core, 'coreCategoryPostCount'); 
     657        $sql 
     658            ->columns('COUNT(post_id) AS nb_post') 
     659            ->from($this->prefix . 'post') 
     660            ->where(array( 
     661                'cat_id = ' . (integer) $id, 
     662                'blog_id = ' . $sql->quote($this->id) 
     663            )); 
     664        $rs = $this->con->select($sql->statement()); 
    647665 
    648666        if ($rs->nb_post > 0) { 
     
    669687    private function checkCategory($title, $url, $id = null) 
    670688    { 
    671         # Let's check if URL is taken... 
    672         $strReq = 
    673         'SELECT cat_url FROM ' . $this->prefix . 'category ' . 
    674         "WHERE cat_url = '" . $this->con->escape($url) . "' " . 
    675         ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') . 
    676         "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    677             'ORDER BY cat_url DESC'; 
    678  
    679         $rs = $this->con->select($strReq); 
     689        // Let's check if URL is taken... 
     690        $sql = new dcSelectStatement($this->core, 'coreCheckCategoryURL'); 
     691        $sql 
     692            ->columns('cat_url') 
     693            ->from($this->prefix . 'category') 
     694            ->where('blog_id = ' . $sql->quote($this->id)) 
     695            ->order('cat_url DESC'); 
     696        if ($id) { 
     697            $sql->where('AND cat_id <> ' . (integer) $i); 
     698        } 
     699        $sql->cond('AND cat_url = ' . $sql->quote($url)); 
     700 
     701        $rs = $this->con->select($sql->statement()); 
    680702 
    681703        if (!$rs->isEmpty()) { 
    682             if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') { 
    683                 $clause = "REGEXP '^" . $this->con->escape($url) . "[0-9]+$'"; 
    684             } elseif ($this->con->driver() == 'pgsql') { 
    685                 $clause = "~ '^" . $this->con->escape($url) . "[0-9]+$'"; 
    686             } else { 
    687                 $clause = "LIKE '" . $this->con->escape($url) . "%'"; 
    688             } 
    689             $strReq = 
    690             'SELECT cat_url FROM ' . $this->prefix . 'category ' . 
    691             "WHERE cat_url " . $clause . ' ' . 
    692             ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') . 
    693             "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    694                 'ORDER BY cat_url DESC '; 
    695  
    696             $rs = $this->con->select($strReq); 
     704            // Replace condition on cat_url 
     705            $sql->cond('AND cat_url ' . $sql->regexp($url), true); 
     706 
     707            $rs = $this->con->select($sql->statement()); 
    697708            $a  = array(); 
    698709            while ($rs->fetch()) { 
     
    791802        $this->core->callBehavior('coreBlogBeforeGetPosts', $params); 
    792803 
     804        $sql = new dcSelectStatement($this->core, 'coreGetPosts'); 
     805 
    793806        if ($count_only) { 
    794             $strReq = 'SELECT count(DISTINCT P.post_id) '; 
     807            $sql->columns('count(DISTINCT P.post_id)'); 
    795808        } elseif (!empty($params['sql_only'])) { 
    796             $strReq = 'SELECT P.post_id '; 
     809            $sql->columns('P.post_id'); 
    797810        } else { 
    798             if (!empty($params['no_content'])) { 
    799                 $content_req = ''; 
    800             } else { 
    801                 $content_req = 
    802                     'post_excerpt, post_excerpt_xhtml, ' . 
    803                     'post_content, post_content_xhtml, post_notes, '; 
     811            if (empty($params['no_content'])) { 
     812                $sql->columns(array('post_excerpt', 'post_excerpt_xhtml', 'post_content', 'post_content_xhtml', 'post_notes')); 
    804813            } 
    805814 
    806815            if (!empty($params['columns']) && is_array($params['columns'])) { 
    807                 $content_req .= implode(', ', $params['columns']) . ', '; 
    808             } 
    809  
    810             $strReq = 
    811                 'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, ' . 
    812                 'post_tz, post_creadt, post_upddt, post_format, post_password, ' . 
    813                 'post_url, post_lang, post_title, ' . $content_req . 
    814                 'post_type, post_meta, ' . 
    815                 'post_status, post_firstpub, post_selected, post_position, ' . 
    816                 'post_open_comment, post_open_tb, nb_comment, nb_trackback, ' . 
    817                 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, ' . 
    818                 'U.user_url, ' . 
    819                 'C.cat_title, C.cat_url, C.cat_desc '; 
    820         } 
    821  
    822         $strReq .= 
    823         'FROM ' . $this->prefix . 'post P ' . 
    824         'INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id ' . 
    825         'LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id '; 
     816                $sql->columns($params['columns']); 
     817            } 
     818 
     819            $sql->columns(array('P.post_id', 'P.blog_id', 'P.user_id', 'P.cat_id', 'post_dt', 'post_tz', 'post_creadt', 
     820                'post_upddt', 'post_format', 'post_password', 'post_url', 'post_lang', 'post_title', 'post_type', 
     821                'post_meta', 'post_status', 'post_firstpub', 'post_selected', 'post_position', 'post_open_comment', 
     822                'post_open_tb', 'nb_comment', 'nb_trackback', 'U.user_name', 'U.user_firstname', 'U.user_displayname', 
     823                'U.user_email', 'U.user_url', 'C.cat_title', 'C.cat_url', 'C.cat_desc')); 
     824        } 
     825 
     826        $sql->from($this->prefix . 'post P') 
     827            ->join('INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id') 
     828            ->join('LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id'); 
     829 
     830        if (!empty($params['join'])) { 
     831            $sql->join($params['join']); 
     832        } 
    826833 
    827834        if (!empty($params['from'])) { 
    828             $strReq .= $params['from'] . ' '; 
    829         } 
    830  
    831         $strReq .= 
    832         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' "; 
     835            $sql->from($params['from']); 
     836        } 
     837 
     838        $sql->where('P.blog_id = ' . $sql->quote($this->id)); 
    833839 
    834840        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    835             $strReq .= 'AND ((post_status = 1 '; 
    836  
     841            $cond = '((post_status = 1'; 
    837842            if ($this->without_password) { 
    838                 $strReq .= 'AND post_password IS NULL '; 
    839             } 
    840             $strReq .= ') '; 
    841  
     843                $cond .= ' AND post_password IS NULL'; 
     844            } 
     845            $cond .= ')'; 
    842846            if ($this->core->auth->userID()) { 
    843                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    844             } else { 
    845                 $strReq .= ') '; 
    846             } 
     847                $cond .= 'OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     848            } 
     849            $cond .= ')'; 
     850            $sql->where($cond); 
    847851        } 
    848852 
     
    850854        if (isset($params['post_type'])) { 
    851855            if (is_array($params['post_type']) || $params['post_type'] != '') { 
    852                 $strReq .= 'AND post_type ' . $this->con->in($params['post_type']); 
     856                $sql->where('post_type ' . $sql->in($params['post_type'])); 
    853857            } 
    854858        } else { 
    855             $strReq .= "AND post_type = 'post' "; 
     859            $sql->where("post_type = 'post' "); 
    856860        } 
    857861 
     
    862866                $params['post_id'] = array((integer) $params['post_id']); 
    863867            } 
    864             $strReq .= 'AND P.post_id ' . $this->con->in($params['post_id']); 
     868            $sql->where('P.post_id ' . $sql->in($params['post_id'])); 
    865869        } 
    866870 
     
    871875                $params['exclude_post_id'] = array((integer) $params['exclude_post_id']); 
    872876            } 
    873             $strReq .= 'AND P.post_id NOT ' . $this->con->in($params['exclude_post_id']); 
     877            $sql->where('P.post_id NOT ' . $sql->in($params['exclude_post_id'])); 
    874878        } 
    875879 
    876880        if (isset($params['post_url']) && $params['post_url'] !== '') { 
    877             $strReq .= "AND post_url = '" . $this->con->escape($params['post_url']) . "' "; 
     881            $sql->where('post_url = ' . $sql->quote($params['post_url'])); 
    878882        } 
    879883 
    880884        if (!empty($params['user_id'])) { 
    881             $strReq .= "AND U.user_id = '" . $this->con->escape($params['user_id']) . "' "; 
     885            $sql->where('U.user_id = ' . $sql->quote($params['user_id'])); 
    882886        } 
    883887 
     
    889893                array_walk($params['cat_id'], function (&$v, $k) {$v = $v . " ?not";}); 
    890894            } 
    891             $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_id'], 'cat_id') . ' '; 
     895            $sql->where($this->getPostsCategoryFilter($params['cat_id'], 'cat_id')); 
    892896        } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { 
    893897            if (!is_array($params['cat_url'])) { 
     
    897901                array_walk($params['cat_url'], function (&$v, $k) {$v = $v . " ?not";}); 
    898902            } 
    899             $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_url'], 'cat_url') . ' '; 
     903            $sql->where($this->getPostsCategoryFilter($params['cat_url'], 'cat_url')); 
    900904        } 
    901905 
    902906        /* Other filters */ 
    903907        if (isset($params['post_status'])) { 
    904             $strReq .= 'AND post_status = ' . (integer) $params['post_status'] . ' '; 
     908            $sql->where('post_status = ' . (integer) $params['post_status']); 
    905909        } 
    906910 
    907911        if (isset($params['post_firstpub'])) { 
    908             $strReq .= 'AND post_firstpub = ' . (integer) $params['post_firstpub'] . ' '; 
     912            $sql->where('post_firstpub = ' . (integer) $params['post_firstpub']); 
    909913        } 
    910914 
    911915        if (isset($params['post_selected'])) { 
    912             $strReq .= 'AND post_selected = ' . (integer) $params['post_selected'] . ' '; 
     916            $sql->where('post_selected = ' . (integer) $params['post_selected']); 
    913917        } 
    914918 
    915919        if (!empty($params['post_year'])) { 
    916             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . ' = ' . 
    917             "'" . sprintf('%04d', $params['post_year']) . "' "; 
     920            $sql->where($sql->dateFormat('post_dt', '%Y') . ' = ' . "'" . sprintf('%04d', $params['post_year']) . "'"); 
    918921        } 
    919922 
    920923        if (!empty($params['post_month'])) { 
    921             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . ' = ' . 
    922             "'" . sprintf('%02d', $params['post_month']) . "' "; 
     924            $sql->where($sql->dateFormat('post_dt', '%m') . ' = ' . "'" . sprintf('%02d', $params['post_month']) . "'"); 
    923925        } 
    924926 
    925927        if (!empty($params['post_day'])) { 
    926             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . ' = ' . 
    927             "'" . sprintf('%02d', $params['post_day']) . "' "; 
     928            $sql->where($sql->dateFormat('post_dt', '%d') . ' = ' . "'" . sprintf('%02d', $params['post_day']) . "'"); 
    928929        } 
    929930 
    930931        if (!empty($params['post_lang'])) { 
    931             $strReq .= "AND P.post_lang = '" . $this->con->escape($params['post_lang']) . "' "; 
     932            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang'])); 
    932933        } 
    933934 
     
    943944                if ($words) { 
    944945                    foreach ($words as $i => $w) { 
    945                         $words[$i] = "post_words LIKE '%" . $this->con->escape($w) . "%'"; 
     946                        $words[$i] = "post_words LIKE '%" . $sql->escape($w) . "%'"; 
    946947                    } 
    947                     $strReq .= 'AND ' . implode(' AND ', $words) . ' '; 
     948                    $sql->where(implode(' AND ', $words)); 
    948949                } 
    949950            } 
     
    951952 
    952953        if (isset($params['media'])) { 
    953             if ($params['media'] == '0') { 
    954                 $strReq .= 'AND NOT '; 
    955             } else { 
    956                 $strReq .= 'AND '; 
    957             } 
    958             $strReq .= 'EXISTS (SELECT M.post_id FROM ' . $this->prefix . 'post_media M ' . 
    959                 'WHERE M.post_id = P.post_id '; 
     954            $sqlEx = new dcSelectStatement($this->core, 'coreGetPostsMedia'); 
     955            $sqlEx 
     956                ->columns('M.post_id') 
     957                ->from($this->prefix . 'post_media M') 
     958                ->where('M.post_id = P.post_id'); 
    960959            if (isset($params['link_type'])) { 
    961                 $strReq .= " AND M.link_type " . $this->con->in($params['link_type']) . " "; 
    962             } 
    963             $strReq .= ")"; 
     960                $sqlEx->where('M.link_type ' . $sql->in($params['link_type'])); 
     961            } 
     962            $sql->where(($params['media'] == '0' ? 'NOT ' : '') . 'EXISTS (' . $sqlEx->statement() . ')'); 
    964963        } 
    965964 
    966965        if (!empty($params['where'])) { 
    967             $strReq .= $params['where'] . ' '; 
     966            $sql->cond($params['where']); 
    968967        } 
    969968 
    970969        if (!empty($params['sql'])) { 
    971             $strReq .= $params['sql'] . ' '; 
     970            $sql->sql($params['sql']); 
    972971        } 
    973972 
    974973        if (!$count_only) { 
    975974            if (!empty($params['order'])) { 
    976                 $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     975                $sql->order($sql->escape($params['order'])); 
    977976            } else { 
    978                 $strReq .= 'ORDER BY post_dt DESC '; 
    979             } 
    980         } 
    981  
    982         if (!$count_only && !empty($params['limit'])) { 
    983             $strReq .= $this->con->limit($params['limit']); 
    984         } 
     977                $sql->order('post_dt DESC'); 
     978            } 
     979            if (!empty($params['limit'])) { 
     980                $sql->limit($params['limit']); 
     981            } 
     982        } 
     983 
     984        $query = $sql->statement(); 
    985985 
    986986        if (!empty($params['sql_only'])) { 
    987             return $strReq; 
    988         } 
    989  
    990         $rs            = $this->con->select($strReq); 
     987            return $query; 
     988        } 
     989 
     990        $rs            = $this->con->select($query); 
    991991        $rs->core      = $this->core; 
    992992        $rs->_nb_media = array(); 
     
    10701070    public function getLangs($params = array()) 
    10711071    { 
    1072         $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang ' . 
    1073         'FROM ' . $this->prefix . 'post ' . 
    1074         "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1075             "AND post_lang <> '' " . 
    1076             "AND post_lang IS NOT NULL "; 
     1072        $sql = new dcSelectStatement($this->core, 'coreGetLangs'); 
     1073        $sql 
     1074            ->columns(array('COUNT(post_id) as nb_post', 'post_lang')) 
     1075            ->from($this->prefix . 'post') 
     1076            ->where(array( 
     1077                'blog_id = ' . $sql->quote($this->id), 
     1078                "post_lang <> ''", 
     1079                'post_lang IS NOT NULL' 
     1080            )); 
    10771081 
    10781082        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1079             $strReq .= 'AND ((post_status = 1 '; 
     1083            $cond = '((post_status = 1'; 
    10801084 
    10811085            if ($this->without_password) { 
    1082                 $strReq .= 'AND post_password IS NULL '; 
    1083             } 
    1084             $strReq .= ') '; 
     1086                $cond .= ' AND post_password IS NULL'; 
     1087            } 
     1088            $cond .= ')'; 
    10851089 
    10861090            if ($this->core->auth->userID()) { 
    1087                 $strReq .= "OR user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    1088             } else { 
    1089                 $strReq .= ') '; 
    1090             } 
     1091                $cond .= " OR user_id = " . $sql->quote($this->core->auth->userID()); 
     1092            } 
     1093            $cond .= ')'; 
     1094            $sql->where($cond); 
    10911095        } 
    10921096 
    10931097        if (isset($params['post_type'])) { 
    10941098            if ($params['post_type'] != '') { 
    1095                 $strReq .= "AND post_type = '" . $this->con->escape($params['post_type']) . "' "; 
     1099                $sql->where("post_type = " . $sql->quote($params['post_type'])); 
    10961100            } 
    10971101        } else { 
    1098             $strReq .= "AND post_type = 'post' "; 
     1102            $sql->where("post_type = 'post'"); 
    10991103        } 
    11001104 
    11011105        if (isset($params['lang'])) { 
    1102             $strReq .= "AND post_lang = '" . $this->con->escape($params['lang']) . "' "; 
    1103         } 
    1104  
    1105         $strReq .= 'GROUP BY post_lang '; 
     1106            $sql->where("post_lang = " . $sql->quote($params['lang'])); 
     1107        } 
     1108 
     1109        $sql->group('post_lang'); 
    11061110 
    11071111        $order = 'desc'; 
     
    11091113            $order = $params['order']; 
    11101114        } 
    1111         $strReq .= 'ORDER BY post_lang ' . $order . ' '; 
    1112  
    1113         return $this->con->select($strReq); 
     1115        $sql->order('post_lang ' . $order); 
     1116 
     1117        return $this->con->select($sql->statement()); 
    11141118    } 
    11151119 
     
    11341138    public function getDates($params = array()) 
    11351139    { 
     1140        $sql = new dcSelectStatement($this->core, 'coreGetDates'); 
     1141 
    11361142        $dt_f  = '%Y-%m-%d'; 
    11371143        $dt_fc = '%Y%m%d'; 
     
    11481154        $dt_fc .= '000000'; 
    11491155 
    1150         $cat_field = $catReq = $limit = ''; 
     1156        $sql 
     1157            ->distinct() 
     1158            ->columns(array( 
     1159                $sql->dateFormat('post_dt', $dt_f) . ' AS dt', 
     1160                'COUNT(P.post_id) AS nb_post' 
     1161            )) 
     1162            ->from($this->prefix . 'post P') 
     1163            ->join('LEFT JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id') 
     1164            ->group('dt'); 
    11511165 
    11521166        if (isset($params['cat_id']) && $params['cat_id'] !== '') { 
    1153             $catReq    = 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' '; 
    1154             $cat_field = ', C.cat_url '; 
     1167            $sql 
     1168                ->columns('C.cat_url') 
     1169                ->where('P.cat_id = ' . (integer) $params['cat_id']) 
     1170                ->group('C.car_url'); 
    11551171        } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { 
    1156             $catReq    = "AND C.cat_url = '" . $this->con->escape($params['cat_url']) . "' "; 
    1157             $cat_field = ', C.cat_url '; 
     1172            $sql 
     1173                ->columns('C.cat_url') 
     1174                ->where('C.cat_url = ' . $sql->quote($params['cat_url'])) 
     1175                ->group('C.car_url'); 
    11581176        } 
    11591177        if (!empty($params['post_lang'])) { 
    1160             $catReq = 'AND P.post_lang = \'' . $params['post_lang'] . '\' '; 
    1161         } 
    1162  
    1163         $strReq = 'SELECT DISTINCT(' . $this->con->dateFormat('post_dt', $dt_f) . ') AS dt ' . 
    1164         $cat_field . 
    1165         ',COUNT(P.post_id) AS nb_post ' . 
    1166         'FROM ' . $this->prefix . 'post P LEFT JOIN ' . $this->prefix . 'category C ' . 
    1167         'ON P.cat_id = C.cat_id ' . 
    1168         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' " . 
    1169             $catReq; 
     1178            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang'])); 
     1179        } 
    11701180 
    11711181        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1172             $strReq .= 'AND ((post_status = 1 '; 
    1173  
     1182            $cond = '((post_status = 1'; 
    11741183            if ($this->without_password) { 
    1175                 $strReq .= 'AND post_password IS NULL '; 
    1176             } 
    1177             $strReq .= ') '; 
     1184                $cond .= ' AND post_password IS NULL'; 
     1185            } 
     1186            $cond .= ')'; 
    11781187 
    11791188            if ($this->core->auth->userID()) { 
    1180                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    1181             } else { 
    1182                 $strReq .= ') '; 
    1183             } 
     1189                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     1190            } 
     1191            $cond .= ')'; 
     1192            $sql->where($cond); 
    11841193        } 
    11851194 
    11861195        if (!empty($params['post_type'])) { 
    1187             $strReq .= "AND post_type " . $this->con->in($params['post_type']) . " "; 
     1196            $sql->where("post_type " . $sql->in($params['post_type'])); 
    11881197        } else { 
    1189             $strReq .= "AND post_type = 'post' "; 
     1198            $sql->where("post_type = 'post'"); 
    11901199        } 
    11911200 
    11921201        if (!empty($params['year'])) { 
    1193             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "' "; 
     1202            $sql->where($sql->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "'"); 
    11941203        } 
    11951204 
    11961205        if (!empty($params['month'])) { 
    1197             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "' "; 
     1206            $sql->where($sql->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "'"); 
    11981207        } 
    11991208 
    12001209        if (!empty($params['day'])) { 
    1201             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "' "; 
     1210            $sql->where($sql->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "'"); 
    12021211        } 
    12031212 
     
    12161225            $dt = date('YmdHis', strtotime($dt)); 
    12171226 
    1218             $strReq .= 'AND ' . $this->con->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "' "; 
    1219             $limit = $this->con->limit(1); 
    1220         } 
    1221  
    1222         $strReq .= 'GROUP BY dt ' . $cat_field; 
     1227            $sql 
     1228                ->where($sql->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "'") 
     1229                ->limit(1); 
     1230        } 
    12231231 
    12241232        $order = 'desc'; 
     
    12261234            $order = $params['order']; 
    12271235        } 
    1228  
    1229         $strReq .= 
    1230             'ORDER BY dt ' . $order . ' ' . 
    1231             $limit; 
    1232  
    1233         $rs = $this->con->select($strReq); 
     1236        $sql->order('dt ' . $order); 
     1237 
     1238        $rs = $this->con->select($sql->statement()); 
    12341239        $rs->extend('rsExtDates'); 
    12351240        return $rs; 
     
    12531258        { 
    12541259            # Get ID 
    1255             $rs = $this->con->select( 
    1256                 'SELECT MAX(post_id) ' . 
    1257                 'FROM ' . $this->prefix . 'post ' 
    1258             ); 
     1260            $sql = new dcSelectStatement($this->core, 'corePostCreateGetID'); 
     1261            $sql 
     1262                ->columns('MAX(post_id)') 
     1263                ->from($this->prefix . 'post'); 
     1264 
     1265            $rs = $this->con->select($sql->statement()); 
    12591266 
    12601267            $cur->post_id     = (integer) $rs->f(0) + 1; 
     
    13281335        $cur->post_upddt = date('Y-m-d H:i:s'); 
    13291336 
    1330         #If user is only "usage", we need to check the post's owner 
     1337        // If user is only "usage", we need to check the post's owner 
    13311338        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1332             $strReq = 'SELECT post_id ' . 
    1333             'FROM ' . $this->prefix . 'post ' . 
    1334             'WHERE post_id = ' . $id . ' ' . 
    1335             "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    1336  
    1337             $rs = $this->con->select($strReq); 
     1339            $sql = new dcSelectStatement($this->core, 'corePostUpdateCheckOwner'); 
     1340            $sql 
     1341                ->column('post_id') 
     1342                ->from($this->prefix . 'post') 
     1343                ->where(array( 
     1344                    'post_id = ' . $id, 
     1345                    'user_id = ' . $sql->quote($this->core->auth->userID()) 
     1346                )); 
     1347 
     1348            $rs = $this->con->select($sql->statement()); 
     1349            unset($sql); 
    13381350 
    13391351            if ($rs->isEmpty()) { 
     
    13451357        $this->core->callBehavior('coreBeforePostUpdate', $this, $cur); 
    13461358 
    1347         $cur->update('WHERE post_id = ' . $id . ' '); 
     1359        $sql = new dcUpdateStatement($this->core, 'corePostUpdate'); 
     1360        $sql->where('post_id = ' . $id); 
     1361 
     1362        $cur->update($sql->whereStatement()); 
    13481363 
    13491364        # --BEHAVIOR-- coreAfterPostUpdate 
     
    13811396        $status    = (integer) $status; 
    13821397 
    1383         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1384         "AND post_id " . $this->con->in($posts_ids); 
     1398        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsStatus'); 
     1399        $sql 
     1400            ->where(array( 
     1401                'blog_id = ' . $sql->quote($this->id), 
     1402                'post_id ' . $sql->in($posts_ids) 
     1403            )); 
    13851404 
    13861405        #If user can only publish, we need to check the post's owner 
    13871406        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1388             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1407            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    13891408        } 
    13901409 
     
    13941413        $cur->post_upddt  = date('Y-m-d H:i:s'); 
    13951414 
    1396         $cur->update($strReq); 
     1415        $cur->update($sql->whereStatement()); 
    13971416        $this->triggerBlog(); 
    13981417 
     
    14261445        $selected  = (boolean) $selected; 
    14271446 
    1428         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1429         "AND post_id " . $this->con->in($posts_ids); 
     1447        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsSelected'); 
     1448        $sql 
     1449            ->where(array( 
     1450                'blog_id = ' . $sql->quote($this->id), 
     1451                'post_id ' . $sql->in($posts_ids) 
     1452            )); 
    14301453 
    14311454        # If user is only usage, we need to check the post's owner 
    14321455        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1433             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1456            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    14341457        } 
    14351458 
     
    14391462        $cur->post_upddt    = date('Y-m-d H:i:s'); 
    14401463 
    1441         $cur->update($strReq); 
     1464        $cur->update($sql->whereStatement()); 
    14421465        $this->triggerBlog(); 
    14431466    } 
     
    14691492        $cat_id    = (integer) $cat_id; 
    14701493 
    1471         $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1472         "AND post_id " . $this->con->in($posts_ids); 
    1473  
    1474         # If user is only usage, we need to check the post's owner 
     1494        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsCategory'); 
     1495        $sql 
     1496            ->where(array( 
     1497                'blog_id = ' . $sql->quote($this->id), 
     1498                'post_id ' . $sql->in($posts_ids) 
     1499            )); 
     1500 
     1501        // If user is only usage, we need to check the post's owner 
    14751502        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1476             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
     1503            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
    14771504        } 
    14781505 
     
    14821509        $cur->post_upddt = date('Y-m-d H:i:s'); 
    14831510 
    1484         $cur->update($strReq); 
     1511        $cur->update($sql->whereStatement()); 
    14851512        $this->triggerBlog(); 
    14861513    } 
     
    15061533        $cur->post_upddt = date('Y-m-d H:i:s'); 
    15071534 
    1508         $cur->update( 
    1509             'WHERE cat_id = ' . $old_cat_id . ' ' . 
    1510             "AND blog_id = '" . $this->con->escape($this->id) . "' " 
    1511         ); 
     1535        $sql = new dcUpdateStatement($this->core, 'coreChangePostsCategory'); 
     1536        $sql 
     1537            ->where(array( 
     1538                'cat_id = ' . $old_cat_id, 
     1539                'blog_id = ' . $sql->quote($this->id) 
     1540            )); 
     1541        $cur->update($sql->whereStatement()); 
    15121542        $this->triggerBlog(); 
    15131543    } 
     
    15401570        } 
    15411571 
    1542         $strReq = 'DELETE FROM ' . $this->prefix . 'post ' . 
    1543         "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1544         "AND post_id " . $this->con->in($posts_ids); 
    1545  
    1546         #If user can only delete, we need to check the post's owner 
     1572        $sql = new dcDeleteStatement($this->core, 'coreDelPosts'); 
     1573        $sql 
     1574            ->from($this->prefix . 'post') 
     1575            ->where(array( 
     1576                'blog_id = ' . $sql->quote($this->id), 
     1577                'post_id ' . $sql->in($posts_ids) 
     1578            )); 
     1579 
     1580        // If user can only delete, we need to check the post's owner 
    15471581        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    1548             $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    1549         } 
    1550  
    1551         $this->con->execute($strReq); 
     1582            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID())); 
     1583        } 
     1584 
     1585        $this->con->execute($sql->statement()); 
    15521586        $this->triggerBlog(); 
    15531587    } 
     
    15581592    public function publishScheduledEntries() 
    15591593    { 
    1560         $strReq = 'SELECT post_id, post_dt, post_tz ' . 
    1561         'FROM ' . $this->prefix . 'post ' . 
    1562         'WHERE post_status = -1 ' . 
    1563         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
    1564  
    1565         $rs = $this->con->select($strReq); 
     1594        $sql = new dcSelectStatement($this->core, 'coreScheduledEntriesPublish'); 
     1595        $sql 
     1596            ->columns(array('post_id', 'post_dt', 'post_tz')) 
     1597            ->from($this->prefix . 'post') 
     1598            ->where(array( 
     1599                'post_status = -1', 
     1600                'blog_id = ' . $sql->quote($this->id) 
     1601            )); 
     1602 
     1603        $rs = $this->con->select($sql->statement()); 
     1604        unset($sql); 
    15661605 
    15671606        $now       = dt::toUTC(time()); 
     
    15881627            $this->core->callBehavior('coreBeforeScheduledEntriesPublish', $this, $to_change); 
    15891628 
    1590             $strReq = 
    1591             'UPDATE ' . $this->prefix . 'post SET ' . 
    1592             'post_status = 1 ' . 
    1593             "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1594             'AND post_id ' . $this->con->in((array) $to_change) . ' '; 
    1595             $this->con->execute($strReq); 
     1629            $sql = new dcUpdateStatement($this->core, 'coreScheduledEntriesPublish'); 
     1630            $sql 
     1631                ->reference($this->prefix . 'post') 
     1632                ->set('post_status = 1') 
     1633                ->where(array( 
     1634                    'blog_id = ' . $sql->quote($this->id), 
     1635                    'post_id ' . $sql->in((array) $to_change) 
     1636                )); 
     1637 
     1638            $this->con->execute($sql->statement()); 
    15961639            $this->triggerBlog(); 
    15971640 
     
    16241667        if (count($to_change)) { 
    16251668 
    1626             $strReq = 
    1627             'UPDATE ' . $this->prefix . 'post ' . 
    1628             'SET post_firstpub = 1 ' . 
    1629             "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1630             'AND post_id ' . $this->con->in((array) $to_change) . ' '; 
    1631             $this->con->execute($strReq); 
     1669            $sql = new dcUpdateStatement($this->core, 'coreFirstPublicationEntries'); 
     1670            $sql 
     1671                ->reference($this->prefix . 'post') 
     1672                ->set('post_firstpub = 1') 
     1673                ->where(array( 
     1674                    'blog_id = ' . $sql->quote($this->id), 
     1675                    'post_id ' . $sql->in((array) $to_change) 
     1676                )); 
     1677 
     1678            $this->con->execute($sql->statement()); 
    16321679 
    16331680            # --BEHAVIOR-- coreFirstPublicationEntries 
     
    16441691    public function getPostsUsers($post_type = 'post') 
    16451692    { 
    1646         $strReq = 'SELECT P.user_id, user_name, user_firstname, ' . 
    1647         'user_displayname, user_email ' . 
    1648         'FROM ' . $this->prefix . 'post P, ' . $this->prefix . 'user U ' . 
    1649         'WHERE P.user_id = U.user_id ' . 
    1650         "AND blog_id = '" . $this->con->escape($this->id) . "' "; 
     1693        $sql = new dcSelectStatement($this->core, 'coreGetPostsUsers'); 
     1694        $sql 
     1695            ->columns(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email')) 
     1696            ->from(array($this->prefix . 'post P', $this->prefix . 'user U')) 
     1697            ->where(array( 
     1698                'P.user_id = U.user_id', 
     1699                'blog_id = ' . $sql->quote($this->id) 
     1700            )); 
    16511701 
    16521702        if ($post_type) { 
    1653             $strReq .= "AND post_type = '" . $this->con->escape($post_type) . "' "; 
    1654         } 
    1655  
    1656         $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; 
    1657  
    1658         return $this->con->select($strReq); 
     1703            $sql->where("post_type = '" . $sql->escape($post_type) . "'"); 
     1704        } 
     1705 
     1706        $sql->group(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email')); 
     1707 
     1708        return $this->con->select($sql->statement()); 
    16591709    } 
    16601710 
     
    16871737 
    16881738        if (!empty($sub)) { 
    1689             $rs = $this->con->select( 
    1690                 'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM ' . $this->prefix . 'category ' . 
    1691                 "WHERE blog_id = '" . $this->con->escape($this->id) . "' " . 
    1692                 'AND ' . $field . ' ' . $this->con->in(array_keys($sub)) 
    1693             ); 
     1739            $sql = new dcSelectStatement($this->core); 
     1740            $sql 
     1741                ->columns(array('cat_id', 'cat_url', 'cat_lft', 'cat_rgt')) 
     1742                ->from($this->prefix . 'category') 
     1743                ->where(array( 
     1744                    'blog_id = ' . $sql->quote($this->id), 
     1745                    $field . ' ' . $sql->in(array_keys($sub)) 
     1746                )); 
     1747            $rs = $this->con->select($sql->statement()); 
    16941748 
    16951749            while ($rs->fetch()) { 
    16961750                $queries[$rs->f($field)] = '(C.cat_lft BETWEEN ' . $rs->cat_lft . ' AND ' . $rs->cat_rgt . ')'; 
    16971751            } 
     1752            unset($sql); 
    16981753        } 
    16991754 
     
    18811936 
    18821937        # Let's check if URL is taken... 
    1883         $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' . 
    1884         "WHERE post_url = '" . $this->con->escape($url) . "' " . 
    1885         'AND post_id <> ' . (integer) $post_id . ' ' . 
    1886         "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    1887             'ORDER BY post_url DESC'; 
    1888  
    1889         $rs = $this->con->select($strReq); 
     1938        $sql = new dcSelectStatement($this->core, 'coreGetPostURL'); 
     1939        $sql 
     1940            ->columns('post_url') 
     1941            ->from($this->prefix . 'post') 
     1942            ->where(array( 
     1943                'post_id <> ' . (integer) $post_id, 
     1944                'blog_id = ' . $sql->quote($this->id) 
     1945            )) 
     1946            ->cond('AND post_url = ' . $sql->quote($url)) 
     1947            ->order('post_url DESC'); 
     1948 
     1949        $rs = $this->con->select($sql->statement()); 
    18901950 
    18911951        if (!$rs->isEmpty()) { 
    1892             if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') { 
    1893                 $clause = "REGEXP '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'"; 
    1894             } elseif ($this->con->driver() == 'pgsql') { 
    1895                 $clause = "~ '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'"; 
    1896             } else { 
    1897                 $clause = "LIKE '" . 
    1898                 $this->con->escape(preg_replace(array('%', '_', '!'), array('!%', '!_', '!!'), $url)) . 
    1899                     "%' ESCAPE '!'"; 
    1900             } 
    1901             $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' . 
    1902             "WHERE post_url " . $clause . ' ' . 
    1903             'AND post_id <> ' . (integer) $post_id . ' ' . 
    1904             "AND blog_id = '" . $this->con->escape($this->id) . "' " . 
    1905                 'ORDER BY post_url DESC '; 
    1906  
    1907             $rs = $this->con->select($strReq); 
     1952            // Replace condition on post_url 
     1953            $sql->cond('AND post_url ' . $sql->regexp($url), true); 
     1954 
     1955            $rs = $this->con->select($sql->statement()); 
    19081956            $a  = array(); 
    19091957            while ($rs->fetch()) { 
     
    19632011    public function getComments($params = array(), $count_only = false) 
    19642012    { 
     2013        $sql = new dcSelectStatement($this->core, 'coreGetComments'); 
     2014 
    19652015        if ($count_only) { 
    1966             $strReq = 'SELECT count(comment_id) '; 
     2016            $sql->columns('count(comment_id)'); 
    19672017        } elseif (!empty($params['sql_only'])) { 
    1968             $strReq = 'SELECT P.post_id '; 
     2018            $sql->columns('P.post_id'); 
    19692019        } else { 
    1970             if (!empty($params['no_content'])) { 
    1971                 $content_req = ''; 
    1972             } else { 
    1973                 $content_req = 'comment_content, '; 
     2020            if (empty($params['no_content'])) { 
     2021                $sql->columns('comment_content'); 
    19742022            } 
    19752023 
    19762024            if (!empty($params['columns']) && is_array($params['columns'])) { 
    1977                 $content_req .= implode(', ', $params['columns']) . ', '; 
    1978             } 
    1979  
    1980             $strReq = 
    1981                 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, ' . 
    1982                 'comment_author, comment_email, comment_site, ' . 
    1983                 $content_req . ' comment_trackback, comment_status, ' . 
    1984                 'comment_spam_status, comment_spam_filter, comment_ip, ' . 
    1985                 'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, ' . 
    1986                 'P.post_dt, P.user_id, U.user_email, U.user_url '; 
    1987         } 
    1988  
    1989         $strReq .= 
    1990         'FROM ' . $this->prefix . 'comment C ' . 
    1991         'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id ' . 
    1992         'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id '; 
     2025                $sql->columns($params['columns']); 
     2026            } 
     2027 
     2028            $sql->columns(array('C.comment_id', 'comment_dt', 'comment_tz', 'comment_upddt', 'comment_author', 'comment_email', 'comment_site', 'comment_trackback', 'comment_status', 'comment_spam_status', 'comment_spam_filter', 'comment_ip', 'P.post_title', 'P.post_url', 'P.post_id', 'P.post_password', 'P.post_type', 'P.post_dt', 'P.user_id', 'U.user_email', 'U.user_url')); 
     2029        } 
     2030 
     2031        $sql 
     2032            ->from($this->prefix . 'comment C') 
     2033            ->join(array( 
     2034                'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id', 
     2035                'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id ' 
     2036            )); 
    19932037 
    19942038        if (!empty($params['from'])) { 
    1995             $strReq .= $params['from'] . ' '; 
    1996         } 
    1997  
    1998         $strReq .= 
    1999         "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' "; 
     2039            $sql->from($params['from']); 
     2040        } 
     2041 
     2042        $sql->where('P.blog_id = ' . $sql->quote($this->id)); 
    20002043 
    20012044        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2002             $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; 
     2045            $cond = '((comment_status = 1 AND P.post_status = 1'; 
    20032046 
    20042047            if ($this->without_password) { 
    2005                 $strReq .= 'AND post_password IS NULL '; 
    2006             } 
    2007             $strReq .= ') '; 
     2048                $cond .= ' AND post_password IS NULL'; 
     2049            } 
     2050            $cond .= ')'; 
    20082051 
    20092052            if ($this->core->auth->userID()) { 
    2010                 $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')"; 
    2011             } else { 
    2012                 $strReq .= ') '; 
    2013             } 
     2053                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID()); 
     2054            } 
     2055            $cond .= ')'; 
     2056            $sql->where($cond); 
    20142057        } 
    20152058 
    20162059        if (!empty($params['post_type'])) { 
    2017             $strReq .= 'AND post_type ' . $this->con->in($params['post_type']); 
     2060            $sql->where('post_type ' . $sql->in($params['post_type'])); 
    20182061        } 
    20192062 
    20202063        if (isset($params['post_id']) && $params['post_id'] !== '') { 
    2021             $strReq .= 'AND P.post_id = ' . (integer) $params['post_id'] . ' '; 
     2064            $sql->where('P.post_id = ' . (integer) $params['post_id']); 
    20222065        } 
    20232066 
    20242067        if (isset($params['cat_id']) && $params['cat_id'] !== '') { 
    2025             $strReq .= 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' '; 
     2068            $sql->where('P.cat_id = ' . (integer) $params['cat_id']); 
    20262069        } 
    20272070 
     
    20322075                $params['comment_id'] = array((integer) $params['comment_id']); 
    20332076            } 
    2034             $strReq .= 'AND comment_id ' . $this->con->in($params['comment_id']); 
     2077            $sql->where('comment_id ' . $sql->in($params['comment_id'])); 
    20352078        } 
    20362079 
    20372080        if (isset($params['comment_email'])) { 
    2038             $comment_email = $this->con->escape(str_replace('*', '%', $params['comment_email'])); 
    2039             $strReq .= "AND comment_email LIKE '" . $comment_email . "' "; 
     2081            $comment_email = $sql->quote(str_replace('*', '%', $params['comment_email'])); 
     2082            $sql->where('comment_email LIKE ' . $comment_email); 
    20402083        } 
    20412084 
    20422085        if (isset($params['comment_site'])) { 
    2043             $comment_site = $this->con->escape(str_replace('*', '%', $params['comment_site'])); 
    2044             $strReq .= "AND comment_site LIKE '" . $comment_site . "' "; 
     2086            $comment_site = $sql->quote(str_replace('*', '%', $params['comment_site'])); 
     2087            $sql->where('comment_site LIKE ' . $comment_site); 
    20452088        } 
    20462089 
    20472090        if (isset($params['comment_status'])) { 
    2048             $strReq .= 'AND comment_status = ' . (integer) $params['comment_status'] . ' '; 
     2091            $sql->where('comment_status = ' . (integer) $params['comment_status']); 
    20492092        } 
    20502093 
    20512094        if (!empty($params['comment_status_not'])) { 
    2052             $strReq .= 'AND comment_status <> ' . (integer) $params['comment_status_not'] . ' '; 
     2095            $sql->where('comment_status <> ' . (integer) $params['comment_status_not']); 
    20532096        } 
    20542097 
    20552098        if (isset($params['comment_trackback'])) { 
    2056             $strReq .= 'AND comment_trackback = ' . (integer) (boolean) $params['comment_trackback'] . ' '; 
     2099            $sql->where('comment_trackback = ' . (integer) (boolean) $params['comment_trackback']); 
    20572100        } 
    20582101 
    20592102        if (isset($params['comment_ip'])) { 
    2060             $comment_ip = $this->con->escape(str_replace('*', '%', $params['comment_ip'])); 
    2061             $strReq .= "AND comment_ip LIKE '" . $comment_ip . "' "; 
     2103            $comment_ip = $sql->quote(str_replace('*', '%', $params['comment_ip'])); 
     2104            $sql->where('comment_ip LIKE ' . $comment_ip); 
    20622105        } 
    20632106 
    20642107        if (isset($params['q_author'])) { 
    2065             $q_author = $this->con->escape(str_replace('*', '%', strtolower($params['q_author']))); 
    2066             $strReq .= "AND LOWER(comment_author) LIKE '" . $q_author . "' "; 
     2108            $q_author = $sql->quote(str_replace('*', '%', strtolower($params['q_author']))); 
     2109            $sql->where('LOWER(comment_author) LIKE ' . $q_author); 
    20672110        } 
    20682111 
     
    20782121                if ($words) { 
    20792122                    foreach ($words as $i => $w) { 
    2080                         $words[$i] = "comment_words LIKE '%" . $this->con->escape($w) . "%'"; 
     2123                        $words[$i] = "comment_words LIKE '%" . $sql->escape($w) . "%'"; 
    20812124                    } 
    2082                     $strReq .= 'AND ' . implode(' AND ', $words) . ' '; 
     2125                    $sql->where(implode(' AND ', $words)); 
    20832126                } 
    20842127            } 
     
    20862129 
    20872130        if (!empty($params['sql'])) { 
    2088             $strReq .= $params['sql'] . ' '; 
     2131            $sql->sql($params['sql']); 
    20892132        } 
    20902133 
    20912134        if (!$count_only) { 
    20922135            if (!empty($params['order'])) { 
    2093                 $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' '; 
     2136                $sql->order($sql->escape($params['order'])); 
    20942137            } else { 
    2095                 $strReq .= 'ORDER BY comment_dt DESC '; 
     2138                $sql->order('comment_dt DESC'); 
    20962139            } 
    20972140        } 
    20982141 
    20992142        if (!$count_only && !empty($params['limit'])) { 
    2100             $strReq .= $this->con->limit($params['limit']); 
    2101         } 
     2143            $sql->limit($params['limit']); 
     2144        } 
     2145 
     2146        $query = $sql->statement(); 
    21022147 
    21032148        if (!empty($params['sql_only'])) { 
    2104             return $strReq; 
    2105         } 
    2106  
    2107         $rs       = $this->con->select($strReq); 
     2149            return $query; 
     2150        } 
     2151 
     2152        $rs       = $this->con->select($query); 
    21082153        $rs->core = $this->core; 
    21092154        $rs->extend('rsExtComment'); 
     
    21282173        { 
    21292174            # Get ID 
    2130             $rs = $this->con->select( 
    2131                 'SELECT MAX(comment_id) ' . 
    2132                 'FROM ' . $this->prefix . 'comment ' 
    2133             ); 
     2175            $sql = new dcSelectStatement($this->core, 'coreCommentCreateGetID'); 
     2176            $sql 
     2177                ->columns('MAX(comment_id)') 
     2178                ->from($this->prefix . 'comment'); 
     2179            $rs = $this->con->select($sql->statement()); 
    21342180 
    21352181            $cur->comment_id    = (integer) $rs->f(0) + 1; 
     
    22082254        $this->core->callBehavior('coreBeforeCommentUpdate', $this, $cur, $rs); 
    22092255 
    2210         $cur->update('WHERE comment_id = ' . $id . ' '); 
     2256        $sql = new dcUpdateStatement($this->core, 'coreCommentUpdate'); 
     2257        $sql->where('comment_id = ' . $id); 
     2258 
     2259        $cur->update($sql->whereStatement()); 
    22112260 
    22122261        # --BEHAVIOR-- coreAfterCommentUpdate 
     
    22432292        $status = (integer) $status; 
    22442293 
    2245         $strReq = 
    2246         'UPDATE ' . $this->prefix . 'comment ' . 
    2247             'SET comment_status = ' . $status . ' '; 
    2248         $strReq .= 
    2249         'WHERE comment_id' . $this->con->in($co_ids) . 
    2250         'AND post_id in (SELECT tp.post_id ' . 
    2251         'FROM ' . $this->prefix . 'post tp ' . 
    2252         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
     2294        $sqlIn = new dcSelectStatement($this->core, 'coreUpdCommentsStatusIn'); 
     2295        $sqlIn 
     2296            ->columns('tp.post_id') 
     2297            ->from($this->prefix . 'post tp') 
     2298            ->where('tp.blog_id = ' . $sqlIn->quote($this->id)); 
    22532299        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2254             $strReq .= 
    2255             "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2256         } 
    2257         $strReq .= ')'; 
    2258         $this->con->execute($strReq); 
     2300            $sqlIn->where('user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2301        } 
     2302 
     2303        $sql = new dcUpdateStatement($this->core, 'coreUpdCommentsStatus'); 
     2304        $sql 
     2305            ->reference($this->prefix . 'comment') 
     2306            ->set('comment_status = ' . $status) 
     2307            ->where(array( 
     2308                'comment_id ' . $sql->in($co_ids), 
     2309                'post_id in (' . $sqlIn->statement() . ')' 
     2310            )); 
     2311 
     2312        $this->con->execute($sql->statement()); 
    22592313        $this->triggerComments($co_ids); 
    22602314        $this->triggerBlog(); 
     
    22902344        # Retrieve posts affected by comments edition 
    22912345        $affected_posts = array(); 
    2292         $strReq         = 
    2293         'SELECT post_id ' . 
    2294         'FROM ' . $this->prefix . 'comment ' . 
    2295         'WHERE comment_id' . $this->con->in($co_ids) . 
    2296             'GROUP BY post_id'; 
    2297  
    2298         $rs = $this->con->select($strReq); 
     2346 
     2347        $sql = new dcSelectStatement($this->core, 'coreDelCommentsScope'); 
     2348        $sql 
     2349            ->column('post_id') 
     2350            ->from($this->prefix . 'comment') 
     2351            ->where('comment_id' . $sql->in($co_ids)) 
     2352            ->group('post_id'); 
     2353 
     2354        $rs = $this->con->select($sql->statement()); 
     2355        unset($sql); 
    22992356 
    23002357        while ($rs->fetch()) { 
     
    23022359        } 
    23032360 
    2304         $strReq = 
    2305         'DELETE FROM ' . $this->prefix . 'comment ' . 
    2306         'WHERE comment_id' . $this->con->in($co_ids) . ' ' . 
    2307         'AND post_id in (SELECT tp.post_id ' . 
    2308         'FROM ' . $this->prefix . 'post tp ' . 
    2309         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
    2310         #If user can only delete, we need to check the post's owner 
     2361        $sqlIn = new dcSelectStatement($this->core, 'coreDelCommentsIn'); 
     2362        $sqlIn 
     2363            ->columns('tp.post_id') 
     2364            ->from($this->prefix . 'post tp') 
     2365            ->where(array('tp.blog_id = ' . $sqlIn->quote($this->id))); 
    23112366        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2312             $strReq .= 
    2313             "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2314         } 
    2315         $strReq .= ")"; 
    2316         $this->con->execute($strReq); 
     2367            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2368        } 
     2369 
     2370        $sql = new dcDeleteStatement($this->core, 'coreDelComments'); 
     2371        $sql 
     2372            ->from($this->prefix . 'comment') 
     2373            ->where(array( 
     2374                'comment_id ' . $sql->in($co_ids), 
     2375                'post_id in (' . $sqlIn->statement() . ')' 
     2376            )); 
     2377 
     2378        $this->con->execute($sql->statement()); 
    23172379        $this->triggerComments($co_ids, true, $affected_posts); 
    23182380        $this->triggerBlog(); 
     
    23252387        } 
    23262388 
    2327         $strReq = 
    2328         'DELETE FROM ' . $this->prefix . 'comment ' . 
    2329         'WHERE comment_status = -2 ' . 
    2330         'AND post_id in (SELECT tp.post_id ' . 
    2331         'FROM ' . $this->prefix . 'post tp ' . 
    2332         "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' "; 
    2333         #If user can only delete, we need to check the post's owner 
     2389        $sqlIn = new dcSelectStatement($this->core, 'coreDelJunkCommentsIn'); 
     2390        $sqlIn 
     2391            ->columns('tp.post_id') 
     2392            ->from($this->prefix . 'post tp') 
     2393            ->where('tp.blog_id = ' . $sqlIn->quote($this->id)); 
    23342394        if (!$this->core->auth->check('contentadmin', $this->id)) { 
    2335             $strReq .= 
    2336             "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' "; 
    2337         } 
    2338         $strReq .= ")"; 
    2339         $this->con->execute($strReq); 
     2395            // If user can only delete, we need to check the post's owner 
     2396            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID())); 
     2397        } 
     2398 
     2399        $sql = new dcDeleteStatement($this->core, 'coreDelJunkComments'); 
     2400        $sql 
     2401            ->from($this->prefix . 'comment') 
     2402            ->where(array( 
     2403                'comment_status = -2', 
     2404                'post_id in (' . $sqlIn->statement() . ')' 
     2405            )); 
     2406 
     2407        $this->con->execute($sql->statement()); 
    23402408        $this->triggerBlog(); 
    23412409    } 
Note: See TracChangeset for help on using the changeset viewer.

Sites map