<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

global $mosConfig_offset, $mosConfig_live_site, $mainframe, $mosConfig_cachepath, $mosConfig_cachetime;

$_cache 		= $params->get('cache', 1);
$_cache_time		= $params->get('cache_time', 3600);

$count = intval( $params->get( 'count', 5 ) );
$catid = trim( $params->get( 'catid' ) );
$secid = trim( $params->get( 'secid' ) );
$show_front = $params->get( 'show_front', 1 );
$moduleclass_sfx = $params->get( 'moduleclass_sfx' );
$imageWidth = intval($params->get('imageWidth', 50)) ;

$class_title = trim( $params->get( 'class_title', 'contentheading' ) );

$vremya = $params->def('vremya', '1') ;
$class_date = trim( $params->get( 'class_date', 'createdate' ) );

$avtor = $params->def('avtor', '1') ;
$class_avtor = trim( $params->get( 'class_avtor', 'createdate' ) );

$sep = trim( $params->get( 'sep', '&nbsp;||&nbsp;' ) );
$class_sep = trim( $params->get( 'class_sep', 'createdate' ) );

$pik = $params->def('pik', '1') ;

$noimage = trim( $params->get( 'noimage' ) );
$listimg = trim( $params->get( 'listimg' ) );
$listimg_yes = $params->def('listimg_yes', '0') ;

$show_intro = $params->get( 'show_intro');
$introtext_limit = intval( $params->get( 'introtext_limit') );

$readmore = $params->get( 'readmore' );
$podrobnee = trim( $params->get( 'podrobnee', 'Read more...' ) );
$class_readmore = trim( $params->get( 'class_sep', 'createdate' ) );

$all_in = $params->def('all_in', '0') ;
$text_all_in = trim( $params->get( 'text_all_in', 'All in section/category...' ) );
$link_all_in = trim( $params->get( 'link_all_in', 'content/blogsection/1/1/' ) );
$class_all_in = trim( $params->get( 'class_all_in', 'small' ) );

$now = date( 'Y-m-d H:i:s', time() );

$query = "SELECT a.id, a.title, a.images, a.created, u.name, a.created_by_alias, a.created_by, a.introtext"
. "\n FROM #__content AS a"
. "\n LEFT JOIN #__users AS u ON u.id = a.created_by"
. "\n LEFT JOIN #__content_frontpage AS f ON f.content_id = a.id"
. "\n WHERE ( a.state = '1' AND a.checked_out = '0' AND a.sectionid > '0' )"
. "\n AND ( a.publish_up = '0000-00-00 00:00:00' OR a.publish_up <= '". $now ."' )"
. "\n AND ( a.publish_down = '0000-00-00 00:00:00' OR a.publish_down >= '". $now ."' )"
. ( $catid ? "\n AND ( a.catid IN (". $catid .") )" : '' )
. ( $secid ? "\n AND ( a.sectionid IN (". $secid .") )" : '' )
. ( $show_front == "0" ? "\n AND f.content_id IS NULL" : '' )
. "\n ORDER BY a.created DESC LIMIT $count"
;
$database->setQuery( $query );
$rows = $database->loadObjectList();

$bs = $mainframe->getBlogSectionCount();
$bc = $mainframe->getBlogCategoryCount();
$gbs = $mainframe->getGlobalBlogSectionCount();

//Открываем общюю таблицу
echo '<!-- DGM News Ultra module created by Denys Nosov [Joomla! Ukraine - http://www.joomla-ua.org], 2006 -->
<!-- Start module -->
<table cellspacing="0" class="dgmnews'.$moduleclass_sfx.'"><tr>';

//Завихрения материалов из БД
foreach ( $rows as $row ) {
    $Itemid = $mainframe->getItemid( $row->id, 0, 0, $bs, $bc, $gbs );

    if ( $row->created_by_alias ) {
			$author = $row->created_by_alias;
		} else {
			$author = htmlspecialchars( $row->name );
		}

     //Чистим тэги, а так же убираем вставки для разных ботов
     $row->introtext = str_replace('&nbsp;','', $row->introtext);
     $row->introtext = str_replace('<h1>','', $row->introtext);
     $row->introtext = str_replace('</h1>','', $row->introtext);
     $row->introtext = str_replace('<h2>','', $row->introtext);
     $row->introtext = str_replace('</h2>','', $row->introtext);
     $row->introtext = str_replace('<br />','', $row->introtext);
     $row->introtext = str_replace('<BR />','', $row->introtext);
     $row->introtext = str_replace('<br>','', $row->introtext);
     $row->introtext = str_replace('<BR>','', $row->introtext);
     $row->introtext = str_replace('<p>','', $row->introtext);

     $row->introtext = preg_replace('/{([a-zA-Z0-9\-_]*)\s*(.*?)}/i', '', $row->introtext);

    //укорачиваем интротекст - количество слов
	if ($introtext_limit) {
		$word_arr = str_word_count($row->introtext, 2);
		$c = 0;
		foreach ($word_arr AS $k => $v) {
			if ($c == $introtext_limit) {
				$row->introtext = substr($row->introtext, 0, $k).'...';
				break;
			}
			$c++;
		}
	}

    $row->id=intval($row->id);

    if ($Itemid == NULL) {
        $Itemid = '';
    }
    else {
        $Itemid = '&amp;Itemid='. $Itemid;
    }

    if (!empty ($row->images))
    {
        $width = ($imageWidth > 0) ? ' width="'.$imageWidth.'"' : '' ;
        $image = explode("|", $row->images) ;

    //Пошла первая ячейка с картинкой

    //Включаем картинку:
    if ($pik) {
        echo '<td '.$width.' class="dgmnews'.$moduleclass_sfx.'">
            <a href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">
            <img src="'.$mosConfig_live_site.'/images/stories/'.$image[0].'" alt="'. $row->title .'"  title="'. $row->title .'" align="left" border="0"'.$width.'/>
            </a>
            </td>';
            }
    else { echo '';	}

    //Пошла вторая ячейка с контентом
    echo'<td class="dgmnews'.$moduleclass_sfx.'">';
        //Выводим заголовок
        echo '<a class="'.$class_title.'" href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">
        '. $row->title .'
        </a><br />';

        //Отображаем время:
        if ($vremya==1) {
            echo '<span class="'.$class_date.'">';
            echo $vremya = mosFormatDate ($row->created, $params->get( 'date_format' ));
            echo '</span>';
            // Встака розделителя
            echo '<span class="'.$class_sep.'">'.$sep.'</span>';
        }
        else {
        //Вместо времени картинка-стрелочка
        if ($listimg_yes==1) { echo '<img src="'.$listimg.'" border="0">';}
        };

        //Включаем алиас автора
        if ($avtor==1) {
        echo '<span class="'.$class_avtor.'">';
        echo $author;
        echo '</span><br />';
        }
        else {
        echo '';
        }

        //Выводим интротекст
        if ($show_intro==1) {
        echo $row->introtext; }

        //Выводим читать дальше...
        if ($readmore==1) { echo '<br /><a class="'.$class_readmore.'" href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">'.$podrobnee.'</a>';
        }
        echo '</td></tr>';
    }

    //===============================================

    //если в контенте нет картинки, ну бывает такое :)
    else  {
        if ($pik) {
            echo '<td '.$width.' class="dgmnews'.$moduleclass_sfx.'">
                <a href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">
                <img src="'.$mosConfig_live_site.'/'.$noimage.'" alt="'. $row->title .'"  title="'. $row->title .'" align="left" border="0"'.$width.'/>
                </a>
                </td>';
            echo '';
        }
        else {
            echo '';
        	}
            echo'<td class="dgmnews'.$moduleclass_sfx.'">';

            //Выводим заголовок
            echo '<a class="'.$class_title.'" href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">'. $row->title .'</a><br />';

            //Отображаем время:
            if ($vremya) {echo '<span class="'.$class_date.'">';
                echo $vremya = mosFormatDate ($row->created, $params->get( 'date_format' ));
                echo '</span>';
                echo '<span class="'.$class_sep.'">'.$sep.'</span>';
            }
            else {
                if ($listimg_yes==1) { echo '<img src="'.$listimg.'" border="0">';}
            };

            //Включаем алиас автора
            if ($avtor==1) {
                echo '<span class="'.$class_avtor.'">';
                echo $author;
                echo '</span><br />';
            }
            else {
                echo '';
            }

            //Выводим интротекст
            if ($show_intro==1) {
                echo $row->introtext; }

            //Выводим читать дальше
            if ($readmore==1) { echo '<br /><a class="'.$class_readmore.'" href="'. sefRelToAbs( 'index.php?option=com_content&amp;task=view&amp;id='. $row->id . $Itemid ) .'">'.$podrobnee.'</a>';}
            }
                echo '</td></tr>';
            }
    echo '</td></tr></table>';

    //Выводим ссылку на роздел/категорию
    if ($all_in==1) { echo '<a class="'.$class_all_in.'" href="'.$link_all_in.'">'.$text_all_in.'</a><br />';}
    echo '<!-- End module -->';
?>
