<?php
/**
 * canckage WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;

$copyright = isset($campoal_options['conikal_copyright_field']) ? $campoal_options['conikal_copyright_field'] : '';

if (is_author()) {
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
}

$follow_user = get_user_meta($curauth->ID, 'follow_user', true);
$following_user = get_user_meta($curauth->ID, 'following_user', true);
$followers_num = $follow_user ? count($follow_user) : 0;
$following_num = $following_user ? count($following_user) : 0;
$followers = array();
$i = 1;
if ($following_user) {
    foreach ($following_user as $user_id) {
        if ($i <= 14) {
            $user = get_userdata($user_id);

            if ($user) {
                $user_name          = $user->display_name;
                $user_link          = get_author_posts_url($user_id);
                $user_address       = get_user_meta($user_id, 'user_address', true);
                $user_neigborhood   = get_user_meta($user_id, 'user_neigborhood', true);
                $user_city          = get_user_meta($user_id, 'user_city', true);
                $user_state         = get_user_meta($user_id, 'user_state', true);
                $user_country       = get_user_meta($user_id, 'user_country', true);
                $user_avatar        = conikal_get_avatar_url($user_id, array('size' => 64));
                $user_avatar_retina = conikal_get_avatar_url($user_id, array('size' => 128));
                
                $follower = array(  
                    'ID'            => $user_id,
                    'name'          => $user_name,
                    'avatar'        => $user_avatar,
                    'avatar_retina' => $user_avatar_retina,
                    'link'          => $user_link,
                    'address'       => $user_address,
                    'neighborhood'  => $user_neigborhood,
                    'city'          => $user_city,
                    'state'         => $user_state,
                    'country'       => $user_country
                );

                $follower = (object) $follower;
                array_push($followers, $follower);
                $i++;
            }
        }
    }
}

$author_petition_count = count_user_posts($curauth->ID, 'petition');

// check if user is decision maker
$decisionmaker_id = get_user_meta($curauth->ID, 'user_decision', true);

// placeholder lazy load avatar
$lazy_placeholder_avatar = conikal_lazy_load_placeholder('avatar');
?>


    <div class="five wide column computer only">
        <div class="ui sticky" id="about-sticky">
            <div class="ui raised padded segment">
                <h2 class="ui header">
                    <?php esc_html_e('About', 'campoal') ?>
                    <?php if (!empty($decisionmaker_id)) { ?>
                        <span class="ui primary label"><i class="balance scale icon"></i><?php esc_html_e('Legislator', 'campoal') ?></span>
                    <?php } ?>
                </h2>
                <p>
                    <?php echo esc_html($curauth->description ? $curauth->description : ''); ?>
                </p>
                <div class="ui list truncate">
                    <?php if ($curauth->user_country || $curauth->user_state || $curauth->user_city) { ?>
                        <div class="item"><i class="map marker icon"></i>
                            <div class="content">
                            <?php echo esc_html($curauth->user_city ? $curauth->user_city . ', ' : '') . esc_html($curauth->user_state ? $curauth->user_state . ', ' : '') . esc_html($curauth->user_country ? $curauth->user_country : ''); ?>
                            </div>
                        </div>
                    <?php } ?>
                    <?php if ($curauth->user_url) { ?>
                        <div class="item"><i class="linkify icon"></i>
                            <div class="content">
                            <a href="<?php echo esc_url($curauth->user_url) ?>"><?php echo esc_html($curauth->user_url) ?></a>
                            </div>
                        </div>
                    <?php } ?>
                    <?php if (($curauth->user_phone_code && $curauth->user_phone) && ($curauth->show_phone != 0 || current_user_can('editor') || current_user_can('administrator'))) { ?>
                        <div class="item"><i class="phone icon"></i>
                            <div class="content">
                            <?php echo esc_html__('Phone:', 'campoal') . ' ' . esc_html('+' . $curauth->user_phone_code) . ' '; ?>
                            <span class="author-phone-number" id="author-phone-number" data-id=""><?php echo esc_html( $curauth->user_phone ) ?></span>
                            </div>
                        </div>
                    <?php } ?>
                    <?php if ($curauth->user_birthday && ($curauth->show_birthday != 0 || current_user_can('editor') || current_user_can('administrator'))) { ?>
                        <div class="item"><i class="birthday icon"></i>
                            <div class="content">
                            <?php echo esc_html__('Born on', 'campoal') . ' ' . esc_html(date_i18n(get_option('date_format'), strtotime($curauth->user_birthday))) ?>
                            </div>
                        </div>
                    <?php } ?>
                    <?php if ($curauth->show_join_date != 0 || !isset($curauth->show_join_date)) { ?>
                    <div class="item"><i class="calendar outline icon"></i>
                        <div class="content">
                        <?php echo esc_html__('Joined', 'campoal') . ' ' . esc_html(date_i18n(get_option('date_format'), strtotime($curauth->user_registered))) ?>
                        </div>
                    </div>
                    <?php } ?>
                </div>
                <?php if ($followers) { ?>
                    <div class="ui divider"></div>
                    <p><?php esc_html_e('Who is following?', 'campoal') ?></p>
                    <div class="ui mini images">
                    <?php foreach ($followers as $follower) : ?>
                        <a href="<?php echo get_author_posts_url($follower->ID) ?>" data-bjax>
                            <img class="ui circular image supporter-avatar lazy" data-src="<?php echo esc_url($follower->avatar) ?>" data-retina="<?php echo esc_url($follower->avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>" alt="<?php echo esc_attr($follower->name) ?>" data-title="<?php echo esc_attr($follower->name) ?>" data-content="<?php if ($follower->country || $follower->state || $follower->city) { ?>
                            <?php echo esc_attr($follower->city ? $follower->city . ', ' : '') . esc_attr($follower->state ? $follower->state . ', ' : '') . esc_attr($follower->country ? $follower->country : '') ?>
                            <?php } ?>">
                        </a>
                    <?php endforeach; ?>
                    </div>
                <?php } ?>
                <?php if ( function_exists('conikal_register_petition_type') ) : ?>
                    <div class="ui divider"></div>
                    <div class="ui mini three statistics">
                        <div class="statistic">
                            <div class="value">
                                <?php echo esc_html(conikal_format_number('%!.0i', $author_petition_count, true)); ?>
                            </div>
                            <div class="label font tiny"><?php esc_html_e('Petitions', 'campoal') ?></div>
                        </div>
                        <div class="statistic">
                            <div class="value">
                                <?php echo esc_html(conikal_format_number('%!.0i', $followers_num, true)); ?>
                            </div>
                            <div class="label font tiny"><?php esc_html_e('Followers', 'campoal') ?></div>
                        </div>
                        <div class="statistic">
                            <div class="value">
                                <?php echo esc_html(conikal_format_number('%!.0i', $following_num, true)); ?>
                            </div>
                            <div class="label font tiny"><?php esc_html_e('Following', 'campoal') ?></div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            <div class="ui text small menu sidebar-menu sidebar-footer-menu">
                    <?php conikal_custom_menu('footer') ?>
            </div>
            <?php if($copyright != '') { ?>
                <div class="copyright font small"><?php echo sprintf( wp_kses_post($copyright ? $copyright : __('© %1$s %2$s by Conikal.', 'campoal')), date('Y'), get_bloginfo('name')); ?></div>
            <?php } ?>
        </div>
    </div>
    <div class="sixteen wide column mobile only mobile-full">
        <div class="ui raised padded segment author-info-segment">
            <div class="ui header"><?php esc_html_e('About', 'campoal') ?></div>
            <?php echo esc_html($curauth->description ? $curauth->description : ''); ?>
            <div class="ui list truncate">
                <?php if ($curauth->user_country || $curauth->user_state || $curauth->user_city) { ?>
                    <div class="item"><i class="map marker icon"></i>
                        <div class="content">
                        <?php echo esc_html($curauth->user_city ? $curauth->user_city . ', ' : '') . esc_html($curauth->user_state ? $curauth->user_state . ', ' : '') . esc_html($curauth->user_country ? $curauth->user_country : '') ?>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($curauth->user_url) { ?>
                    <div class="item"><i class="linkify icon"></i>
                        <div class="content">
                        <a href="<?php echo esc_url($curauth->user_url) ?>"><?php echo esc_html($curauth->user_url) ?></a>
                        </div>
                    </div>
                <?php } ?>
                <?php if (($curauth->user_phone_code && $curauth->user_phone) && ($curauth->show_phone != 0 || current_user_can('editor') || current_user_can('administrator'))) { ?>
                    <div class="item"><i class="phone icon"></i>
                        <div class="content">
                        <?php echo esc_html__('Phone:', 'campoal') . ' ' . esc_html('+' . $curauth->user_phone_code) . ' '; ?>
                        <span class="author-phone-number" id="author-phone-number" data-id=""><?php echo esc_html( $curauth->user_phone ) ?></span>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($curauth->user_birthday && ($curauth->show_birthday != 0 || current_user_can('editor') || current_user_can('administrator'))) { ?>
                    <div class="item"><i class="birthday icon"></i>
                        <div class="content">
                        <?php echo esc_html__('Born on', 'campoal') . ' ' . esc_html(date_i18n(get_option('date_format'), strtotime($curauth->user_birthday))) ?>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($curauth->show_join_date != 0 || !isset($curauth->show_join_date)) { ?>
                <div class="item"><i class="calendar outline icon"></i>
                    <div class="content">
                    <?php echo esc_html__('Joined', 'campoal') . ' ' . esc_html(date_i18n(get_option('date_format'), strtotime($curauth->user_registered))) ?>
                    </div>
                </div>
                <?php } ?>
            </div>
            <?php if ($followers) { ?>
                <div class="ui divider"></div>
                <p><?php esc_html_e('Who is following?', 'campoal') ?></p>
                <div class="ui mini images">
                <?php foreach ($followers as $follower) : ?>
                    <a href="<?php echo get_author_posts_url($follower->ID) ?>" data-bjax>
                        <img class="ui circular image supporter-avatar lazy" data-src="<?php echo esc_url($follower->avatar) ?>" data-retina="<?php echo esc_url($follower->avatar_retina) ?>" src="<?php echo esc_url($lazy_placeholder_avatar) ?>" alt="<?php echo esc_attr($follower->name) ?>" data-title="<?php echo esc_attr($follower->name) ?>" data-content="<?php if ($follower->country || $follower->state || $follower->city) { ?>
                        <?php echo esc_attr($follower->city ? $follower->city . ', ' : '') . esc_attr($follower->state ? $follower->state . ', ' : '') . esc_attr($follower->country ? $follower->country : '') ?>
                        <?php } ?>">
                    </a>
                <?php endforeach; ?>
                </div>
            <?php } ?>
            <?php if ( function_exists('conikal_register_petition_type') ) : ?>
                <div class="ui divider"></div>
                <div class="ui mini three statistics">
                    <div class="statistic">
                        <div class="value">
                            <?php echo esc_html(conikal_format_number('%!.0i', $author_petition_count, true)); ?>
                        </div>
                        <div class="label font tiny"><?php esc_html_e('Petitions', 'campoal') ?></div>
                    </div>
                    <div class="statistic">
                        <div class="value">
                            <?php echo esc_html(conikal_format_number('%!.0i', $followers_num, true)); ?>
                        </div>
                        <div class="label font tiny"><?php esc_html_e('Followers', 'campoal') ?></div>
                    </div>
                    <div class="statistic">
                        <div class="value">
                            <?php echo esc_html(conikal_format_number('%!.0i', $following_num, true)); ?>
                        </div>
                        <div class="label font tiny"><?php esc_html_e('Following', 'campoal') ?></div>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>