<?php /*

 Composr
 Copyright (c) ocProducts, 2004-2016

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    cns_member_avatars
 */

/**
 * Hook class.
 */
class Hook_profiles_tabs_edit_avatar
{
    /**
     * Find whether this hook is active.
     *
     * @param  MEMBER $member_id_of The ID of the member who is being viewed
     * @param  MEMBER $member_id_viewing The ID of the member who is doing the viewing
     * @return boolean Whether this hook is active
     */
    public function is_active($member_id_of, $member_id_viewing)
    {
        return (($member_id_of == $member_id_viewing) || (has_privilege($member_id_viewing, 'assume_any_member')) || (has_privilege($member_id_viewing, 'member_maintenance')));
    }

    /**
     * Render function for profile tabs edit hooks.
     *
     * @param  MEMBER $member_id_of The ID of the member who is being viewed
     * @param  MEMBER $member_id_viewing The ID of the member who is doing the viewing
     * @param  boolean $leave_to_ajax_if_possible Whether to leave the tab contents null, if tis hook supports it, so that AJAX can load it later
     * @return ?array A tuple: The tab title, the tab body text (may be blank), the tab fields, extra JavaScript (may be blank) the suggested tab order, hidden fields (optional) (null: if $leave_to_ajax_if_possible was set), the icon
     */
    public function render_tab($member_id_of, $member_id_viewing, $leave_to_ajax_if_possible = false)
    {
        $title = do_lang_tempcode('AVATAR');

        $order = 20;

        // Actualiser
        if (post_param_integer('submitting_avatar_tab', 0) == 1) {
            require_code('uploads');
            if (has_privilege($member_id_viewing, 'own_avatars')) {
                if (!(((is_plupload(true)) && (array_key_exists('avatar_file', $_FILES))) || ((array_key_exists('avatar_file', $_FILES)) && (is_uploaded_file($_FILES['avatar_file']['tmp_name']))))) { // No upload -> URL or stock or none
                    $urls = array();
                    $stock = post_param_string('avatar_alt_url', '');
                    if ($stock == '') { // No URL -> Stock or none
                        $stock = post_param_string('avatar_stock', null);
                        if (!is_null($stock)) { // Stock
                            $urls[0] = ($stock == '') ? '' : find_theme_image($stock, false, true);
                        } else {
                            $urls[0] = ''; // None
                        }
                    } else {
                        if ((url_is_local($stock)) && (!$GLOBALS['FORUM_DRIVER']->is_super_admin($member_id_viewing))) {
                            $old = $GLOBALS['FORUM_DB']->query_select_value('f_members', 'm_avatar_url', array('id' => $member_id_of));
                            if ($old != $stock) {
                                access_denied('ASSOCIATE_EXISTING_FILE');
                            }
                        }
                        $urls[0] = $stock; // URL
                    }
                } else { // Upload
                    // We have chosen an upload. Note that we will not be looking at alt_url at this point, even though it is specified below for canonical reasons
                    set_images_cleanup_pipeline_settings(IMG_RECOMPRESS_LOSSLESS, null, null, true); // Code to strip GPS
                    $urls = get_url('avatar_alt_url', 'avatar_file', file_exists(get_custom_file_base() . '/uploads/avatars') ? 'uploads/avatars' : 'uploads/cns_avatars', 0, CMS_UPLOAD_IMAGE, false, '', '', false, true);
                    reset_images_cleanup_pipeline_settings();
                    if (((get_base_url() != get_forum_base_url()) || ((!empty($GLOBALS['SITE_INFO']['on_msn'])) && ($GLOBALS['SITE_INFO']['on_msn'] == '1'))) && ($urls[0] != '') && (url_is_local($urls[0]))) {
                        $urls[0] = get_custom_base_url() . '/' . $urls[0];
                    }
                }

                $avatar_url = $urls[0];
            } else {
                $stock = post_param_string('avatar_stock');
                $avatar_url = ($stock == '') ? '' : find_theme_image($stock, false, true);
            }

            require_code('cns_members_action');
            require_code('cns_members_action2');
            cns_member_choose_avatar($avatar_url, $member_id_of);

            attach_message(do_lang_tempcode('SUCCESS_SAVE'), 'inform');
        }

        if ($leave_to_ajax_if_possible) {
            return null;
        }

        // UI fields

        $avatar_url = $GLOBALS['FORUM_DRIVER']->get_member_row_field($member_id_of, 'm_avatar_url');

        require_javascript('multi');

        $fields = new Tempcode();
        require_code('form_templates');
        require_code('themes2');
        $ids = get_all_image_ids_type('cns_default_avatars', true);

        $found_it = false;
        foreach ($ids as $id) {
            $pos = strpos($avatar_url, '/' . $id);
            $selected = ($pos !== false);
            if ($selected) {
                $found_it = true;
            }
        }
        $hidden = new Tempcode();
        if (has_privilege($member_id_viewing, 'own_avatars')) {
            $set_name = 'avatar';
            $required = false;
            $set_title = do_lang_tempcode('IMAGE');
            $field_set = alternate_fields_set__start($set_name);

            $field_set->attach(form_input_upload(do_lang_tempcode('UPLOAD'), '', 'avatar_file', false, null, null, true, str_replace(' ', '', get_option('valid_images'))));

            $field_set->attach(form_input_url(do_lang_tempcode('URL'), '', 'avatar_alt_url', $found_it ? '' : $avatar_url, false));

            $field_set->attach(form_input_theme_image(do_lang_tempcode('STOCK'), '', 'avatar_stock', $ids, $avatar_url, null, null, false));

            $fields->attach(alternate_fields_set__end($set_name, $set_title, '', $field_set, $required, null, true));

            handle_max_file_size($hidden, 'image');
        } else {
            single_field__start();

            $fields->attach(form_input_theme_image(do_lang_tempcode('STOCK'), '', 'avatar_stock', $ids, $avatar_url, null, null, true));

            single_field__end();
        }

        if ($avatar_url != '') {
            if (url_is_local($avatar_url)) {
                $avatar_url = get_complex_base_url($avatar_url) . '/' . $avatar_url;
            }
            $avatar = do_template('CNS_TOPIC_POST_AVATAR', array('_GUID' => '50a5902f3ab7e384d9cf99577b222cc8', 'AVATAR' => $avatar_url));
        } else {
            $avatar = do_lang_tempcode('NONE_EM');
        }

        $width = cns_get_member_best_group_property($member_id_of, 'max_avatar_width');
        $height = cns_get_member_best_group_property($member_id_of, 'max_avatar_height');

        $text = do_template('CNS_EDIT_AVATAR_TAB', array('_GUID' => 'dbdac6ca3bc752b54d2a24a4c6e69c7c', 'MEMBER_ID' => strval($member_id_of), 'USERNAME' => $GLOBALS['FORUM_DRIVER']->get_username($member_id_of), 'AVATAR' => $avatar, 'WIDTH' => integer_format($width), 'HEIGHT' => integer_format($height)));

        $hidden = new Tempcode();
        $hidden->attach(form_input_hidden('submitting_avatar_tab', '1'));

        $javascript = '';

        return array($title, $fields, $text, $javascript, $order, $hidden, 'tabs/member_account/edit/avatar', true);
    }
}
