# PLUGIN PREVIEW BY TEXTPATTERN.INFO
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('aks_article')
->register('aks_article_count')
->register('aks_article_zebra')
->register('if_aks_article_active')
;
}
//--- Only-form-tags
function aks_article_zebra($atts){
global $thisarticle;
extract(lAtts(array(
'class' => '' // lazy-wrap return: class="$class$zebra"
),$atts));
return ( $class ) ? " class=\"{$class}{$thisarticle['aks_article_zebra']}\"" : $thisarticle['aks_article_zebra'];
}
function aks_article_count($atts){
global $thisarticle;
return $thisarticle['aks_article_count'];
}
function if_aks_article_active($atts, $thing = NULL){
global $thisarticle;
extract(lAtts(array(
'value' => '' // single tag attribute, return: $value
),$atts));
if( !$value ){ return EvalElse($thing, $thisarticle['aks_article_active']); }
return ( $thisarticle['aks_article_active'] ) ? $value : '';
}
//--- Main-tags
function aks_article($atts, $thing = NULL){
global $is_article_list; $old_ial = $is_article_list; $is_article_list = true;
article_push();
$out = @aks_article_doArticles($atts, EvalElse($thing, true));
article_pop(); $is_article_list = $old_ial;
if (!$out) $out=parse(EvalElse($thing, false));
return $out;
}
// ------------------------------------------------------------- internal subs
function aks_article_doArticles($atts, $thing = NULL){
global $prefs, $thisarticle, $variable;
$customFields = getCustomFields();
$customlAtts = array_null(array_flip($customFields));
$active_id = @intval($thisarticle['thisid']);
$theAtts = lAtts(array(
'form' => 'default',
'category' => '',
'category1' => '',
'category2' => '',
'where' => '', // WARNING $where not slashed!!! safety use it.
'section' => '',
'excerpted' => '',
'author' => '',
'sort' => '',
'month' => '',
'keywords' => '',
'expired' => $prefs['publish_expired_articles'],
'id' => '',
'exclude' => '',
'time' => 'past',
'status' => '4',
'allowoverride' => 0,
'offset' => 0,
'limit' => 10,
'search' => '',
'searchfield' => ( empty($prefs['searchable_article_fields']) ? 'Title, Body' : $prefs['searchable_article_fields']),
'zebra' => 0,
'zebra_next' => 0, //set initial count for zebra, used for second call aks_article
'carousel' => 0,
'hideself' => 0,
'column' => 1,
'columntag' => 'div',
'columnclass' => '',
'list' => '',
'listfield' => '', // WARNING $listfield not slashed!!! safety use it.
'wraptag' => '',
'wrapid' => '',
'class' => '',
'break' => '',
'breakclass' => '',
'label' => '',
'labeltag' => ''
)+$customlAtts,$atts);
extract($theAtts);
$zebra_next = intval($zebra_next);
$limit = assert_int($limit);
$hideself = ( $hideself ) ? 1 : 0;
$column = (assert_int($column)>1) ? assert_int($column) : 1;
$category = join("','", doSlash(aks_article_ulist($category)));
$category = (!$category) ? '' : " and (Category1 IN ('".$category."') or Category2 IN ('".$category."'))";
$category1 = join("','", doSlash(aks_article_ulist($category1))); if($category1){ $category.=" and Category1 IN ('".$category1."')"; }
$category2 = join("','", doSlash(aks_article_ulist($category2))); if($category2){ $category.=" and Category2 IN ('".$category2."')"; }
$where = (!$where) ? '' : " and ($where)";
$section = (!$section) ? '' : " and Section IN ('".join("','", doSlash(aks_article_ulist($section)))."')";
$excerpted = ($excerpted=='y') ? " and Excerpt !=''" : '';
$author = (!$author) ? '' : " and AuthorID IN ('".join("','", doSlash(aks_article_ulist($author)))."')";
$month = (!$month) ? '' : " and Posted like '".doSlash($month)."%'";
$ids = array_map('intval', aks_article_ulist($id));
$id = (empty($id)) ? '' : " AND ID IN (".join(',', $ids).")";
$excludes = array_map('intval', aks_article_ulist($exclude));
$exclude = (empty($exclude)) ? '' : " AND ID NOT IN (".join(',', $excludes).")";
if( $list ){ // list shorthand
if( preg_match('/^(all_)(.*)/i', $list, $mm) ){ $list = @$mm[2];
$time = "all";
}
if( preg_match('/^(ul_)(.*)/i', $list, $mm) ){ $list = @$mm[2];
$wraptag = "ul";
$break = "li";
}
}
// Using SQL caching capabilities. Txp 4.6+
if (function_exists('now')) {
$where_posted = now('posted');
$where_expired = " AND (".now('expires')." <= Expires OR Expires IS NULL)";
} else {
$where_posted = "now()";
$where_expired = " AND (now() <= Expires OR Expires = ".NULLDATETIME.")";
}
switch ($time) {
case 'any': $time = ""; break;
case 'all': $time = ""; $expired = 1; break;
case 'future': $time = " AND Posted > ".$where_posted; break;
default: $time = " AND Posted <= ".$where_posted;
}
if (!$expired) {
$time .= $where_expired;
}
$custom = '';
if ($customFields) {
foreach($customFields as $cField) {
if (isset($atts[$cField]))
$customPairs[$cField] = $atts[$cField];
}
if(!empty($customPairs)) {
$custom = buildCustomSql($customFields,$customPairs);
}
}
if ($keywords) {
$keys = doSlash(aks_article_ulist($keywords));
foreach ($keys as $key) {
$keyparts[] = "FIND_IN_SET('".$key."',Keywords)";
}
$keywords = " and (" . join(' or ',$keyparts) . ")";
}
$statuss = array_map('aks_article_getStatusNum', aks_article_ulist($status));
$statusq = (!$status) ? 'Status=4' : "Status IN (".join(',', array_unique($statuss)).")"; //fix later
$where = $statusq. $time. $where. $id. $category. $section. $exclude. $excerpted. $month. $author. $keywords. $custom;
if (!empty($atts['id']) && empty($atts['sort'])){
$safe_sort = 'field(id, '.join(',', $ids).')';
}else{
$safe_sort = ($sort) ? doSlash($sort) : 'Posted desc';
}
if( $carousel && $active_id){
$rs2=aks_article_safe_column_num("id", "textpattern", "$where order by $safe_sort"); if( empty($rs2) ){ return; }
$nm = array_keys($rs2, $active_id);
$max=count($rs2)-$hideself;
if( empty($nm[0]) || ( intval($nm[0])-floor(($limit+$hideself)/2) )<=0 ){
$offset = 0;
}else{
$offset = intval($nm[0])-floor(($limit+$hideself)/2);
if( $offset+$limit > $max ){ $offset = $max-$limit; }
if( $offset<0 ){ $offset=0; }
}
}
if( $hideself && $active_id){ $where .= " AND id<>$active_id "; }
$sf = "";
if( !empty($search) ){
$safe_sort = 'aks_article_scope desc';
$ff = '`'.join('`, `', do_list($searchfield)).'`';
$sf = ", match($ff) AGAINST ('".doSlash($search)."') as aks_article_scope";
// if you have problem with `having` then comment `having` line and uncomment next line
$where .= " having aks_article_scope>0 ";
// $where .= " and match($ff) AGAINST ('".doSlash($search)."')>0 ";
}
if( $list ){
switch ($list) {
case 'permlink': $thing = '<a href="<txp:permlink />"><txp:title /></a>'; break;
case 'permlink_nf': $thing = '<a href="<txp:permlink />" rel="nofollow"><txp:title /></a>'; break;
case 'permlink2': $thing = '<txp:permlink><txp:title /></txp:permlink>'; break;
}
$ff = array("ID", "Title", "url_title", "Section", "Category1", "Category2");
if( $listfield ){
$listfield = aks_article_ulist($listfield);
foreach( $listfield as $a){
if( in_array($a, $customFields) ){
$nm = array_keys($customFields, $a);
$ff[] = "custom_{$nm[0]}";
}elseif( $a ){
$ff[] = $a;
}
}
}
$fields = join(',', array_unique($ff) );
$rs = safe_rows_start($fields . $sf, "textpattern", "$where order by $safe_sort limit ".intval($offset).", $limit");
}else{
$rs = safe_rows_start("*, unix_timestamp(Posted) as uPosted, unix_timestamp(Expires) as uExpires, unix_timestamp(LastMod) as uLastMod" .$sf, "textpattern", "$where order by $safe_sort limit ".intval($offset).", $limit");
}
if ($rs) {
$count = 0;
$col=1;
$last = numRows($rs);
$percolumn = ceil($last/$column);
$articles = array();
$article_ids = array();
while($a = nextRow($rs)) {
unset($GLOBALS['thisarticle']);
populateArticleData($a);
global $thisarticle;
$thisarticle['aks_article_zebra'] = ($zebra > 1) ? ($count + $zebra_next) % $zebra : 0;
++$count;
if( $count > $percolumn*$col){ ++$col; }
$thisarticle['is_first'] = ($count == 1);
$thisarticle['is_last'] = ($count == $last);
$thisarticle['aks_article_active'] = ($active_id == $thisarticle['thisid'] );
$thisarticle['aks_article_count'] = $count;
$article_ids[] = $thisarticle['thisid'];
// this variable used for debug search feature inside article form
$variable['aks_article_scope'] = @$a['aks_article_scope'];
$variable['aks_article_count'] = $count;
if( $allowoverride and $a['override_form'] ){
$articles[$col][] = parse_form($a['override_form']);
}else{
$articles[$col][] = ($thing) ? parse($thing) : parse_form($form);
}
}
$variable['aks_article_count'] = $count;
$variable['aks_article_rest'] = $limit - $count;
$variable['aks_article_ids'] = join(',', $article_ids);
if ($zebra > 1) { $variable['aks_article_zebra_next'] = ($count + $zebra_next) % $zebra; }
if( $col==1 ){ return doLabel($label, $labeltag).doWrap($articles[1], $wraptag, $break, $class, $breakclass, '', '', $wrapid); }
$out=array();
for ($i = 1; $i <= $col; $i++) {
if( is_array($articles[$i]) ){
$out[]=doWrap($articles[$i], $wraptag, $break, $class, $breakclass, '', '', $wrapid);
}
}
return doWrap($out, '', $columntag, '', $columnclass);
}
}
// ------------------------------------------------------------- getStatusNum($name) - added numeric status support
function aks_article_getStatusNum($name){
$labels = array('draft' => 1, 'hidden' => 2, 'pending' => 3, 'live' => 4, 'sticky' => 5, '1'=>1, '2'=>2, '3'=>3, '4'=>4, '5'=>5);
$status = strtolower($name);
$num = empty($labels[$status]) ? 4 : $labels[$status];
return $num;
}
// -------------------------------------------------------------
function aks_article_ulist($list, $delim = ','){
return array_unique(array_map('trim', explode($delim, $list)));
}
// ------------------------------------------------------------- for compatibity with Txp < 4.5.0
function aks_article_safe_column_num($thing, $table, $where, $debug=''){
$q = "select $thing from ".safe_pfx_j($table)." where $where";
$rs = getRows($q,$debug);
if ($rs) {
foreach($rs as $a) {
$v = array_shift($a);
$out[] = $v;
}
return $out;
};
return array();
}