Example #1
0
 /**
  * @param Field $field
  * @throws DuplicateFieldException
  */
 public function addField(Field $field)
 {
     if (isset($this->fields[$field->getName()])) {
         throw new DuplicateFieldException('Field `' . $field->getName() . '` already exists');
     }
     $this->fields[$field->getName()] = $field;
 }
Example #2
0
 public function removeField(Field $field)
 {
     if (isset($this->fields[$field->getName()])) {
         unset($this->fields[$field->getName()]);
     }
     return $this;
 }
Example #3
0
 /**
  * Renders a non select field.
  *
  * @return string (X)HTML.
  */
 protected function renderNonSelectField()
 {
     $o = '';
     if (function_exists('advfrm_custom_field_default')) {
         $val = advfrm_custom_field_default($this->form, $this->field->getName(), null, isset($_POST['advfrm']));
     }
     if (!isset($val)) {
         $val = isset($_POST[$this->name]) ? stsl($_POST[$this->name]) : $this->field->getDefaultValue();
     }
     if ($this->field->getType() == 'textarea') {
         $cols = $this->field->getColumnCount() ? $this->field->getColumnCount() : 40;
         $rows = $this->field->getRowCount() ? $this->field->getRowCount() : 4;
         $o .= '<textarea id="' . $this->id . '" name="' . $this->name . '" cols="' . $cols . '" rows="' . $rows . '">' . XH_hsc($val) . '</textarea>';
     } elseif ($this->field->getType() == 'output') {
         $o .= $val;
     } else {
         if ($this->field->getType() == 'date') {
             $this->initDatePicker();
         }
         $size = $this->field->getType() == 'hidden' || $this->field->getSize() ? ' size="' . $this->field->getSize() . '"' : '';
         $maxlen = in_array($this->field->getType(), array('hidden', 'file')) || !$this->field->getMaxLength() ? '' : ' maxlength="' . $this->field->getMaxLength() . '"';
         if ($this->field->getType() == 'file' && $this->field->getMaxLength()) {
             $o .= tag('input type="hidden" name="MAX_FILE_SIZE" value="' . $this->field->getMaxLength() . '"');
         }
         if ($this->field->getType() == 'file') {
             $value = '';
             $accept = ' accept="' . XH_hsc($this->prefixFileExtensionList($val)) . '"';
         } else {
             $value = ' value="' . XH_hsc($val) . '"';
             $accept = '';
         }
         $o .= tag('input type="' . $this->getInputElementType() . '" id="' . $this->id . '" name="' . $this->name . '"' . $value . $accept . $size . $maxlen);
     }
     return $o;
 }
Example #4
0
 /**
  * Add field
  * 
  * @param  Field $field
  * @throws \LogicException
  */
 public function addField(Field $field)
 {
     $name = $field->getName();
     if (isset($this->fields[$name])) {
         throw new \LogicException(sprintf('Field "%s" already exists', $name));
     }
     $this->fields[$name] = $field;
 }
 /**
  * Validates a filled in field wrt. custom constraints.
  *
  * @return string (X)HTML.
  */
 protected function validateFilledInFieldCustom()
 {
     $o = '';
     if (function_exists('advfrm_custom_valid_field')) {
         $value = $this->field->getType() == 'file' ? $_FILES[$this->name] : stsl($_POST[$this->name]);
         $valid = advfrm_custom_valid_field($this->formId, $this->field->getName(), $value);
         if ($valid !== true) {
             $o .= '<li>' . $valid . '</li>' . PHP_EOL;
             Controller::focusField($this->formId, $this->name);
         }
     }
     return $o;
 }
 /**
  * Renders a field.
  *
  * @param Field $field A field.
  *
  * @return string (X)HTML
  */
 protected function renderField(Field $field)
 {
     $name = 'advfrm-' . $field->getName();
     $o = '<tr><td class="label">' . XH_hsc($field->getLabel()) . '</td><td class="field">';
     if (isset($_POST[$name])) {
         if (is_array($_POST[$name])) {
             foreach ($_POST[$name] as $val) {
                 $o .= '<div>' . XH_hsc(stsl($val)) . '</div>';
             }
         } else {
             $o .= $this->nl2br(XH_hsc(stsl($_POST[$name])));
         }
     } elseif (isset($_FILES[$name])) {
         $o .= stsl($_FILES[$name]['name']);
     }
     $o .= '</td></tr>' . PHP_EOL;
     return $o;
 }
 public function testField()
 {
     $f = new Field('test', 1);
     $this->assertEquals('test', $f->getName());
     $this->assertEquals(1, $f->getTable());
     $f = new AllFields();
     $this->assertEquals(0, $f->getTable());
     $f = new AllFields(2);
     $this->assertEquals(2, $f->getTable());
     try {
         $f = new Field(array('1'));
         $this->fail('first parameter should be string (array is not allowed)');
     } catch (InvalidArgumentException $e) {
     }
     try {
         $f = new Field('foo', 'bar');
         $this->fail('second parameter has to be numeric');
     } catch (InvalidArgumentException $e) {
     }
 }
 /**
  * Adds a field.
  *
  * @param Field $field A field.
  */
 public function add(Field $field)
 {
     $this->fields[$field->getName()] = $field;
 }
Example #9
0
 /**
  * Update field in storage, sets hashes for field and it's model
  * @param Field $field
  */
 public function updateFieldSync($field)
 {
     $this->query('update [fields] set [hash] = %s where [name] = %s and [table] = %s', $field->getHash(), $field->getName(), $field->getModel()->GetTableName());
     $this->updateModelSync($field->getModel());
 }
 public function validate(Field $field)
 {
     $l = strlen($field->getValue());
     if ($l < $this->min) {
         return $field->addError(sprintf($this->too_short, $field->getName(), $this->min));
     } elseif ($l > $this->max) {
         return $field->addError(sprintf($this->too_long, $field->getName(), $this->max));
     }
 }
Example #11
0
 public function addField(Field $field)
 {
     $this->fields[$field->getName()] = $field;
     return $this;
 }
Example #12
0
 /**
  * @param Field  $field
  * @param string $operation
  * @param string $value
  */
 public function __construct(Field $field, $operation, $value)
 {
     $this->field = $field->getName();
     $this->operation = $operation;
     $this->value = $value;
 }
Example #13
0
 protected static function renderButtonInput(Field $field)
 {
     return sprintf('<button type="%s" name="%s" value="%s" %s>%s</button>', $field->getType(), $field->getName(), $field->getValue(), $field->getAttributes(), $field->getLabel());
 }
Example #14
0
 /**
  * Adds field to model
  * @param string $fieldName
  * @param Field $field
  */
 protected function attachField($field, $toBeginning = false)
 {
     if ($this->isFrozen()) {
         throw new Exception("Unable to attach field '" . $field->getName() . "' to frozen model.");
     }
     if (key_exists($field->getName(), $this->fields)) {
         throw new Exception("Field with name '" . $field->getName() . "' already exists in model '" . get_class($this) . "'");
     }
     if ($toBeginning) {
         $this->fields = array($field->getName() => $field) + $this->fields;
     } else {
         $this->fields[$field->getName()] = $field;
     }
     //$this->fields[$field->getName()]->setModel($this);
     return $this->fields[$field->getName()];
 }
 /**
  * Add a field.
  *
  * @param Field|string $field The form field.
  *
  * @return Field
  */
 public function addField($field)
 {
     if (!$field instanceof Field) {
         $field = new Field($field);
     }
     $this->fields[$field->getName()] = $field;
     return $field;
 }
Example #16
0
 /**
  * Performs validation
  *
  * The validation fail if this SQL returns a result:
  * <code>
  * SELECT ${FIELD.NAME} FROM ${TABLE} WHERE ${FIELD.NAME}=${FIELD.VALUE} AND ${ROW.PRIMARY_KEY.NAME}!=${ROW.PRIMARY_KEY.VALUE}
  * </code>
  * 
  * @see Validator::validates
  */
 public function validate(Field $field)
 {
     $supp = $this->record->getPrimaryKey()->getValue() === NULL ? '' : ' and ' . $this->record->getPrimaryKey()->getName() . '!=?';
     try {
         ActiveRecord::build(new QueryBuilder($this->record->getClassName(), array('first', array('condition' => $field->getName() . '=?' . $supp, 'columns' => $field->getName()), array($field->getValue(), $this->record->getPrimaryKey()->getValue()))));
         $field->addError(sprintf($this->message, $field->getFormattedName()));
     } catch (RecordNotFoundException $rnfEx) {
     }
 }
 public function getAttributes(Field $field)
 {
     return $this->getConfigurationFor($field->getDocument())->findAttributes($field->getName());
 }
 public static function fieldComparator(Field $f1, Field $f2)
 {
     return strcmp($f1->getName(), $f2->getName());
 }
Example #19
0
 /**
  * Adds the current datatype to the associated table.
  * @param string $after Name of column to place new column after. Null
  * puts new column at the end of the table. 'FIRST' makes it the first column.
  */
 public function add($after = null)
 {
     if (!empty($after)) {
         if ($after !== 'FIRST') {
             $field = new Field($this->table, $after);
             $after = 'AFTER ' . $field->getName();
         }
     }
     $query = 'ALTER TABLE ' . $this->table->getFullName() . ' ADD COLUMN ' . $this->__toString() . ' ' . $after;
     return $this->table->db->exec($query);
 }
Example #20
0
/**
 * User defined callback function if field validation fails.
 *
 * @param Field $f field object with invalid content
 */
function request_error_callback($f)
{
    echo "Invalid value: " . $f->getName();
}
Example #21
0
 public function testGetName()
 {
     $crud = new LouisCRUD();
     $field = new Field($crud, "product_name", "varchar(255)");
     $this->assertEquals("product_name", $field->getName());
 }
Example #22
0
 /**
  * FormHandler::fieldExists()
  *
  * Check if the field exists in the form
  *
  * @param Field|string $field The field to check if it exists in the form or not
  * @return boolean
  * @author Teye Heimans
  * @author Marien den Besten
  */
 public function fieldExists($field)
 {
     $name = $field instanceof Field\Field ? $field->getName() : $field;
     return array_key_exists($name, $this->fields);
 }
Example #23
0
 /**
  * @param Field  $field
  * @param string $sort_type
  */
 public function __construct(Field $field, $sort_type)
 {
     $this->field = $field->getName();
     $this->sort_type = $sort_type;
 }
Example #24
0
 /**
  * @param string $stmt
  * @return $this
  */
 public function parse($stmt)
 {
     $lines = array_map('trim', explode(PHP_EOL, $stmt));
     $this->first = array_shift($lines);
     $last = array_pop($lines);
     //remove AUTO_INCREMENT bit from raw statement
     $this->last = preg_replace('/AUTO_INCREMENT=\\d+ ?/', '', $last);
     $this->statement = str_replace($last, $this->last, $stmt);
     if ($this->name === null) {
         if (!preg_match('/`([^`]+)/', $this->first, $match)) {
             throw new \RuntimeException(sprintf('Unable to extract name from %s (looked at line %s)', $stmt, $lines[0]));
         }
         $this->name = $match[1];
     }
     foreach ($lines as $ln) {
         if (mb_substr($ln, -1) == ',') {
             $ln = mb_substr($ln, 0, -1);
         }
         //field lines start with back-tick
         switch ($ln[0]) {
             case '`':
                 $field = new Field($ln);
                 $this->fields[$field->getName()] = $field;
                 break;
             case 'P':
                 $this->primary = new Primary($ln);
                 break;
             case 'S':
                 //spatial
             //spatial
             case 'U':
                 //unique
             //unique
             case 'F':
                 //fulltext
             //fulltext
             case 'I':
                 //index
             //index
             case 'K':
                 //Key
                 $idx = new Index($ln);
                 $this->indexes[$idx->getName()] = $idx;
                 break;
             case 'C':
                 $constraint = new ForeignKey($ln);
                 $this->constraints[$constraint->getName()] = $constraint;
                 break;
             default:
                 throw new \LogicException(sprintf('Unable to parse line %s in %s', $ln, $stmt));
         }
     }
     return $this;
 }
Example #25
0
 public function addField($name, $message = '')
 {
     $field = new Field($name, $message);
     $this->fields[$field->getName()] = $field;
 }
Example #26
0
 /**
  * @param Field $field
  */
 public function addField(Field $field)
 {
     $name = $field->getName();
     if ($this->hasField($name)) {
         throw new \InvalidArgumentException(sprintf('Field "%s" already exists.', $name));
     }
     $this->fields[$name] = $field;
 }
Example #27
0
 /**
  * Returns TRUE whether the given field is contained in the fields collection
  *
  * @param Field $field
  *
  * @return boolean
  */
 public function hasField(Field $field)
 {
     return array_key_exists($field->getName(), $this->fields);
 }
Example #28
0
 public function add(Field $field)
 {
     $name = $field->getName();
     $this->fields[$name] = $field;
 }
Example #29
0
 /**
  * Remove data field.
  *
  * @param Field $field Data field.
  */
 public function removeField(Field $field)
 {
     if (isset($this->fields[$field->getName()])) {
         unset($this->fields[$field->getName()]);
         $this->length--;
     }
 }
 /**
  * Renders a field.
  *
  * @param Field $field A field.
  *
  * @return string
  */
 protected function renderField(Field $field)
 {
     $name = 'advfrm-' . $field->getName();
     $o = $field->getLabel() . PHP_EOL;
     if (isset($_POST[$name])) {
         if (is_array($_POST[$name])) {
             foreach ($_POST[$name] as $val) {
                 $o .= '  ' . stsl($val) . PHP_EOL;
             }
         } else {
             $o .= '  ' . $this->indent(stsl($_POST[$name])) . PHP_EOL;
         }
     } elseif (isset($_FILES[$name])) {
         $o .= '  ' . stsl($_FILES[$name]['name']) . PHP_EOL;
     }
     return $o;
 }