<?php
/**
 * Created by PhpStorm.
 * User: imyuvii
 * Date: 30/09/16
 * Time: 12:52 PM
 */

/**
 * Initializing plugin data
 */
function webtangl_ejg_activate() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'options';
    $optionsArray = (array)get_option('webtangl_ejg_options');
    $row_height = isset($optionsArray['row_height'])?$optionsArray['row_height']:250;
    $margin = isset($optionsArray['margin'])?$optionsArray['margin']:10;
    $caption = isset($optionsArray['caption']) && ($optionsArray['caption']=='yes')?1:0;
    $target = isset($optionsArray['target'])?$optionsArray['target']:'Popup';
    $gallery_ids = isset($optionsArray['gallery-ids']) ? json_decode($optionsArray['gallery-ids']) : [];
    $newGalleryIds = '';
    if( count($gallery_ids) > 0 ) {
        $newGalleryIds = array();
        foreach ($gallery_ids as $gallery) {
            array_push($newGalleryIds, $gallery->id);
        }
        $newGalleryIds = implode(",", $newGalleryIds);
    }
    $value = 'a:8:{s:16:"8253b4469b36fc35";s:0:"";s:16:"fdd0b6858328ea98";s:0:"";s:10:"row_height";s:'.strlen($row_height).':"'.$row_height.'";s:6:"margin";s:'.strlen($margin).':"'.$margin.'";s:7:"caption";s:1:"'.$caption.'";s:6:"target";s:'.strlen($target).':"'.$target.'";s:11:"gallery-ids";s:'.strlen($newGalleryIds).':"'.$newGalleryIds.'";s:16:"b44dad46bb647e75";s:0:"";}';
    $wpdb->insert(
        $table_name,
        array(
            'option_name' => 'easy-justified-gallery_options',
            'option_value' => 's:'.strlen($value).':"'.$value.'";',
            'autoload' => 'yes',
        )
    );

    delete_option('webtangl_ejg_options');
}

/**
 * Admin options
 */
function webtangl_ejg_admin_options() {
    $titan = TitanFramework::getInstance( 'easy-justified-gallery' );

    $options = $titan->createAdminPanel( array(
        'name' => 'Easy Gallery',
        'id' => 'easy-justified-gallery',
        'parent' => 'options-general.php',
    ) );

    $options->createOption( array(
        'name' => 'Gallery Settings',
        'type' => 'heading',
    ));

    $options->createOption( array(
        'type' => 'note',
        'desc' => 'Select images and configure options in below form. Add <strong>[easyGallery]</strong> ShortCode to your page or post.'
    ) );

    $options->createOption( array(
        'name' => 'Image Height',
        'id' => 'row_height',
        'type' => 'text',
        'desc' => "This will define height of each image in gallery, Width will be adjusted by keeping aspect ration in mind"
    ));

    $options->createOption( array(
        'name' => 'Space between images (PX)',
        'id' => 'margin',
        'type' => 'text',
        'desc' => 'Margin between images',
    ));

    $options->createOption( array(
        'name' => 'Display Image Caption?',
        'id' => 'caption',
        'type' => 'enable',
        'default' => true,
    ));

    $options->createOption( array(
        'name' => 'Open image in',
        'id' => 'target',
        'type' => 'select',
        'options' => array(
            'Popup'         => 'Popup',
            'New Tab'       => 'New Tab',
            'Same Window'   => 'Same Window'
        ),
        'default' => 'Popup'
    ));

    $options->createOption( array(
        'name' => 'Images',
        'id' => 'gallery-ids',
        'type' => 'gallery',
    ));

    $options->createOption( array(
        'type' => 'note',
        'desc' => '<b>Note:</b> If you lose your settings after updating from <u>1.0.3</u> or below version, simply deactivate plugin and than activate plugin again to get your settings back. sorry for inconvenience'
    ) );

    $options->createOption( array(
        'type' => 'save',
    ));
}



