<?php

class CASS_User_Profile_Extention
{
    public static function execute()
    {
        add_action('show_user_profile', 'cass_user_profile_extension_fields');
        add_action('edit_user_profile', 'cass_user_profile_extension_fields');

        function cass_user_profile_extension_fields($user)
        {
            global $wpdb;

            //Extensions fields
            $cassMemberNo = SantizeGlobal(get_user_meta($user->ID, 'cassMemberNo', true));
            $cassLevel = get_user_meta($user->ID, 'cassLevel', true);
            $cassLevelTechSki = get_user_meta($user->ID, 'cassLevelTechSki', true);
            $cassLevelClimbing = get_user_meta($user->ID, 'cassLevelClimbing', true);
            $cassLevelMountaineering = get_user_meta($user->ID, 'cassLevelMountaineering', true);
            $cassGroup = get_user_meta($user->ID, 'cassGroup', true);
            $cassTelephone = get_user_meta($user->ID, 'cassTelephone', true);
            $cassAddress =get_user_meta($user->ID, 'cassAddress', true);
            $cassNPA = get_user_meta($user->ID, 'cassNPA', true);
            $cassLocality = get_user_meta($user->ID, 'cassLocality', true);
            $cassMemberOfSion = get_user_meta($user->ID, 'cassMemberOfSion', true);

            ?>
            <h3>Club Alpin Suisse de Sion</h3>
            <table class="form-table">
                <tr>
                    <th><label for="cassMemberOfSion">Membre de Sion</label></th>
                    <td>
                        <input type="checkbox" name="cassMemberOfSion" id="cassMemberOfSion" value="1" <?php if ($cassMemberOfSion == 1) {
                            echo "checked";
                        } ?> />
                    </td>
                <tr>
                    <th><label for="cassMemberNo">N° de membre</label></th>
                    <td>
                        <input type="text" name="cassMemberNo" id="cassMemberNo" value="<?php echo esc_attr($cassMemberNo) ?>"
                            class="regular-text" />
                    </td>
                </tr>
                <tr>
                    <th><label for="cassLevel">Niveau physique</label></th>
                    <td>

                        <select name="cassLevel" id="cassLevel">
                            <option value="">Sélectionner un niveau physique</option>";
                            <?php
                            //Select all group
                            $rows = Level::selectAll();
                            foreach ($rows as $level) {
                                $selected = "";

                                if (intval($cassLevel) == intval($level->IDLevel)) {
                                    $selected = "selected";
                                }


                                echo "<option value=" . intval($level->IDLevel) . " " . $selected . ">" . ReadGlobal($level->name) . "</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th><label for="cassLevelTechSki">Niveau de ski</label></th>
                    <td>

                        <select name="cassLevelTechSki" id="cassLevelTechSki">
                            <option value="">Sélectionner un niveau de ski</option>";
                            <?php
                            //Select all group
                            $rows = Level::selectAll();
                            foreach ($rows as $level) {
                                $selected = "";

                                if (intval($cassLevelTechSki) == intval($level->IDLevel)) {
                                    $selected = "selected";
                                }


                                echo "<option value=" . intval($level->IDLevel) . " " . $selected . ">" . ReadGlobal($level->name) . "</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
            
                <tr>
                    <th><label for="cassLevelClimbing">Niveau d'escalade</label></th>
                    <td>

                        <select name="cassLevelClimbing" id="cassLevelClimbing">
                            <option value="">Sélectionner un niveau d'escalade</option>";
                            <?php
                            //Select all group
                            $rows = Level::selectAll();
                            foreach ($rows as $level) {
                                $selected = "";

                                if (intval($cassLevelClimbing) == intval($level->IDLevel)) {
                                    $selected = "selected";
                                }


                                echo "<option value=" . intval($level->IDLevel) . " " . $selected . ">" . ReadGlobal($level->name) . "</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th><label for="cassLevelMountaineering">Niveau d'alpinisme</label></th>
                    <td>

                        <select name="cassLevelMountaineering" id="cassLevelMountaineering">
                            <option value="">Sélectionner un niveau d'alpinisme</option>";
                            <?php
                            //Select all group
                            $rows = Level::selectAll();
                            foreach ($rows as $level) {
                                $selected = "";

                                if (intval($cassLevelMountaineering) == intval($level->IDLevel)) {
                                    $selected = "selected";
                                }


                                echo "<option value=" . intval($level->IDLevel) . " " . $selected . ">" . ReadGlobal($level->name) . "</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th><label for="cassMemberNo">Groupe</label></th>
                    <td>
                        <select name="cassGroup" id="cassGroup">
                            <option value="">Sélectionner un groupe</option>";
                            <?php
                            //Select all group
                            $sql = "SELECT IDGroup,name FROM cass_group ORDER BY name ASC;";
                            $rows = $wpdb->get_results($sql);
                            foreach ($rows as $group) {
                                $selected = "";

                                if (intval($cassGroup) == intval($group->IDGroup)) {
                                    $selected = "selected";
                                }


                                echo "<option value=" . intval($group->IDGroup) . " " . $selected . ">" . ReadGlobal($group->name). "</option>";
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th><label for="cassTelephone">N° de téléphone</label></th>
                    <td>
                        <input type="text" name="cassTelephone" id="cassTelephone"
                            value="<?php echo $cassTelephone ?>" />
                    </td>
                </tr>
                <tr>
                    <th><label for="cassAddress">Adresse</label></th>
                    <td>
                        <input type="text" name="cassAddress" id="cassAddress" value="<?php echo $cassAddress ?>"
                            class="regular-text" />
                    </td>
                </tr>
                <tr>
                    <th><label for="cassNPA">NPA</label></th>
                    <td>
                        <input type="text" name="cassNPA" id="cassNPA" value="<?php echo $cassNPA ?>"
                            class="regular-text" />
                    </td>
                </tr>
                <tr>
                    <th><label for="cassLocality">Localité</label></th>
                    <td>
                        <input type="text" name="cassLocality" id="cassLocality" value="<?php echo $cassLocality ?>"
                            class="regular-text" />
                    </td>
                </tr>
            </table>
            <?php
        }
        add_action('personal_options_update', 'cass_save_user_profile_extension_fields');
        add_action('edit_user_profile_update', 'cass_save_user_profile_extension_fields');

        function cass_save_user_profile_extension_fields($user_id)
        {

            if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-user_' . $user_id)) {
                return;
            }

            if (!current_user_can('edit_user', $user_id)) {
                return;
            }

            update_user_meta($user_id, 'cassMemberNo', SantizeGlobal($_POST['cassMemberNo']));
            update_user_meta($user_id, 'cassLevel', SantizeGlobal($_POST['cassLevel']));
            update_user_meta($user_id, 'cassLevelTechSki', SantizeGlobal($_POST['cassLevelTechSki']));
            update_user_meta($user_id, 'cassLevelClimbing', SantizeGlobal($_POST['cassLevelClimbing']));
            update_user_meta($user_id, 'cassLevelMountaineering', SantizeGlobal($_POST['cassLevelMountaineering']));
            update_user_meta($user_id, 'cassGroup', SantizeGlobal($_POST['cassGroup']));
            update_user_meta($user_id, 'cassTelephone', SantizeGlobal($_POST['cassTelephone']));
            update_user_meta($user_id, 'cassAddress', SantizeGlobal($_POST['cassAddress']));
            update_user_meta($user_id, 'cassNPA', SantizeGlobal($_POST['cassNPA']));
            update_user_meta($user_id, 'cassLocality', SantizeGlobal($_POST['cassLocality']));
            update_user_meta($user_id, 'cassMemberOfSion', $_POST['cassMemberOfSion']);
        }

        //No de membre
        add_filter('manage_users_columns', 'add_cassMemberNo');
        function add_cassMemberNo($columns)
        {
            $columns['cassMemberNo'] = 'N° du membre';
            return $columns;
        }


        add_action('manage_users_custom_column', 'add_cassMemberNo_column_content', 10, 3);
        function add_cassMemberNo_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassMemberNo' == $column_name) {
                return get_user_meta($user->ID, 'cassMemberNo', true);
            }
            return $value;
        }

        //Level default
        add_filter('manage_users_columns', 'add_cassLevel');
        function add_cassLevel($columns)
        {
            $columns['cassLevel'] = 'Niveau physique';
            return $columns;
        }

        add_action('manage_users_custom_column', 'add_cassLevel_column_content', 10, 3);
        function add_cassLevel_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassLevel' == $column_name) {
                $level = new Level();
                $level->loadFromId(get_user_meta($user->ID, 'cassLevel', true));

                return $level->getName();
            }
            return $value;
        }


        //Group
        add_filter('manage_users_columns', 'add_cassGroup');
        function add_cassGroup($columns)
        {
            $columns['cassGroup'] = 'cassGroup';
            return $columns;
        }

        add_action('manage_users_custom_column', 'add_cassGroup_column_content', 10, 3);
        function add_cassGroup_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassGroup' == $column_name) {
                $group = new Group();
                $group->loadFromId(intval(get_user_meta($user->ID, 'cassGroup', true)));
                return ReadGlobal($group->getName());
            }
            return $value;
        }

        //Telephone
        add_filter('manage_users_columns', 'add_cassTelephone');
        function add_cassTelephone($columns)
        {
            $columns['cassTelephone'] = 'Téléphone';
            return $columns;
        }


        add_action('manage_users_custom_column', 'add_cassTelephone_column_content', 10, 3);
        function add_cassTelephone_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassTelephone' == $column_name) {
                return get_user_meta(intval($user->ID), 'cassTelephone', true);
            }
            return $value;
        }

        //Address
        add_filter('manage_users_columns', 'add_cassAddress');
        function add_cassAddress($columns)
        {
            $columns['cassAddress'] = 'Adresse';
            return $columns;
        }


        add_action('manage_users_custom_column', 'add_cassAddress_column_content', 10, 3);
        function add_cassAddress_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassAddress' == $column_name) {
                return get_user_meta(intval($user->ID), 'cassAddress', true);
            }
            return $value;
        }

        //NPA
        add_filter('manage_users_columns', 'add_cassNPA');
        function add_cassNPA($columns)
        {
            $columns['cassNPA'] = 'NPA';
            return $columns;
        }


        add_action('manage_users_custom_column', 'add_cassNPA_column_content', 10, 3);
        function add_cassNPA_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassNPA' == $column_name) {
                return get_user_meta(intval($user->ID), 'cassNPA', true);
            }
            return $value;
        }

        //Locality
        add_filter('manage_users_columns', 'add_cassLocality');
        function add_cassLocality($columns)
        {
            $columns['cassLocality'] = 'Localité';
            return $columns;
        }

        //MemberOfSion
        add_filter('manage_users_columns', 'add_cassMemberOfSion');
        function add_cassMemberOfSion($columns)
        {
            $columns['cassMemberOfSion'] = 'Membre de Sion';
            return $columns;
        }

        add_action('manage_users_custom_column', 'add_cassMemberOfSion_column_content', 10, 3);
        function add_cassMemberOfSion_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassMemberOfSion' == $column_name) {
                if (get_user_meta(intval($user->ID), 'cassMemberOfSion', true) == 1) {
                    return "Oui";
                } else {
                    return "Non";
                }
            }
            return $value;
        }


        add_action('manage_users_custom_column', 'add_cassLocality_column_content', 10, 3);
        function add_cassLocality_column_content($value, $column_name, $user_id)
        {
            $user = get_userdata($user_id);
            if ('cassLocality' == $column_name) {
                return get_user_meta(intval($user->ID), 'cassLocality', true);
            }
            return $value;
        }

    }
}