<commit_before><?php namespace Bkwld\Decoy\Fields;

// Dependencies
use Bkwld\Library\Utils;
use Config;
use Former\Form\Fields\File;
use HtmlObject\Input as HtmlInput;
use Illuminate\Container\Container;
use Str;

/**
 * Creates a file upload field with addtional UI for reviewing the last upload
 * and deleting it.
 */
class Upload extends File {
	use Traits\InsertMarkup;

	/**
	 * Create a regular file type field
	 *
	 * @param Container $app        The Illuminate Container
	 * @param string    $type       text
	 * @param string    $name       Field name
	 * @param string    $label      Its label
	 * @param string    $value      Its value
	 * @param array     $attributes Attributes
	 */
	public function __construct(Container $app, $type, $name, $label, $value, $attributes) {
		parent::__construct($app, 'file', $name, $label, $value, $attributes);
		$this->addGroupClass('upload');

		// Add the max upload info
		$this->addClass('js-tooltip');
		$this->title('Max upload size: <b>'.Utils\String::humanSize(Utils\File::maxUpload(), 1).'</b>');
	}

	/**
	 * Prints out the field, wrapped in its group.  Additional review UI is tacked on here.
	 * 
	 * @return string
	 */
	public function wrapAndRender() {

		// Get additional UI first so it can modify the normal form-group UI.
		$html = $this->renderReview();

		// Add extra markup
		return $this->appendToGroup(parent::wrapAndRender(), $html);
	}

	/**
	 * Prints out the current file upload field. The hidden field with the current upload is
	 * prepended before the input so it can be overriden
	 *
	 * @return string An input tag
	 */
	public function render() {

		// A file has already been uploaded
		if ($this->value) {

			// If it's required, show the icon but don't enforce it.  There is already
			// a file uploaded after all
			if ($this->isRequired()) $this->setAttribute('required', null);

			// Add hidden field and return
			return $this->renderHidden().parent::render();
		
		// The field is empty
		} else return parent::render();
	}

	/**
	 * Render the hidden field that contains the currently uploaded file
	 *
	 * @return string A hidden field
	 */
	protected function renderHidden() {
		return HtmlInput::hidden($this->name, $this->value);
	}

	/**
	 * Render the display of the currently uploaded item
	 *
	 * @return string HTML
	 */
	protected function renderReview() {
		if (!$this->value) return;
		else if (!$this->isRequired() && $this->isInUploads()) return $this->renderDestuctableReview();
		else return $this->renderIndestructibleReview();
	}

	/**
	 * Check if the file is in the uploads directory. The use case for this arose 
	 * with the Fragments system where the default images would usually be in the 
	 * img directory
	 *
	 * @return boolean<commit_msg>Stop passing the old path in upload / image fields<commit_after><?php namespace Bkwld\Decoy\Fields;

// Dependencies
use Bkwld\Library\Utils;
use Config;
use Former\Form\Fields\File;
use HtmlObject\Input as HtmlInput;
use Illuminate\Container\Container;
use Str;

/**
 * Creates a file upload field with addtional UI for reviewing the last upload
 * and deleting it.
 */
class Upload extends File {
	use Traits\InsertMarkup;

	/**
	 * Create a regular file type field
	 *
	 * @param Container $app        The Illuminate Container
	 * @param string    $type       text
	 * @param string    $name       Field name
	 * @param string    $label      Its label
	 * @param string    $value      Its value
	 * @param array     $attributes Attributes
	 */
	public function __construct(Container $app, $type, $name, $label, $value, $attributes) {
		parent::__construct($app, 'file', $name, $label, $value, $attributes);
		$this->addGroupClass('upload');

		// Add the max upload info
		$this->addClass('js-tooltip');
		$this->title('Max upload size: <b>'.Utils\String::humanSize(Utils\File::maxUpload(), 1).'</b>');
	}

	/**
	 * Prints out the field, wrapped in its group.  Additional review UI is tacked on here.
	 * 
	 * @return string
	 */
	public function wrapAndRender() {

		// Get additional UI first so it can modify the normal form-group UI.
		$html = $this->renderReview();

		// Add extra markup
		return $this->appendToGroup(parent::wrapAndRender(), $html);
	}

	/**
	 * Prints out the current file upload field. The hidden field with the current upload is
	 * prepended before the input so it can be overriden
	 *
	 * @return string An input tag
	 */
	public function render() {

		// A file has already been uploaded
		if ($this->value) {

			// If it's required, show the icon but don't enforce it.  There is already
			// a file uploaded after all
			if ($this->isRequired()) $this->setAttribute('required', null);

		}

		// Continue execution
		return parent::render();
	}

	/**
	 * Render the display of the currently uploaded item
	 *
	 * @return string HTML
	 */
	protected function renderReview() {
		if (!$this->value) return;
		else if (!$this->isRequired() && $this->isInUploads()) return $this->renderDestuctableReview();
		else return $this->renderIndestructibleReview();
	}

	/**
	 * Check if the file is in the uploads directory. The use case for this arose 
	 * with the Fragments system where the default images would usually be in the 
	 * img directory
	 *
	 * @return boolean
{'fraction_non_alphanumeric': 0.11911094224924013, 'fraction_numerical': 0.00037993920972644377, 'mean_word_length': 3.3584437086092715, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 36, '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': '2627295', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1490, 'n_words': 570}