<?php
//controllers/backend/post/edit.php

function edit($f3){
    require('setting.php');
    require('models/posts/editdb.php');
    $items = editdb($f3, $setting['backendPostLimit']);

    $f3->mset([
        'appName'=>$setting['siteTitle'], 
        'pageTitle'=>'ទំព័រ​ការផ្សាយ', 
        'date'=>$setting['date'],
        'message'=>'ទិន្នន័យ​ការផ្សាយសរុប',
        'singleItem'=>$items[0],
        'item'=>$items[1],
        'route'=>'post',
        'edit'=>'edit'
    ]);

    echo View::instance()->render('views/backend/post.html');
}

 

<?php
//controllers/backend/post/editdb.php

function editdb($f3,$limit){
    $post = new DB\SQL\Mapper($f3->get('DB'),'posts');
    $post->load(['id=?',$f3->get('PARAMS.id')]);
    $posts = new DB\SQL\Mapper($f3->get('DB'),'posts');
    $posts->load([], ['order'=>'postDate DESC', 'limit'=>$limit]);

    return [$post, $posts];
}

 

 <!--views/backend/post.html-->

<script src="<?php echo $BASE ?>/public/scripts/backend/video.js"></script>
<script src="<?php echo $BASE ?>/public/scripts/ckeditor/ckeditor.js"></script>
<link href="<?php echo $BASE ?>/public/styles/backend/post.css" rel="stylesheet"></link>

<?php include('views/backend/partials/header.html') ?>

<section class='Main region'>
    <div class='sidebar'>
        <?php include('views/backend/partials/menu.html') ?>
    </div>

    <div class='content'>
        <?php if(isset($edit)){ ?>
        <form action='<?php echo $BASE ?>/backend/post' method='post'>
            <div class='editor'>
                <input type='text' name='title' required value='<?php echo $singleItem->title ?>' />
                <textarea name='content' id='editor'><?php echo $singleItem->content ?></textarea>
            </div>
            <div class='wrapper'>
                <select name='category' class='category'>
                    <option>ជាតិ</option>
                    <option>អន្តរជាតិ</option>
                    <option>ជំនួញ</option>
                </select>
                <script>$(".category").val("<?php echo $singleItem->category ?>").change();</script>
                <input type='text' name='thumb' value='<?php echo $singleItem->thumb ?>' required />
                <input type='datetime-local' name='date' value='<?php echo $singleItem->postDate ?>' required />
                <input type='hidden' name='video' value='<?php echo $singleItem->video ?>' />
                <input type='hidden' name='editID' value='<?php echo $singleItem->id ?>' />
                <input type='submit' value='ចុះ​ផ្សាយ' />
            </div>
        </form>
        <?php }else{ ?>
            <form action='<?php echo $BASE ?>/backend/post' method='post'>
                <div class='editor'>
                    <input type='text' name='title' required placeholder='ចំណងជើង' />
                    <textarea name='content' id='editor'></textarea>
                </div>
                <div class='wrapper'>
                    <select name='category'>
                        <option>ជាតិ</option>
                        <option>អន្តរជាតិ</option>
                        <option>ជំនួញ</option>
                    </select>
    
                    <input type='text' name='thumb' placeholder='តំណរភ្ជាប់​រូប' required />
                    <input type='datetime-local' name='date' required />
                    <input type='hidden' name='video' value='' />
                    <input type='submit' value='ចុះ​ផ្សាយ' />
                </div>
            </form>
            <?php } ?>
        <div class='video'>
            <select name='type'>
                <option>YouTube</option>
                <option>YTPlaylist</option>
                <option>Facebook</option>
            </select>
            <input type='text' name='id' required placeholder='ID' />

            <select name='ending'>
               <option>Yes</option>
               <option>No</option> 
            </select>

            <input type='submit' value='បញ្ចូល​វីដេអូ' onClick='genJson()' />
        </div>

        <table class='viddata'></table>

        <?php if(isset($edit)){ ?>
            <script>
                var entries = JSON.parse('<?php echo $singleItem->video ?>'.replace(/&quot;/g, '\"'))
            </script>
       <?php } ?>

        <script>
        if(entries.length > 0){

            let html = ``
            for(let v in entries){
                episode += 1
                html += `<tr>`
                html += `<td title="Delete" onClick="deleteRow(event)" class="episode">${episode}</td>`
                html += `<td class="td${episode}">${entries[v].type}</td>`
                html += `<td class="td${episode}">${entries[v].id}</td>`
                html += `<td class="td${episode}">${entries[v].ending}</td>`
                html += `</tr>`
            }

            if($('.viddata').html() === ''){
                $('.viddata').append('<tr>')
                $('.viddata').append('<th>ភាគ/លុប</th>')
                $('.viddata').append('<th>ប្រភេទ​</th>')
                $('.viddata').append('<th>អត្តសញ្ញាណ​</th>')
                $('.viddata').append('<th>ចប់ឬ​នៅ?</th>')
                $('.viddata').append('</tr>')
            }

            $('.viddata').append(`${html}`)

        }
        </script>

    </div>

    <script src="<?php echo $BASE ?>/public/scripts/ckeditor/config.js"></script>

</section>

<section class='region'>
    <?php include('views/backend/partials/listing.html') ?>
</section>

<?php include('views/backend/partials/footer.html') ?>

 

<?php
//routes/backend/post.php

$f3->route('GET /backend/post', function($f3){
    if($f3->get('SESSION.userID')){
        require('controllers/backend/post/get.php');
        get($f3);
    }else{
        $f3->reroute('/login');
    }
});

$f3->route('POST /backend/post', function($f3){
    if($f3->get('SESSION.userID')){
        require('controllers/backend/post/create.php');
        create($f3);
    }else{
        $f3->reroute('/login');
    }
});

$f3->route('GET /backend/post/edit/@id', function($f3){
    if($f3->get('SESSION.userID')){
        require('controllers/backend/post/edit.php');
        edit($f3);
    }else{
        $f3->reroute('/login');
    }
});

 

<?php 
//controllers/backend/post/create.php

function create($f3){
    if($f3->get('SESSION.role') != 'visitor'){
        require('models/posts/createdb.php');
        createdb($f3);
    }
    
    $f3->reroute('/backend/post');
}

 

<?php 
//models/post/createdb.php

function createdb($f3){
    if($f3->get('SESSION.role') != 'visitor'){
        $title = $f3->get('POST.title');
        $content = $f3->get('POST.content');
        $category = $f3->get('POST.category');
        $thumb = $f3->get('POST.thumb');
        $date = $f3->get('POST.date');
        $video = $f3->get('POST.video');
        $userID = $f3->get('SESSION.userID');
        $editID = $f3->get('POST.editID');
       
        $post = new DB\SQL\Mapper($f3->get('DB'),'posts');
        if($editID){
            $post->load(['id=?',$editID]);
        }else{
            $post->id = uniqid();
        }

        $post->title = $title;
        $post->content = $content;
        $post->category = $category;
        $post->thumb = $thumb;
        $post->postDate = $date;
        $post->video = $video;
        $post->userID = $userID;
        $post->save();
    }
}

 

https://github.com/Sokhavuth/PHP-REST-API

http://khmerweb.epizy.com/media/