<commit_before><?php
namespace Selene\Matisse\Components;

use Selene\Matisse\AttributeType;
use Selene\Matisse\ComponentAttributes;
use Selene\Matisse\VisualComponent;
use Selene\Media;

class FileUploadAttributes extends ComponentAttributes
{
  public $name;
  public $value;
  public $no_clear = false;
  public $disabled = false;

  protected function typeof_name ()
  {
    return AttributeType::ID;
  }

  protected function typeof_value ()
  {
    return AttributeType::TEXT;
  }

  protected function typeof_no_clear ()
  {
    return AttributeType::BOOL;
  }

  protected function typeof_disabled ()
  {
    return AttributeType::BOOL;
  }
}

class FileUpload extends VisualComponent
{

  protected $autoId = true;

  /**
   * Returns the component's attributes.
   *
   * @return FileUploadAttributes
   */
  public function attrs ()
  {
    return $this->attrsObj;
  }

  /**
   * Creates an instance of the component's attributes.
   *
   * @return FileUploadAttributes
   */
  public function newAttributes ()
  {
    return new FileUploadAttributes($this);
  }

  protected function preRender ()
  {
  }

  protected function postRender ()
  {
  }

  protected function render ()
  {
    $this->page->enableFileUpload = true;
    $attr  = $this->attrs ();
    $value = $attr->get ('value', '');

    if ($this->autoId)
      $this->setAutoId ();
    $this->beginTag ('input');
    $this->addAttribute ('id', $attr->id . (empty($value) ? 'File' : 'Text'));
    $this->addAttribute ('class', enum (' ',
      $this->className,
      $this->cssClassName,
      $this->attrs ()->class,
      $this->attrs ()->css_class,
      $this->attrs ()->disabled ? 'disabled' : null
    ));
    if (!empty($this->attrs ()->html_attrs))
      echo ' ' . $this->attrs ()->html_attrs;

    if (!empty($value)) {
      // File exists

      $this->addAttribute ('type', 'text');
      $this->addAttribute ('value', Media::getOriginalFileName ($value));
      $this->addAttribute ('readonly', "");

      $this->beginTag ('input');
      $this->addAttribute ('type', 'file');
      $this->addAttribute('style', 'display: none');
      $this->addAttribute ('id', "{$attr->id}File");
      $this->endTag ();
      $this->addTag ('button', [
        'onclick' => "$('#{$attr->id}Field').val('');$('#{$attr->id}Text').hide();$('#{$attr->id}File').show();$(this).hide()"
      ], 'Clear');

    } else {
      // File doesn't exist

      $this->addAttribute ('type', 'file');
      $this->endTag ();
    }

    $this->beginTag ('input');
    $this->addAttribute ('type', 'hidden');
    $this->addAttribute ('id', "{$attr->id}Field");
    if (isset($attr->name))
      $this->addAttribute ('name', $attr->name);
    else $this->addAttribute ('name', $attr->id);
    $this->addAttribute ('value', $value);
    $this->endTag ();
    $this->handleFocus ();<commit_msg>Completed the file-upload remake.<commit_after><?php
namespace Selene\Matisse\Components;

use Selene\Matisse\AttributeType;
use Selene\Matisse\ComponentAttributes;
use Selene\Matisse\VisualComponent;
use Selene\Media;

class FileUploadAttributes extends ComponentAttributes
{
  public $name;
  public $value;
  public $no_clear = false;
  public $disabled = false;
  public $clear_button_class = 'fa fa-times';

  protected function typeof_name ()
  {
    return AttributeType::ID;
  }

  protected function typeof_value ()
  {
    return AttributeType::TEXT;
  }

  protected function typeof_no_clear ()
  {
    return AttributeType::BOOL;
  }

  protected function typeof_disabled ()
  {
    return AttributeType::BOOL;
  }

  protected function typeof_clear_button_class ()
  {
    return AttributeType::TEXT;
  }
}

class FileUpload extends VisualComponent
{

  protected $autoId = true;

  /**
   * Returns the component's attributes.
   *
   * @return FileUploadAttributes
   */
  public function attrs ()
  {
    return $this->attrsObj;
  }

  /**
   * Creates an instance of the component's attributes.
   *
   * @return FileUploadAttributes
   */
  public function newAttributes ()
  {
    return new FileUploadAttributes($this);
  }

  protected function preRender ()
  {
  }

  protected function postRender ()
  {
  }

  protected function render ()
  {
    $this->page->enableFileUpload = true;
    $attr                         = $this->attrs ();
    $value                        = $attr->get ('value', '');
    $id = $attr->id;
    $name = $attr->name;

    if ($this->autoId)
      $this->setAutoId ();
    $this->beginTag ('input');
    $this->addAttribute ('id', $id . (empty($value) ? 'File' : 'Text'));
    $this->addAttribute ('class', enum (' ',
      $this->className,
      $this->cssClassName,
      $this->attrs ()->class,
      $this->attrs ()->disabled ? 'disabled' : null
    ));
    if (!empty($this->attrs ()->html_attrs))
      echo ' ' . $this->attrs ()->html_attrs;

    if (empty($value)) {
      // File doesn't exist

      $this->addAttribute ('type', 'file');
      $this->addAttribute ('name', "{$name}_file");
      $this->endTag ();
    }
    else {
      // File exists

      $this->addAttribute ('type', 'text');
      $this->addAttribute ('value', Media::getOriginalFileName ($value));
      $this->addAttribute ('readonly', "");

      $this->addTag ('button', [
        'class' => "btn btn-default $attr->clear_button_class",
        'onclick' => "$('#{$id}Field').val('');var c=$('#{$id}Text').hide().attr('class');$('#{$id}File').show().attr('class',c);$(this).hide()"
      ]);

      $this->beginTag ('input');
      $this->addAttribute ('type', 'file');
      $this->addAttribute ('name', "{$name}_file");
      $this->addAttribute ('style', 'display: none');
      $this->addAttribute ('id', "{$id}File");
      $this->endTag ();
    }

    $this->beginTag ('input');
    $this->addAttribute ('type', 'hidden');
    $this->addAttribute ('id', "{$id}Field");
    if (isset($name))
      $this->addAttribute ('name', $name);
    else $this->addAttribute ('name', $id);
    $this->addAttribute ('value', $value);
    $this->endTag ();
    $this->handleFocus ();
{'fraction_non_alphanumeric': 0.18490441661173368, 'fraction_numerical': 0.0, 'mean_word_length': 3.0731543624161075, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 100, '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': '5897350', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1845, 'n_words': 393}