/**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param  sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array('\'required\' => false');
     if ($column->isForeignKey()) {
         $columns = $column->getForeignTable()->getColumns();
         foreach ($columns as $name => $col) {
             if (isset($col['primary']) && $col['primary']) {
                 break;
             }
         }
         $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getForeignTable()->getOption('name'), $column->getForeignTable()->getFieldName($name));
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->table->getOption('name'), $this->table->getFieldName($columnName));
         } else {
             switch ($column->getDoctrineType()) {
                 case 'boolean':
                     $options[] = "'choices' => array('', 1, 0)";
                     break;
                 case 'date':
                 case 'datetime':
                 case 'timestamp':
                     $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDate(array('required' => false))";
                     break;
                 case 'enum':
                     $values = array_combine($column['values'], $column['values']);
                     $options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values));
                     break;
             }
         }
     }
     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
 }
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array();
     if ($column->isForeignKey()) {
         $options[] = sprintf('\'model\' => $this->getRelatedModelName(\'%s\')', $column->getRelationKey('alias'));
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'choices\' => array($this->getObject()->get(\'%s\')), \'empty_value\' => $this->getObject()->get(\'%1$s\')', $column->getFieldName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'string':
                     if ($column['length']) {
                         $options[] = sprintf('\'max_length\' => %s', $column['length']);
                     }
                     if (isset($column['minlength'])) {
                         $options[] = sprintf('\'min_length\' => %s', $column['minlength']);
                     }
                     if (isset($column['regexp'])) {
                         $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']);
                     }
                     break;
                 case 'enum':
                     $options[] = '\'choices\' => ' . $this->arrayExport($column['values']);
                     break;
             }
         }
     }
     // If notnull = false, is a primary or the column has a default value then
     // make the widget not required
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options[] = '\'required\' => false';
     }
     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
 }
Example #3
0
$t->is($column->getName(), 'name');
$t->is($column->getFieldName(), 'name');
$t->is($column->getPhpName(), 'name');
$t->is($column->isNotNull(), true);
$column = new sfDoctrineColumn('test', Doctrine::getTable('Test'));
$t->is($column->getName(), 'test');
$t->is($column->getFieldName(), 'TEST');
$t->is($column->getPhpName(), 'TEST');
$t->is($column->getDoctrineType(), 'string');
$t->is($column->getType(), 'VARCHAR');
$t->is($column->getLength(), 255);
$t->is($column->getSize(), 255);
$t->is($column->hasDefinitionKey('length'), true);
$t->is($column->getDefinitionKey('type'), 'string');
$t->is($column->isNotNull(), false);
// Is not null and has definition key
$column = new sfDoctrineColumn('email', Doctrine::getTable('Test'));
$t->is($column->isNotNull(), true);
$t->is($column->hasDefinitionKey('email'), true);
$t->is($column->getDefinitionKey('email'), true);
// Is primary key
$column = new sfDoctrineColumn('id', Doctrine::getTable('Test'));
$t->is($column->isPrimaryKey(), true);
// Relation/foreign key functions
$column = new sfDoctrineColumn('test_id', Doctrine::getTable('TestRelation'));
$t->is($column->isForeignKey(), true);
$t->is($column->getForeignClassName(), 'Test');
$t->is($column->getForeignTable()->getOption('name'), 'Test');
$t->is($column->getTable()->getOption('name'), 'TestRelation');
// Array access
$t->is($column['type'], 'integer');
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array();
     $isForeignKey = $column->isForeignKey();
     $requiresEm = $isForeignKey || $column->isPrimaryKey();
     if ($isForeignKey) {
         $options[] = sprintf('\'model\' => \'%s\'', $column->getForeignMetadata()->name);
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'string':
                     if ($column['length']) {
                         $options[] = sprintf('\'max_length\' => %s', $column['length']);
                     }
                     if (isset($column['minlength'])) {
                         $options[] = sprintf('\'min_length\' => %s', $column['minlength']);
                     }
                     if (isset($column['regexp'])) {
                         $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']);
                     }
                     break;
                 case 'enum':
                     $values = array_combine($column['values'], $column['values']);
                     $options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values));
                     break;
             }
         }
     }
     // If notnull = false, is a primary or the column has a default value then
     // make the widget not required
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options[] = '\'required\' => false';
     }
     return ($requiresEm ? '$this->em, ' : '') . (count($options) ? sprintf('array(%s)', implode(', ', $options)) : '');
 }
 /**
  * Returns a sfValidator class name for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The name of a subclass of sfValidator
  */
 public function getValidatorClassForColumn($column)
 {
     switch ($column->getDoctrineType()) {
         case 'boolean':
             $validatorSubclass = 'Boolean';
             break;
         case 'string':
             if ($column->getDefinitionKey('email')) {
                 $validatorSubclass = 'Email';
             } elseif ($column->getDefinitionKey('regexp')) {
                 $validatorSubclass = 'Regex';
             } elseif ($column->getTable()->isLinkColumn($column->getName())) {
                 $validatorClass = 'dmValidatorLinkUrl';
             } else {
                 $validatorSubclass = 'String';
             }
             break;
         case 'clob':
         case 'blob':
             $validatorSubclass = 'String';
             break;
         case 'float':
         case 'decimal':
             $validatorSubclass = 'Number';
             break;
         case 'integer':
             $validatorSubclass = 'Integer';
             break;
         case 'date':
             $validatorClass = 'dmValidatorDate';
             break;
         case 'time':
             $validatorSubclass = 'Time';
             break;
         case 'timestamp':
             $validatorSubclass = 'DateTime';
             break;
         case 'enum':
             $validatorSubclass = 'Choice';
             break;
         default:
             $validatorSubclass = 'Pass';
     }
     if ($column->isPrimaryKey() || $column->isForeignKey()) {
         $validatorSubclass = 'DoctrineChoice';
     }
     $validatorClass = isset($validatorClass) ? $validatorClass : sprintf('sfValidator%s', $validatorSubclass);
     $validatorClass = $this->getGeneratorManager()->getConfiguration()->getEventDispatcher()->filter(new sfEvent($this, 'dm.form_generator.validator_class', array('column' => $column)), $validatorClass)->getReturnValue();
     return $validatorClass;
 }
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param  sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array('\'required\' => false');
     $isForeignKey = $column->isForeignKey();
     $requiresEm = $isForeignKey || $column->isPrimaryKey();
     if ($isForeignKey) {
         foreach ($column->getForeignMetadata()->fieldMappings as $name => $fieldMapping) {
             if (isset($fieldMapping['id']) && $fieldMapping['id']) {
                 break;
             }
         }
         $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $column->getForeignMetadata()->name, $column->getName());
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'boolean':
                     $options[] = "'choices' => array('', 1, 0)";
                     break;
                 case 'date':
                     $options[] = "'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false))";
                     break;
                 case 'datetime':
                 case 'timestamp':
                     $options[] = "'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59'))";
                     break;
                 case 'enum':
                     $values = array_combine($column['values'], $column['values']);
                     $options[] = "'choices' => " . $this->arrayExport($values);
                     break;
             }
         }
     }
     return ($requiresEm ? '$this->em, ' : '') . (count($options) ? sprintf('array(%s)', implode(', ', $options)) : '');
 }
 /**
  * Returns a PHP string representing options to pass to a widget for a given column.
  *
  * @param sfDoctrineColumn $column
  *
  * @return string The options to pass to the widget as a PHP string
  */
 public function getWidgetOptionsForColumn($column)
 {
     $options = array();
     if ($column->isForeignKey()) {
         $options[] = sprintf('\'model\' => $this->getRelatedModelName(\'%s\'), \'add_empty\' => %s', $column->getRelationKey('alias'), $column->isNotNull() ? 'false' : 'true');
     } else {
         if ('enum' == $column->getDoctrineType() && is_subclass_of($this->getWidgetClassForColumn($column), 'sfWidgetFormChoiceBase')) {
             $options[] = '\'choices\' => ' . $this->arrayExport(array_combine($column['values'], $column['values']));
         }
     }
     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
 }
 protected function isCkEditorColumn(sfDoctrineColumn $column)
 {
     return false !== strpos(dmArray::get($column->getTable()->getColumnDefinition($column->getName()), 'extra', ''), 'ckeditor');
 }
 /**
  * Get validator options for column
  * 
  * @param   sfDoctrineColumn    $column
  * @param   integer             $indent Indentation value [optional]
  * @return  string  The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column, $indent = 0)
 {
     $type = $column->getDoctrineType();
     $default = isset($this->validatorOptions[$type]) ? $this->validatorOptions[$type] : array();
     $options = array('required' => 'false');
     if ($column->isForeignKey()) {
         $columns = $column->getForeignTable()->getColumns();
         foreach ($columns as $name => $col) {
             if (isset($col['primary']) && $col['primary']) {
                 break;
             }
         }
         $options['model'] = "\$this->getRelatedModelName( '" . $column->getRelationKey('alias') . "' )";
         $options['column'] = "'" . $column->getForeignTable()->getFieldName($name) . "'";
     } else {
         if ($column->isPrimaryKey()) {
             $options['model'] = "'" . $this->table->getOption('name') . "'";
             $options['column'] = "'" . $column->getFieldName() . "'";
         } else {
             if ($type == 'enum') {
                 $values = array_combine($column['values'], $column['values']);
                 $options['choices'] = $this->arrayExport($values);
             }
             $options = array_merge($options, $this->config->getFilterValidatorOptions($this->modelName, $column->getName(), $type, $default));
         }
     }
     $out = array();
     $ni = str_repeat(' ', $indent);
     foreach ($options as $k => $v) {
         if (!is_null($v)) {
             $out[] = $ni . "    '" . $k . "' => " . $v;
         }
     }
     return count($out) ? " array(\n" . implode(",\n", $out) . "\n" . $ni . ") " : '';
 }
 /**
  * Returns a sfWidgetForm class name for a given column.
  *
  * @param  sfDoctrineColumn $column
  * @return string    The name of a subclass of sfWidgetForm
  */
 public function getWidgetClassForColumn($column)
 {
     switch ($column->getDoctrineType()) {
         case 'string':
             $widgetSubclass = 'Text';
             break;
         case 'integer':
             $widgetSubclass = 'Integer';
             break;
         case 'boolean':
             $widgetSubclass = 'Boolean';
             break;
         case 'blob':
         case 'clob':
             $widgetSubclass = 'Text';
             break;
         case 'date':
             $widgetSubclass = 'Date';
             break;
         case 'time':
             $widgetSubclass = 'Time';
             break;
         case 'timestamp':
             $widgetSubclass = 'DateTime';
             break;
         case 'enum':
             $widgetSubclass = 'Choice';
             break;
         default:
             $widgetSubclass = 'Text';
     }
     return sprintf('sfWidgetPagerColumn%s', $widgetSubclass);
 }
 /**
  * Returns a PHP string representing options to pass to a validator for a given column.
  *
  * @param sfDoctrineColumn $column
  * @return string    The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column)
 {
     $options = array();
     if ($column->isForeignKey()) {
         $options[] = sprintf('\'model\' => \'%s\'', $column->getForeignTable()->getOption('name'));
     } else {
         if ($column->isPrimaryKey()) {
             $options[] = sprintf('\'model\' => \'%s\', \'column\' => \'%s\'', $this->modelName, $column->getName());
         } else {
             switch ($column->getDoctrineType()) {
                 case 'string':
                     if ($column['length']) {
                         $options[] = sprintf('\'max_length\' => %s', $column['length']);
                     }
                     if (isset($column['minlength'])) {
                         $options[] = sprintf('\'min_length\' => %s', $column['minlength']);
                     }
                     if (isset($column['regexp'])) {
                         $options[] = sprintf('\'pattern\' => \'%s\'', $column['regexp']);
                     }
                     break;
                 case 'enum':
                     $values = array_combine($column['values'], $column['values']);
                     $options[] = "'choices' => " . str_replace("\n", '', $this->arrayExport($values));
                     break;
             }
         }
     }
     if (!$column->isNotNull() || $column->isPrimaryKey()) {
         $options[] = '\'required\' => false';
     }
     return count($options) ? sprintf('array(%s)', implode(', ', $options)) : '';
 }
 /**
  * Get validator options for column
  * 
  * @param   sfDoctrineColumn    $column
  * @param   integer             $indent Indentation value [optional]
  * @return  string  The options to pass to the validator as a PHP string
  */
 public function getValidatorOptionsForColumn($column, $indent = 0)
 {
     $type = $column->getDoctrineType();
     $options = array();
     if ($column->isForeignKey()) {
         $options['model'] = "\$this->getRelatedModelName( '" . $column->getRelationKey('alias') . "' )";
     } else {
         if ($column->isPrimaryKey()) {
             $options['choices'] = "array( \$this->getObject()->get( '" . $column->getFieldName() . "' ) )";
             $options['empty_value'] = "\$this->getObject()->get( '" . $column->getFieldName() . "' )";
         } else {
             if ($type == 'string') {
                 if ($column['length']) {
                     $options['max_length'] = $column['length'];
                 }
                 if (isset($column['minlength'])) {
                     $options['min_length'] = $column['minlength'];
                 }
                 if (isset($column['regexp'])) {
                     $options['pattern'] = "'" . $column['regexp'] . "'";
                 }
             } else {
                 if ($type == 'enum') {
                     $options['choices'] = $this->arrayExport($column['values']);
                 }
             }
         }
     }
     if (!$column->isNotNull() || $column->isPrimaryKey() || $column->hasDefinitionKey('default')) {
         $options['required'] = 'false';
         if (!$column->isNotNull() && !$column->isPrimaryKey()) {
             $options['empty_value'] = 'null';
         }
     }
     $options = array_merge($options, $this->config->getFormValidatorOptions($this->modelName, $column->getName(), $type, array()));
     $out = array();
     $ni = str_repeat(' ', $indent);
     foreach ($options as $k => $v) {
         if (!is_null($v)) {
             $out[] = $ni . "    '" . $k . "' => " . $v;
         }
     }
     return count($out) ? " array(\n" . implode(",\n", $out) . "\n" . $ni . ") " : '';
 }