<commit_before><?php

namespace Yaim\Mitter;

use Illuminate\Support\Facades\Form;

class FormBuilder
{
	protected $structure;
	protected $apiController;
	protected $html;
	protected $oldData = null;
	protected $isDeleted;

	public function __construct($structure, $apiController = null, $oldData = null, $id = null )
	{
		$this->structure = $structure;
		$this->apiController = $apiController;
		$this->oldData = $oldData;
		$this->isDeleted = (isset($oldData['deleted_at'])) ? true : false;
		$this->id = $id;

		$this->formPrefix();

		if (isset($structure['self'])) {
			$this->formContent($structure['self']);
		}

		if (isset($structure['relations'])) {
			$this->formContent($structure['relations']);
		}

		$this->formPostfix();
	}

	public function formPrefix()
	{
		$structure = $this->structure;

		$this->html ='
			<div class="row">
				<div class="col-xs-6">
					<h2>'.$structure['title'].'</h2>
				</div>';

		if ($this->isDeleted) {
			$this->html .= Form::open(['action' => [$structure['controller']."@store", $this->id]]);
			$this->html .='
					<div class="col-xs-6">
						<input type="submit" class="btn btn-info pull-right" value="Restore Item">
					</div>';
			$this->html .= Form::close();

			$formTitle = 'Deleted '.$structure['title'];
		} elseif (isset($this->id)) {
			$this->html .= Form::open(['action' => [$structure['controller']."@destroy", $this->id], 'method' => 'delete']);
			$this->html .='
					<div class="col-xs-6">
						<input type="submit" class="btn btn-danger pull-right" onclick="return confirm(`Deleting this item means other models\'s relations to this item, wouldn\'t work anymore! Are you sure you want to delete this?`); return false;" value="Delete Item">
					</div>';
			$this->html .= Form::close();

			$formTitle = 'Edit '.$structure['title'];
		} else {
			$formTitle = 'Add '.$structure['title'];
		}

		$this->html .='
			</div>';

		$this->html .= Form::open(['action' => $structure['controller']."@store", 'parsley-validate', 'novalidate']);

		if (isset($this->id)) {
			$this->html .="<input type='hidden' name='id' value='$this->id'>";
		}

		$this->html .='
				<div class="row">
					<div class="col-sm-12 col-md-12">
						<div class="row">
							<h3 class="col-xs-6">'.$formTitle.'</h3>
						</div>
						<div class="tab-container block-web">';
	}

	public function formContent($structure)
	{
		foreach ($structure as $name => $field) {
			$title = $field['title'];

			if(isset($field['type'])) {
				if ($field['type'] == "divider") {
					$this->divider($title);
					continue;
				}
			}

			$oldData = (isset($this->oldData[snake_case($name)]))? $this->oldData[snake_case($name)] : false;
			$repeat = (isset($field['repeat']))? $field['repeat'] : false;

			if($oldData) {
				if($repeat) {
					$count = count($oldData);
					$i = 1;
					$repeat = false;

					if (@$field['type'] != "locked") {
						$this->html .= '<input type="hidden" name="'.$name.'" value="1" data-hidden-placeholder/>';
					}

					foreach ($oldData as $data) {
						if($i == $count)
							$repeat = true;

						$this->rowPrefix($title, $name, $repeat);
						$this->rowContent($name, $field, true, $data, $num = $i);
						$this->rowPostfix(true);

						$i++;
					}
					$this->html .='<hr/>';
				} else {
					$this->rowPrefix($title, $name, $repeat);
					$this->rowContent($name, $field, false, $oldData);
					$this->rowPostfix();
				}
			} else {
				$this->rowPrefix($title, $name, $repeat);
				$this->rowContent($name, $field, $repeat);
				$this->rowPostfix();
			}
		}
	}

	public function formPostfix()
	{
		$structure = $this->structure;

		$this->html .='
				</div>
			</div>
		</div>';

		$this->html .= Form::submit("Submit ".$structure['title'], array('class' => 'btn btn-primary'));

		if($structure['model'] == "Manufacture\Production") {
			$this->html .= ' <a class="btn btn-success" href="generate-map">Generate Production Map</a>';
			$this->html .= ' <a class="btn btn-danger" onClick="if(confirm(`Deleting this production\'s stock types means that no other relations to these stock types, would work anymore! Are you sure you want to delete these stock types?`))alert(`You are very brave!`);else {alert(`A wise decision!`); return false;}" href="delete-stock-types">Delete Stock Types</a>';
		}

		if($structure['model'] == "Warehouse\StockType") {
			$this->html .= ' <a class="btn btn-danger" href="update-stocks">Update Stocks</a>';
		}

		$this->html .= Form::close();
	}

	public function rowPrefix($title, $name, $repeat = false)
	{
		$extraInputs = '';
		$extraAttributes = '';

		if ($repeat) {
			$extraAttributes .=" data-repeat data-name='$name' ";
		}<commit_msg>Adding links to structure<commit_after><?php

namespace Yaim\Mitter;

use Illuminate\Support\Facades\Form;

class FormBuilder
{
	protected $structure;
	protected $apiController;
	protected $html;
	protected $oldData = null;
	protected $isDeleted;

	public function __construct($structure, $apiController = null, $oldData = null, $id = null )
	{
		$this->structure = $structure;
		$this->apiController = $apiController;
		$this->oldData = $oldData;
		$this->isDeleted = (isset($oldData['deleted_at'])) ? true : false;
		$this->id = $id;

		$this->formPrefix();

		if (isset($structure['self'])) {
			$this->formContent($structure['self']);
		}

		if (isset($structure['relations'])) {
			$this->formContent($structure['relations']);
		}

		$this->formPostfix();
	}

	public function formPrefix()
	{
		$structure = $this->structure;

		$this->html ='
			<div class="row">
				<div class="col-xs-6">
					<h2>'.$structure['title'].'</h2>
				</div>';

		if ($this->isDeleted) {
			$this->html .= Form::open(['action' => [$structure['controller']."@store", $this->id]]);
			$this->html .='
					<div class="col-xs-6">
						<input type="submit" class="btn btn-info pull-right" value="Restore Item">
					</div>';
			$this->html .= Form::close();

			$formTitle = 'Deleted '.$structure['title'];
		} elseif (isset($this->id)) {
			$this->html .= Form::open(['action' => [$structure['controller']."@destroy", $this->id], 'method' => 'delete']);
			$this->html .='
					<div class="col-xs-6">
						<input type="submit" class="btn btn-danger pull-right" onclick="return confirm(`Deleting this item means other models\'s relations to this item, wouldn\'t work anymore! Are you sure you want to delete this?`); return false;" value="Delete Item">
					</div>';
			$this->html .= Form::close();

			$formTitle = 'Edit '.$structure['title'];
		} else {
			$formTitle = 'Add '.$structure['title'];
		}

		$this->html .='
			</div>';

		$this->html .= Form::open(['action' => $structure['controller']."@store", 'parsley-validate', 'novalidate']);

		if (isset($this->id)) {
			$this->html .="<input type='hidden' name='id' value='$this->id'>";
		}

		$this->html .='
				<div class="row">
					<div class="col-sm-12 col-md-12">
						<div class="row">
							<h3 class="col-xs-6">'.$formTitle.'</h3>
						</div>
						<div class="tab-container block-web">';
	}

	public function formContent($structure)
	{
		foreach ($structure as $name => $field) {
			$title = $field['title'];

			if(isset($field['type'])) {
				if ($field['type'] == "divider") {
					$this->divider($title);
					continue;
				}
			}

			$oldData = (isset($this->oldData[snake_case($name)]))? $this->oldData[snake_case($name)] : false;
			$repeat = (isset($field['repeat']))? $field['repeat'] : false;

			if($oldData) {
				if($repeat) {
					$count = count($oldData);
					$i = 1;
					$repeat = false;

					if (@$field['type'] != "locked") {
						$this->html .= '<input type="hidden" name="'.$name.'" value="1" data-hidden-placeholder/>';
					}

					foreach ($oldData as $data) {
						if($i == $count)
							$repeat = true;

						$this->rowPrefix($title, $name, $repeat);
						$this->rowContent($name, $field, true, $data, $num = $i);
						$this->rowPostfix(true);

						$i++;
					}
					$this->html .='<hr/>';
				} else {
					$this->rowPrefix($title, $name, $repeat);
					$this->rowContent($name, $field, false, $oldData);
					$this->rowPostfix();
				}
			} else {
				$this->rowPrefix($title, $name, $repeat);
				$this->rowContent($name, $field, $repeat);
				$this->rowPostfix();
			}
		}
	}

	public function formPostfix()
	{
		$structure = $this->structure;

		$this->html .='
				</div>
			</div>
		</div>';

		$this->html .= Form::submit("Submit ".$structure['title'], array('class' => 'btn btn-primary'));

		if(isset($structure['links'])) {
			$this->html .= $structure['links'];
		}

		$this->html .= Form::close();
	}

	public function rowPrefix($title, $name, $repeat = false)
	{
		$extraInputs = '';
		$extraAttributes = '';

		if ($repeat) {
			$extraAttributes .=" data-repeat data-name='$name' ";
		}
{'fraction_non_alphanumeric': 0.22378180988246035, 'fraction_numerical': 0.0031952527673171287, 'mean_word_length': 3.56696195935383, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 165, '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': '795599', 'n_tokens_mistral': 3546, 'n_tokens_neox': 2962, 'n_words': 605}