<?php namespace ProcessWire;

/**
 * An Inputfield for handling "textarea" form inputs
 * 
 * @property int $rows Number of rows for textarea (default=5)
 * @property int $contentType Content type, applicable when used with FieldtypeTextarea. See FieldtypeTextarea contentType constants (default=contentTypeUnknown)
 *
 */
class InputfieldTextarea extends InputfieldText {

	const defaultRows = 5; 

	public static function getModuleInfo() {
		return array(
			'title' => __('Textarea', __FILE__), // Module Title
			'summary' => __('Multiple lines of text', __FILE__), // Module Summary
			'version' => 103,
			'permanent' => true, 
			);
	}


	public function init() {
		parent::init();
		$this->setAttribute('rows', self::defaultRows); 
		$this->setAttribute('maxlength', 1024*32); 
		$this->set('contentType', FieldtypeTextarea::contentTypeUnknown); 
	}

	public function ___render() {

		$attrs = $this->getAttributes();
		unset($attrs['value'], $attrs['size'], $attrs['type']); 
		if($this->hasFieldtype !== false) unset($attrs['maxlength']); 

		$out = "\n<textarea " . $this->getAttributesString($attrs) . ">" . 
			htmlspecialchars($this->value, ENT_QUOTES, "UTF-8") . 
			"</textarea>";
		return $out; 
	}

	protected function setAttributeValue($value) {
		if($this->maxlength > 0 && $this->hasFieldtype === false) { 
			$value = $this->wire('sanitizer')->textarea($value, array(
				'maxLength' => $this->maxlength, 
				'maxBytes' => $this->maxlength*3, 
				'stripTags' => false, 
				)); 
		} else {
			if(strpos($value, "\r\n") !== false) {
				$value = str_replace("\r\n", "\n", $value); 
			}
		}
		if($this->stripTags) $value = strip_tags($value);
		return trim($value); 
	}

	/**
	 * Render just the value (not input) in text/markup for presentation purposes
	 *
	 * @return string of text or markup where applicable
	 *
	 */
	public function ___renderValue() {
		if($this->contentType == FieldtypeTextarea::contentTypeHTML) {
			$out = "<div class='InputfieldTextareaContentTypeHTML'>" . 
				$this->wire('sanitizer')->purify($this->attr('value')) . "</div>";
		} else {
			$out = nl2br(htmlentities($this->attr('value'), ENT_QUOTES, "UTF-8"));
		}	
		return $out;
	}


	public function ___getConfigInputfields() {
		$inputfields = parent::___getConfigInputfields();
		$inputfields->remove($inputfields->get('size')); // size is not applicable to textarea
		$inputfields->remove($inputfields->get('pattern')); // pattern is not applicable to textarea
		if($this->hasFieldtype !== false) $inputfields->remove($inputfields->get('maxlength')); 

		$field = $this->modules->get('InputfieldInteger'); 
		$field->setAttribute('name', 'rows'); 
		$field->label = $this->_('Rows');
		$field->setAttribute('value', $this->attr('rows') > 0 ? $this->attr('rows') : self::defaultRows); 
		$field->setAttribute('size', 3); 
		$field->description = $this->_('The number of rows initially shown for this field.'); 
		if($field->attr('value') == self::defaultRows) $field->collapsed = Inputfield::collapsedYes; 
		$inputfields->append($field);

		return $inputfields; 
	}
	
	public function ___getConfigAllowContext($field) {
		return array_merge(parent::___getConfigAllowContext($field), array('rows')); 
	}
	
}
