<commit_before><?php
namespace Chamilo\Core\Repository\Implementation\Youtube\Form;

use Chamilo\Core\Repository\Implementation\Youtube\DataConnector;
use Chamilo\Core\Repository\Implementation\Youtube\ExternalObject;
use Chamilo\Libraries\Format\Form\FormValidator;
use Chamilo\Libraries\Platform\Translation;
use Chamilo\Libraries\Utilities\Utilities;

/**
 * $Id: youtube_external_repository_manager_form.class.php 224 2009-11-13 14:40:30Z kariboe $
 *
 * @package
 *
 *
 *
 */
class ExternalObjectForm extends FormValidator
{
    const TYPE_CREATE = 1;
    const TYPE_EDIT = 2;
    const RESULT_SUCCESS = 'GroupUpdated';
    const RESULT_ERROR = 'GroupUpdateFailed';
    const VIDEO_TITLE = 'title';
    const VIDEO_CATEGORY = 'category';
    const VIDEO_TAGS = 'tags';
    const VIDEO_DESCRIPTION = 'description';

    private $application;

    private $form_type;

    private $video;

    private $external_repository_object;

    public function __construct($form_type, $action, $application)
    {
        parent :: __construct('youtube_upload', 'post', $action);

        $this->application = $application;

        $this->form_type = $form_type;
        if ($this->form_type == self :: TYPE_CREATE)
        {
            $this->build_creation_form();
        }
        elseif ($this->form_type == self :: TYPE_EDIT)
        {
            $this->build_creation_form();
        }
        $this->build_upload_form();

        $this->setDefaults();
    }



    public function build_upload_form()
    {
        $this->addElement('hidden', 'token', $this->token);
        $this->addElement('file', 'file', sprintf(Translation :: get('FileName'), '2Gb'));

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Upload', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }


    public function set_external_repository_object(ExternalObject $external_repository_object)
    {
        $this->external_repository_object = $external_repository_object;
        $this->addElement('hidden', ExternalObject :: PROPERTY_ID);
        $defaults[ExternalObject :: PROPERTY_TITLE] = $external_repository_object->get_title();
        $defaults[ExternalObject :: PROPERTY_DESCRIPTION] = $external_repository_object->get_description();
        $defaults[ExternalObject :: PROPERTY_CATEGORY] = $external_repository_object->get_category();
        $defaults[ExternalObject :: PROPERTY_TAGS] = $this->get_tags();

        parent :: setDefaults($defaults);
    }

    public function get_tags()
    {
        $external_repository_object = $this->external_repository_object;
        $tags = $external_repository_object->get_tags();
        return implode(",", $tags);
    }

    public function build_basic_form()
    {
        $this->addElement(
            'text',
            ExternalObject :: PROPERTY_TITLE,
            Translation :: get('Title', null, Utilities :: COMMON_LIBRARIES),
            array("size" => "50"));
        $this->addRule(
            ExternalObject :: PROPERTY_TITLE,
            Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES),
            'required');

        $this->addElement(
            'select',
            ExternalObject :: PROPERTY_CATEGORY,
            Translation :: get('Category', null, Utilities :: COMMON_LIBRARIES),
            $this->get_youtube_categories());

        $this->addElement(
            'textarea',
            ExternalObject :: PROPERTY_TAGS,
            Translation :: get('Tags'),
            array("rows" => "2", "cols" => "80"));
        $this->addRule(
            ExternalObject :: PROPERTY_TAGS,
            Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES),
            'required');

        $this->addElement(
            'textarea',
            ExternalObject :: PROPERTY_DESCRIPTION,
            Translation :: get('Description', null, Utilities :: COMMON_LIBRARIES),
            array("rows" => "7", "cols" => "80"));
    }

    public function get_youtube_categories()
    {
        $connector = DataConnector :: get_instance($this->application->get_external_repository());
        return $connector->retrieve_categories();
    }

    public function build_editing_form()
    {
        $this->build_basic_form();

        $this->addElement('hidden', ExternalObject :: PROPERTY_ID);

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Edit', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive update'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }

    public function build_creation_form()
    {
        $this->build_basic_form();

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Create', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }

    public function update_video_entry()
    {
        $youtube = $this->application->get_external_repository_manager_connector();
        $values = $this->exportValues();

        return $youtube->update_youtube_video($values);
    }

    public function upload_video()
    {
        $values = $this->exportValues();

        $connector = $this->application->get_external_repository_manager_connector();
        return $connector->upload_video($values);
    }

    /**
     * Sets default values.
     *
     * @param $defaults array Default values for this form's parameters.
     */
    public function setDefaults($defaults = array ())
    {
        // $defaults[self :: VIDEO_TITLE] = $this->video->getVideoTitle();
        // $defaults[self :: VIDEO_CATEGORY] = $this->video_entry->getVideoCategory();
        // $defaults[self :: VIDEO_TAGS] = $this->video_entry->getVideoTags();
        // $defaults[self :: VIDEO_DESCRIPTION] = $this->video_entry->getVideoDescription();
        parent :: setDefaults($defaults);
    }

    public function get_video()
    {
        return $this->video;
    }
}
<commit_msg>youtube uplaoder<commit_after><?php
namespace Chamilo\Core\Repository\Implementation\Youtube\Form;

use Chamilo\Core\Repository\Implementation\Youtube\DataConnector;
use Chamilo\Core\Repository\Implementation\Youtube\ExternalObject;
use Chamilo\Libraries\Format\Form\FormValidator;
use Chamilo\Libraries\Platform\Translation;
use Chamilo\Libraries\Utilities\Utilities;

/**
 * $Id: youtube_external_repository_manager_form.class.php 224 2009-11-13 14:40:30Z kariboe $
 *
 * @package
 *
 *
 *
 */
class ExternalObjectForm extends FormValidator
{
    const TYPE_CREATE = 1;
    const TYPE_EDIT = 2;
    const RESULT_SUCCESS = 'GroupUpdated';
    const RESULT_ERROR = 'GroupUpdateFailed';
    const VIDEO_TITLE = 'title';
    const VIDEO_CATEGORY = 'category';
    const VIDEO_TAGS = 'tags';
    const VIDEO_DESCRIPTION = 'description';

    private $application;

    private $form_type;

    private $video;

    private $external_repository_object;

    public function __construct($form_type, $action, $application)
    {
        parent :: __construct('youtube_upload', 'post', $action);

        $this->application = $application;

        $this->form_type = $form_type;
        if ($this->form_type == self :: TYPE_CREATE)
        {
            $this->build_uploading_form();
        }
        elseif ($this->form_type == self :: TYPE_EDIT)
        {
            $this->build_edit_form();
        }

        $this->setDefaults();
    }



    public function build_upload_form()
    {
        $this->addElement('hidden', 'token', $this->token);
        $this->addElement('file', 'file', sprintf(Translation :: get('FileName'), '2Gb'));

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Upload', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }


    public function set_external_repository_object(ExternalObject $external_repository_object)
    {
        $this->external_repository_object = $external_repository_object;
        $this->addElement('hidden', ExternalObject :: PROPERTY_ID);
        $defaults[ExternalObject :: PROPERTY_TITLE] = $external_repository_object->get_title();
        $defaults[ExternalObject :: PROPERTY_DESCRIPTION] = $external_repository_object->get_description();
        $defaults[ExternalObject :: PROPERTY_CATEGORY] = $external_repository_object->get_category();
        $defaults[ExternalObject :: PROPERTY_TAGS] = $this->get_tags();

        parent :: setDefaults($defaults);
    }

    public function get_tags()
    {
        $external_repository_object = $this->external_repository_object;
        $tags = $external_repository_object->get_tags();
        return implode(",", $tags);
    }

    public function build_basic_form()
    {
        $this->addElement(
            'text',
            ExternalObject :: PROPERTY_TITLE,
            Translation :: get('Title', null, Utilities :: COMMON_LIBRARIES),
            array("size" => "50"));
        $this->addRule(
            ExternalObject :: PROPERTY_TITLE,
            Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES),
            'required');

        $this->addElement(
            'select',
            ExternalObject :: PROPERTY_CATEGORY,
            Translation :: get('Category', null, Utilities :: COMMON_LIBRARIES),
            $this->get_youtube_categories());

        $this->addElement(
            'textarea',
            ExternalObject :: PROPERTY_TAGS,
            Translation :: get('Tags'),
            array("rows" => "2", "cols" => "80"));
        $this->addRule(
            ExternalObject :: PROPERTY_TAGS,
            Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES),
            'required');

        $this->addElement(
            'textarea',
            ExternalObject :: PROPERTY_DESCRIPTION,
            Translation :: get('Description', null, Utilities :: COMMON_LIBRARIES),
            array("rows" => "7", "cols" => "80"));
    }

    public function get_youtube_categories()
    {
        $connector = DataConnector :: get_instance($this->application->get_external_repository());
        return $connector->retrieve_categories();
    }

    public function build_editing_form()
    {
        $this->build_basic_form();

        $this->addElement('hidden', ExternalObject :: PROPERTY_ID);

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Edit', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive update'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }

    public function build_uploading_form()
    {
        $this->build_basic_form();

//         $this->addElement('hidden', 'token', $this->token);
        $this->addElement('file', 'file', sprintf(Translation :: get('FileName'), '2Gb'));

        $buttons[] = $this->createElement(
            'style_submit_button',
            'submit',
            Translation :: get('Upload', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'positive'));
        $buttons[] = $this->createElement(
            'style_reset_button',
            'reset',
            Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES),
            array('class' => 'normal empty'));

        $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
    }

    public function update_video_entry()
    {
        $youtube = $this->application->get_external_repository_manager_connector();
        $values = $this->exportValues();

        return $youtube->update_youtube_video($values);
    }

    public function upload_video()
    {
        $values = $this->exportValues();

        $connector = $this->application->get_external_repository_manager_connector();
        return $connector->upload_video($values);
    }

    /**
     * Sets default values.
     *
     * @param $defaults array Default values for this form's parameters.
     */
    public function setDefaults($defaults = array ())
    {
        // $defaults[self :: VIDEO_TITLE] = $this->video->getVideoTitle();
        // $defaults[self :: VIDEO_CATEGORY] = $this->video_entry->getVideoCategory();
        // $defaults[self :: VIDEO_TAGS] = $this->video_entry->getVideoTags();
        // $defaults[self :: VIDEO_DESCRIPTION] = $this->video_entry->getVideoDescription();
        parent :: setDefaults($defaults);
    }

    public function get_video()
    {
        return $this->video;
    }
}

{'fraction_non_alphanumeric': 0.1405982905982906, 'fraction_numerical': 0.00405982905982906, 'mean_word_length': 2.7402770378263184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6650507', 'n_tokens_mistral': 4055, 'n_tokens_neox': 3851, 'n_words': 702}