<?php
includeContainer('_library_lib.php', 1);
setSessionLang();
$socInfo = getSocInfo();

$pageId = GETPOSTINT('blog');
$eventId = GETPOSTINT('id');

$showDonationLink = 0;
if (isModEnabled('don') && getDolGlobalString('DONATION_ENABLE_PUBLIC')) {
    $showDonationLink = 1;
}

$showEventGoalRaised = 0;
if (isModEnabled('don') && getDolGlobalString('DONATION_ENABLE_PUBLIC') && isModEnabled('eventorganization') && getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'SHOW_EVENT_GOAL_RAISED_AMOUNTS')) {
    $showEventGoalRaised = 1;
}

$showParticipateLink = 0;
if (isModEnabled('eventorganization') && getDolGlobalString('WS_'.strtoupper($website->ref).'_' . 'SHOW_EVENT_PARTICIPATE_LINK')) {
    $showParticipateLink = 1;
}

$link_participation = '';

if ($eventId) {
    $sql = "SELECT p.rowid, p.title, p.description, p.datec, p.dateo, p.datee, p.budget_amount, p.fk_statut 
        FROM ".MAIN_DB_PREFIX."projet as p 
        WHERE p.fk_statut IN (0,1) AND p.usage_organize_event IN (1) AND p.rowid = " . $eventId;
        
    $resql = $db->query($sql);
    if ($resql) {
        $obj = $db->fetch_object($resql);
        $encodedsecurekey = dol_hash(getDolGlobalString('EVENTORGANIZATION_SECUREKEY').'conferenceorbooth'.((int) $obj->rowid), 'md5');
        $link_participation = $socInfo['link_participation'] . '&id='.urlencode($obj->rowid).'&securekey='.urlencode($encodedsecurekey);
    }
}

?>
<div class="col-md-4">

	
	<div class="container-fluid causes sidebar-joinus">
        <div class="container">
            <div class="section-title">
              <div class="line left-line"></div>
              <h1><?php echo $weblangs->trans("joinUs"); ?></h1>
              <div class="line right-line"></div>
            </div>
            <div class="row g-4">
                <?php if ($showDonationLink) { ?>
                    <?php if ($eventId) { ?>
                        <a href="<?php echo $socInfo['link_donation'] . '?project_id='.urlencode($obj->rowid); ?>" target="new" class="btn flat-button text-white py-2 px-3"><i class="fas fa-donate"></i> <?php echo $weblangs->trans("donateNow"); ?> </a>
                    <?php } else { ?>
                        <a href="<?php echo $socInfo['link_donation']; ?>" target="new" class="btn flat-button text-white py-2 px-3"><i class="fas fa-donate"></i> <?php echo $weblangs->trans("donateNow"); ?> </a>
                    <?php } ?>
                    
                <?php } ?>
                <?php if ($link_participation && $showParticipateLink) { ?>
                    <a href="<?php echo $link_participation; ?>" target="new" class="btn flat-button text-white py-2 px-3"><i class="fas fa-user-plus"></i> <?php echo $weblangs->trans("Participate"); ?> </a>
                <?php } ?>
            </div>
        </div>
    </div>
	
	<div class="container-fluid causes">
        <div class="container py-5">
            <div class="section-title">
              <div class="line left-line"></div>
              <h1><?php echo $weblangs->trans("news"); ?></h1>
              <div class="line right-line"></div>
            </div>
            <div class="row g-4">
            <?php 
            $MAXNEWS = 2;
            $allblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', 0, 0, array('type_container'=>'blogpost', 'status'=>1, 'lang'=>'null, "" '.$_SESSION['lang']['shortcode']));
            $countofblogs = count($allblogs);
            $arrayofblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', $MAXNEWS, 0, array('type_container'=>'blogpost', 'status'=>1, 'lang'=>'null, "" '.$_SESSION['lang']['shortcode']));
            foreach($arrayofblogs as $blog)
            {
            ?>
                <div class="col-12-12">
                    <div class="causes-item">
                        <div class="causes-img">
                            <?php
                                $urlforimage = getImageFromHtmlContent($blog->content);
                                if (empty($urlforimage)) {
                                    $urlforimage = 'medias/image/'.$website->ref.'/noimage.jpg';
                                }
                            ?>
                            <img src="<?php echo $urlforimage; ?>" class="img-fluid w-100" style="height: 275px; object-fit: cover;" alt="Image">
                        </div>
                        <div class="progress progress-news">
                          <div class="progress-bar" role="progressbar" style="width: 100%;" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100">
                          </div>
                        </div>
                        <div class="causes-content p-4">
                            <span class="text-body">
                                <i class="fas fa-calendar-alt me-2"></i> <?php echo dol_print_date($blog->date_creation, 'daytext', 'tzuser', $weblangs); ?>
                                <span class="author-span"><i class="fas fa-user me-2"></i> <?php echo ($blog->author_alias ? $blog->author_alias : 'anonymous'); ?></span>
                            </span>
                            <h4 class="py-3"><?php echo $blog->title; ?></h4>
                            <hr>
                            <p class="mb-4 truncate3"><?php echo $blog->description; ?></p>
                            <a href="page.php?ref=<?php print $blog->pageurl; ?>&blog=<?php print $blog->id; ?>" class="btn flat-button text-white py-2 px-3 float-end"><?php echo $weblangs->trans("readMore"); ?></a>
                        </div>
                    </div>
                </div>
                <?php 
            }
            ?>
            </div>
                
        </div>
    </div>
    
    
    <div class="container-fluid causes">
        <div class="container">
            <div class="section-title">
              <div class="line left-line"></div>
              <h1><?php echo $weblangs->trans("actions"); ?></h1>
              <div class="line right-line"></div>
            </div>
            <div class="row g-4">
    
                <?php 
                $maxevents = 2;
                $countofevents = 0;
                $sqlCount = "SELECT count(p.rowid) as countofevents 
                        FROM ".MAIN_DB_PREFIX."projet as p 
                        WHERE p.fk_statut IN (0,1) AND p.usage_organize_event IN (1) 
                        ORDER BY p.datec DESC ";
                $resqlCount = $db->query($sqlCount);
                if ($resqlCount) {
                    $objCount = $db->fetch_object($resqlCount);
                    $countofevents = $objCount->countofevents ? $objCount->countofevents : 0;
                }
            
                $sql = "SELECT p.rowid, p.title, p.description, p.datec, p.dateo, p.datee, p.budget_amount, p.fk_statut 
                        FROM ".MAIN_DB_PREFIX."projet as p 
                        WHERE p.fk_statut IN (0,1) AND p.usage_organize_event IN (1) 
                        ORDER BY p.datec DESC 
                        LIMIT ".$maxevents;
                $resql = $db->query($sql);
                if ($resql) {
                    $countofblogs = $db->num_rows($resql);
                    while ($obj = $db->fetch_object($resql)) {
                        ?>
                        <div class="col-xs-12">
                            <div class="causes-item">
                                <div class="causes-img">
                                    <?php
                                        $urlforimage = getImageFromHtmlContent($obj->description ? $obj->description : "");
                                        if (empty($urlforimage)) {
                                            $urlforimage = 'medias/image/'.$website->ref.'/noimage.jpg';
                                        }
                                    ?>
                                    <img src="<?php echo $urlforimage; ?>" class="img-fluid w-100" style="height: 275px; object-fit: cover;" alt="Image">
                                    <div class="causes-link pb-2 px-3">
                                        <?php
                                            $progress = 100;
                                            if ($obj->budget_amount > 0 && $showEventGoalRaised) {
                                                $sqlCollected = "SELECT SUM(amount) as total_collected 
                                                                 FROM ".MAIN_DB_PREFIX."don 
                                                                 WHERE fk_projet = ".$obj->rowid;
                                                $resqlCollected = $db->query($sqlCollected);
                                                $collected = 0;
                                                if ($resqlCollected) {
                                                    $objCollected = $db->fetch_object($resqlCollected);
                                                    $collected = $objCollected->total_collected ? $objCollected->total_collected : 0;
                                                }
                                                $progress = round(($collected / $obj->budget_amount) * 100, 2);
                                            }
                                        ?>
                                        
                                        <?php if ($showEventGoalRaised) { ?>
                                            <small class="text-white"><i class="fas fa-chart-bar secondary-color me-2"></i><?php echo $weblangs->trans("goal"); ?>: <?php print price(price2num($obj->budget_amount, 'MT'), 1, '', 1, -1); ?> €</small>
                                            <small class="text-white"><i class="fa fa-thumbs-up secondary-color me-2"></i><?php echo $weblangs->trans("raised"); ?>: <?php print price(price2num($collected, 'MT'), 1, '', 1, -1); ?> €</small>
                                        <?php } ?>  
                                    </div>
                                    <div class="causes-dination p-2">
                                        <?php if ($showDonationLink) { ?>
                                            <?php if (getDolGlobalInt('WS_' . strtoupper($website->ref) . '_PAYMENT_IN_FRAME')) { ?>
                                                <a href="page.php?donation=<?php echo $obj->rowid; ?>" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("donateNow"); ?></a>
                                            <?php } else { ?>
                                                <a href="<?php echo $socInfo['link_donation'] . '?project_id='.urlencode($obj->rowid); ?>" target="new" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("donateNow"); ?></a>
                                            <?php } ?>
                                        <?php } ?>
                                        
                                        <?php if ($showParticipateLink) { ?>
                                            <?php $encodedsecurekey = dol_hash(getDolGlobalString('EVENTORGANIZATION_SECUREKEY').'conferenceorbooth'.((int) $obj->rowid), 'md5'); ?>
                                            <a href="<?php echo $socInfo['link_participation'] . '&id='.urlencode($obj->rowid).'&securekey='.urlencode($encodedsecurekey); ?>" target="new" class="btn flat-button text-white py-2 px-3"><?php echo $weblangs->trans("Participate"); ?></a>
                                        <?php } ?>

                                    </div>
                                </div>
                                
                                <div class="progress">
                                  <div class="progress-bar" role="progressbar" style="width: <?php echo $progress; ?>%;" aria-valuenow="<?php echo $progress; ?>" aria-valuemin="0" aria-valuemax="100">
                                    <?php if ($showEventGoalRaised) { ?>
                                    <span><?php echo $progress; ?>%</span>
                                    <?php } ?>
                                  </div>
                                </div>
                                <div class="causes-content p-4">
                                    <span class="text-body">
                                        <i class="fas fa-calendar-alt me-2"></i> <?php echo dol_print_date($obj->datec, 'daytext', 'tzuser', $weblangs); ?>
                                    </span>
                                    <h4 class="py-3"><?php echo $obj->title; ?></h4>
    
                                    <a href="page.php?id=<?php echo $obj->rowid; ?>" class="btn flat-button text-white py-2 px-3 float-end"><?php echo $weblangs->trans("readMore"); ?></a>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                }
                ?>
            </div>

        </div>
    </div>
	
</div>