Пример #1
0
    /**
     * Return a list  of users
     * @param array $customer_list_params
     * @param integer $selected_user
     * @param boolean $multiple
     * @param boolean $disabled
     * @return string
     */
    function custom_user_list($customer_list_params = array('name' => 'user[customer_id]', 'id' => 'user_customer_id'), $selected_user = "", $multiple = false, $disabled = false)
    {
        $content_output = '';
        // USERS
        $wps_customer_mdl = new wps_customer_mdl();
        $users = $wps_customer_mdl->getUserList();
        $select_users = '';
        if (!empty($users)) {
            foreach ($users as $user) {
                if ($user->ID != 1) {
                    $lastname = get_user_meta($user->ID, 'last_name', true);
                    $firstname = get_user_meta($user->ID, 'first_name', true);
                    $select_users .= '<option value="' . $user->ID . '"' . (!$multiple && $selected_user == $user->ID ? ' selected="selected"' : '') . ' >' . $lastname . ' ' . $firstname . ' (' . $user->user_email . ')</option>';
                }
            }
            $content_output = '
			<select name="' . $customer_list_params['name'] . '" id="' . $customer_list_params['id'] . '" data-placeholder="' . __('Choose a customer', 'wpshop') . '" class="chosen_select"' . ($multiple ? ' multiple="multiple" ' : '') . '' . ($disabled ? ' disabled="disabled" ' : '') . '>
				<option value="0" ></option>
				' . $select_users . '
			</select>';
        }
        return $content_output;
    }
    /**
     * Affiche la page des groupes
     */
    public static function display_page()
    {
        self::manage_post();
        ob_start();
        wpshop_display::displayPageHeader(__('Groups', 'wpshop'), '', __('Groups', 'wpshop'), __('Groups', 'wpshop'), true, 'admin.php?page=' . WPSHOP_NEWTYPE_IDENTIFIER_GROUP . '&action=add', '');
        $content = ob_get_contents();
        ob_end_clean();
        $wps_customer_mdl = new wps_customer_mdl();
        // Si on re�oit une action
        if (!empty($_GET['action'])) {
            $readonly_name_field = '';
            switch ($_GET['action']) {
                case 'delete':
                    if (!empty($_GET['code'])) {
                        $roles = get_option('wp_user_roles', array());
                        if (isset($roles[$_GET['code']]) && $_GET['code'] != 'customer' && $_GET['code'] != 'wpshop_customer') {
                            unset($roles[$_GET['code']]);
                            $this->unaffectUsersToGroup($_GET['code']);
                            update_option('wp_user_roles', $roles);
                        }
                    }
                    wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . WPSHOP_NEWTYPE_IDENTIFIER_GROUP));
                    break;
                case 'edit':
                    $readonly_name_field = 'readonly';
                    if (!empty($_GET['code'])) {
                        $role = $this->getRole($_GET['code']);
                        if (!empty($role)) {
                            $group_name = $role['name'];
                            $group_description = $role['description'];
                            $group_parent = $role['parent'];
                            $submit_button_value = __('Edit the group', 'wpshop');
                            $submit_button_name = 'editrole';
                            // ROLES
                            $roles = get_option('wp_user_roles', array());
                            $select_parent = '<option value="">--</option>';
                            foreach ($roles as $code => $role) {
                                if ($code != $_GET['code']) {
                                    $selected = $group_parent == $code ? 'selected' : '';
                                    $select_parent .= '<option value="' . $code . '" ' . $selected . '>' . $role['name'] . '</option>';
                                }
                            }
                            // USERS
                            $users = $wps_customer_mdl->getUserList();
                            if (!empty($users)) {
                                $select_users = '';
                                foreach ($users as $user) {
                                    if ($user->ID != 1) {
                                        $u = new WP_User($user->ID);
                                        $selected = isset($u->roles[0]) && $u->roles[0] == $_GET['code'] ? 'selected' : '';
                                        $select_users .= '<option value="' . $user->ID . '" ' . $selected . '>' . $user->user_login . '</option>';
                                    }
                                }
                            }
                        } else {
                            wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . WPSHOP_NEWTYPE_IDENTIFIER_GROUP));
                            exit;
                        }
                    } else {
                        wpshop_tools::wpshop_safe_redirect(admin_url('admin.php?page=' . WPSHOP_NEWTYPE_IDENTIFIER_GROUP));
                        exit;
                    }
                    break;
                case 'add':
                    $group_name = $group_description = '';
                    $submit_button_value = __('Create the group', 'wpshop');
                    $submit_button_name = 'addrole';
                    // ROLES
                    $roles = get_option('wp_user_roles', array());
                    $select_parent = '<option value="">--</option>';
                    foreach ($roles as $code => $role) {
                        $select_parent .= '<option value="' . $code . '">' . $role['name'] . '</option>';
                    }
                    // USERS
                    $users = $wps_customer_mdl->getUserList();
                    $select_users = '';
                    if (!empty($users)) {
                        foreach ($users as $user) {
                            if ($user->ID != 1) {
                                $select_users .= '<option value="' . $user->ID . '">' . $user->user_login . '</option>';
                            }
                        }
                    }
                    break;
            }
            $content .= '
				<form method="post">
					<label>' . __('Name', 'wpshop') . '</label><br /><input type="text" name="group-name" style="width:500px;" value="' . $group_name . '" ' . $readonly_name_field . ' /><br /><br />

					<label>' . __('Parent', 'wpshop') . '</label><br />
					<select name="group-parent" class="chosen_select" style="width:500px;">
						' . $select_parent . '
					</select><br /><br />

					<label>' . __('Users', 'wpshop') . '</label><br />
					<select name="group-users[]" class="chosen_select" multiple style="width:500px;">
						' . $select_users . '
					</select><br /><br />

					<label>' . __('Description', 'wpshop') . '</label><br /><textarea name="group-description" style="width:500px;">' . $group_description . '</textarea><br /><br />

					<input type="submit" class="button-primary" name="' . $submit_button_name . '" value="' . $submit_button_value . '" /> &nbsp;&nbsp;&nbsp; <a href="admin.php?page=' . WPSHOP_NEWTYPE_IDENTIFIER_GROUP . '">' . __('Cancel', 'wpshop') . '</a>
				</form>
			';
        } else {
            $wpshop_list_table = new wpshop_groups_custom_List_table();
            //Fetch, prepare, sort, and filter our data...
            $status = "'valid'";
            if (!empty($_REQUEST['attribute_status'])) {
                switch ($_REQUEST['attribute_status']) {
                    case 'unactive':
                        $status = "'moderated', 'notused'";
                        if (empty($_REQUEST['orderby']) && empty($_REQUEST['order'])) {
                            $_REQUEST['orderby'] = 'status';
                            $_REQUEST['order'] = 'asc';
                        }
                        break;
                    default:
                        $status = "'" . $_REQUEST['attribute_status'] . "'";
                        break;
                }
            }
            $roles = get_option('wp_user_roles', array());
            $i = 0;
            $attribute_set_list = array();
            $group_not_to_display = array('administrator', 'editor', 'author', 'contributor', 'subscriber');
            $wpshop_groups_meta = get_option('wpshop_groups_meta', array());
            foreach ($roles as $code => $role) {
                if (!in_array($code, $group_not_to_display)) {
                    $description = !empty($wpshop_groups_meta[$code]['description']) ? $wpshop_groups_meta[$code]['description'] : '--';
                    $attribute_set_list[$i]['name'] = $role['name'];
                    $attribute_set_list[$i]['description'] = $description;
                    $attribute_set_list[$i]['code'] = $code;
                    $i++;
                }
            }
            $wpshop_list_table->prepare_items($attribute_set_list);
            ob_start();
            $wpshop_list_table->display();
            $element_output = ob_get_contents();
            ob_end_clean();
            $content .= $element_output;
        }
        $content .= '</div>';
        echo $content;
    }