<?php
    /**
     * @author Webfairy MediaT CMS - www.Webfairy.net
     */
    include dirname(__FILE__).'/lib/engine.php';
        $Webfairy = new Webfairy();
            $Webfairy->start();

    if ($Webfairy->isNumber($_GET['id']) == true) {
        $w = array(
        'id' => (int) $_GET['id'],
      );
    } else {
        $w = array(
        'name' => $Webfairy->plaintext($_GET['id']),
      );
    }

    $db_row = $Webfairy->db->posts($w);

    if (($post = $Webfairy->post_db_row($db_row, array('all' => true, 'related' => true))) == false) {
        $Webfairy->redirect(
            tr('err_not_found'),
            $Webfairy->url('index.html')
        );
    }

    $db_row->fetch();

    $db_row->update(
        array(
            'views' => new NotORM_Literal("views+1"),
            'lastview' => new NotORM_Literal("CURRENT_TIMESTAMP()"),
        )
    );

    if ($post['count_comments'] == true) {
        if ($count = $Webfairy->count_fb_comments($post['absolute_url'])) {
            $db_row->update(array(
                'comments' => (int) $count,
                'comments_update' => new NotORM_Literal("UNIX_TIMESTAMP()"),
            ));
        }
    }

    if ($post['require_player'] == true) {
        $Webfairy
            ->cssFile('player/mediaelementplayer.min.css', 'header')
            ->jsFile('player/mediaelement-and-player.min.js', 'footer')
            ->html(
                $Webfairy->javascript_code("$('audio,video').mediaelementplayer();"), 'footer'
            );
    }

    if ($post['type'] == 2) {
        $Webfairy
            ->cssFile('css/prism.css', 'header')
            ->jsFile('js/prism.js', 'footer');
    }

    $Webfairy->smarty->assign('post', $post);

    $Webfairy->html($Webfairy->facebook_sdk(), 'footer');

    $description = $Webfairy->OneLine($Webfairy->plaintext($post['title'], false));

    $property_meta = array(
        'og:url' => $Webfairy->absolute_url($post['url']),
        'og:description' => $description
    );
    
    if(empty($post['thumb']) == false){
        $property_meta['og:image'] = $Webfairy->absolute_url($Webfairy->image($post['thumb'],'medium'));
    }

    $options = array(
        'description' => $description,
        'keywords' => $Webfairy->keywords($post['title']),
        'property_meta' => $property_meta,
        'canonical' => $Webfairy->absolute_url($post['url']),
    );

    $Webfairy->display('view.tpl', array($post['title'], $post['cat']['title']), $options);
