<?php
/**
 *  This helper file contains all LTcms short version helpers that can be used throughout the CMS
 *  You can use them where appropriate in your system
 * 
 */

// Create a helper function
 function getVarOrNull($key) {
    $value = $this->request->getVar($key);
    return !empty($value) ? $value : null;
}

//Ordering of Post Blocks
function shiftOrdering($postId, $newOrder, $blockId)
{
    // Get all blocks ordered by ordering
    $blocks = $this->where('post_id', $postId)
                   ->where('id !=', $blockId)
                   ->orderBy('ordering', 'ASC')
                   ->findAll();

    $order = 1;
    foreach ($blocks as $b) {
        if ($order == $newOrder) {
            $order++; // leave space for updated block
        }
        $this->update($b['id'], ['ordering' => $order]);
        $order++;
    }
}
