<?php
/**
 * Restricted Categories Integration Admin Pages
 * 
 * This file contains function to handle Restricted Categories config logic in wp-admin 
 * and config form.
 *
 * @package     Adverts
 * @subpackage  RestrictedCategories
 * @copyright   Copyright (c) 2015, Marek Winiarski
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       1.0
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Add Restricted Categories checkbox into category form
 * 
 * @sinde 1.0
 * @return void
 */
function wpadverts_restricted_categories_category_form_fields($term) {
        
    if(is_object($term)) {   
        $is_restricted = get_term_meta( $term->term_id, "wpadvert_is_restricted", true );
    }
    
    ?>
        <?php wp_nonce_field( basename( __FILE__ ), 'wpadverts-restricted-categories-category-restriction' ); ?> 
        <label for="advert_restricted_categories"><?php _e("Restrict", "adverts-restricted-categories") ?>
            <input type="checkbox" name="wpadvert_is_restricted" id="advert_restricted_categories" value="1" <?php if(isset($is_restricted) && $is_restricted == 1): ?> checked="checked" <?php endif; ?> />
            <span class="description"><?php echo __("Do not allow users to post Ads in this category.", "adverts-restricted-categories"); ?></span>
        </label>
        <p></p>
    <?php
}

/**
 * Add Restricted Categories checkbox into category form
 * 
 * @sinde 1.0
 * @return void
 */
function wpadverts_restricted_categories_category_form_fields_edit($term) {
        
    if(is_object($term)) {   
        $is_restricted = get_term_meta( $term->term_id, "wpadvert_is_restricted", true );
    }

    ?>
        <?php wp_nonce_field( basename( __FILE__ ), 'wpadverts-restricted-categories-category-restriction' ); ?> 
        <tr class="form-field form-required term-name-wrap">
            <th scope="row"><label for="advert_restricted_categories"><?php _e("Restrict", "adverts-restricted-categories") ?></label></th>
            <td><input type="checkbox" name="wpadvert_is_restricted" id="advert_restricted_categories" value="1" <?php if($is_restricted == 1): ?> checked="checked" <?php endif; ?> />
            <span class="description"><?php echo __("Do not allow users to post Ads in this category.", "adverts-restricted-categories"); ?></span></td>
        </tr>
    <?php
   
}

/**
 * Save value of is_restricted field after category save
 * 
 * @param int $term_id - category id
 * @return void
 */
function wpadverts_restricted_categories_save_category($term_id) {
    
    if ( ! wp_verify_nonce( $_POST['wpadverts-restricted-categories-category-restriction'], basename( __FILE__ ) ) ) {
        return;
    }
    
    $old_value = get_term_meta($term_id, "wpadvert_is_restricted", true);
    $new_value = isset($_POST["wpadvert_is_restricted"]) ? $_POST["wpadvert_is_restricted"] : '';
    
    if($old_value && '' === $new_value) {
        delete_term_meta($term_id, "wpadvert_is_restricted");
    } else if($old_value != $new_value) {
        update_term_meta($term_id, "wpadvert_is_restricted", $new_value);
    }
}

/**
 * Create header for "is_restricted" column in backend adverts categories array
 * 
 * @param array $columns - all columns in array
 * @return array - columns after edition
 */
function wpadverts_restricted_categories_edit_restricted_columns( $columns ) {

    ?>
    
    <style type="text/css"> 
        .column-wpadverts_is_restricted {
            width: 20px;
        }
    </style>
    
    <?php
    
    $title = __( "Is Restricted?", "wpadverts-restricted-categories" );
    $columns['wpadverts_is_restricted'] = sprintf( '<span class="dashicons dashicons-lock" style="vertical-align:middle" title="%s"></span>', $title );

    return $columns;
}

/**
 * Sets values for "is_resctricted" column in backend adverts categories array
 * 
 * @param string $out - current value
 * @param string $column - coumln name
 * @param int $term_id - category id
 * @return string - value to write
 */
function wpadverts_restricted_categories_manage_advert_category_column( $out, $column, $term_id ) {

    if ( 'wpadverts_is_restricted' === $column ) {

        $is_restricted = get_term_meta($term_id, "wpadvert_is_restricted", true);
        
        if(empty($is_restricted)) {
            $out = "—";
        } else {
            $out = '<span class="dashicons dashicons-yes" style="vertical-align:middle; color:green"></span>';
        }
    }

    return $out;
}

/**
 * Feature disables the ability to choose restricted category in backend
 * 
 * @param string $name - category name
 * @param object $category - category object
 * @return filter
 */
function wpadverts_restricted_categories_admin_form($name, $category = "") {
    
    if(is_object($category)) {
        if(get_term_meta($category->term_id, "wpadvert_is_restricted", true) == 1) {
            ?>
                <script type="text/javascript">
                    jQuery("#in-advert_category-<?php echo $category->term_id ?>").parent().css("color", "#aaa");
                </script>
            <?php
        }
    }
    
    return apply_filters( 'wpadverts_restricted_categories_admin_form', $name, $category );
}