Skip to content

Instantly share code, notes, and snippets.

@228vit
Created October 22, 2012 10:24
Show Gist options
  • Save 228vit/3930806 to your computer and use it in GitHub Desktop.
Save 228vit/3930806 to your computer and use it in GitHub Desktop.
base form doctrine
abstract class BaseFormDoctrine extends sfFormDoctrine
{
public $contentFields = array();
public function setup()
{
ProjectConfiguration::getActive()->loadHelpers( array( 'Thumbnail', 'Asset' ) );
unset($this['created_at'], $this['updated_at']);
if (isSet($this['published_at']))
{
$years = range(
(int)date('Y')-sfConfig::get('app_date_widget_years_before', 2),
(int)date('Y')+sfConfig::get('app_date_widget_years_after', 2)
); //Creates array of years
$years_list = array_combine($years, $years); //Creates new array where key and value are both values from $years list
$this->setWidget('published_at', new sfWidgetFormI18nDate(
array(
'culture' => 'ru',
'format' => '%day%.%month%.%year%',
'years' => $years_list,
),
array()
));
if ($this->getObject()->isNew())
{
$this->widgetSchema->setDefault('published_at', date('Y-m-d'));
}
} // published at
$announces = array('announce', 'extract', 'note');
foreach ($announces as $announceFieldName)
{
if (isSet($this[$announceFieldName]))
{
$this->widgetSchema[$announceFieldName] = new sfWidgetFormTextarea(array(), array('style' => 'width: 400px; height: 40px;'));
} // announce
} // each announce field
$contents = array('content', 'body', 'description');
$contents = array();
foreach ($contents as $contentFieldName)
{
if (isSet($this[$contentFieldName]))
{
$this->widgetSchema[$contentFieldName] = new sfWidgetFormCKEditor();
$finder = $this->widgetSchema[$contentFieldName]->getFinder();
$editor = $this->widgetSchema[$contentFieldName]->getEditor();
$editor->config['toolbar'] = array(
array('Source','-','Save', '-', 'Cut','Copy','Paste','PasteText','PasteFromWord', '-'),
array('Button', '-', 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','Styles', '-', 'Format','Font','FontSize','TextColor','RemoveFormat', '-'),
array('DefinitionList', 'DefinitionTerm', 'DefinitionDescription', '-', 'NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','-','Blockquote'),
array('Link', 'Unlink', 'Maximize', 'ShowBlocks','-', 'Image'),
);
// ,'Outdent','Indent','-'
$editor->config['height'] = '400px';
} // content
} // each content field
$pics = array('banner', 'logo', 'pic', 'picture', 'thumb', 'img', 'image', 'photo', 'avatar');
foreach ($pics as $picFieldName)
{
if (isSet($this[$picFieldName]))
{
// if (is_callable('getPicDir', true))
if (method_exists($this, 'getPicDir'))
{
$modelName = $this->getPicDir();
}
else if (is_callable('getModelName', true, $modelName))
{
// TODO: fix this, make it editable
$modelName = strtolower($this->getModelName());
$modelName = myConfig::get($modelName.'_dir', $modelName);
} else {
$modelName = false;
}
$uploadDirName = implode('/', array(
sfConfig::get('sf_upload_dir'),
$modelName,
));
$picPath = implode('/', array(
'uploads',
$modelName,
$this->getObject()->get($picFieldName)
));
$this->setWidget($picFieldName, new sfWidgetFormInputFileEditable(array(
'label' => ucfirst($picFieldName),
'file_src' => $picPath,
'is_image' => true,
'edit_mode' => ($this->getObject()->get($picFieldName) AND !$this->getObject()->isNew() ? true: false),
'template' => '<table><tr>
<td>
%input% <br /> %delete% delete file
</td>
<td>
<a rel="extraPics" title="" href="/'.$picPath.'" target="_blank"><img
src="'.thumbnail_path($picPath, 50, 50, $modelName).'" /></a>
</tr>
</table>',
)));
$this->setValidator(
$picFieldName, new sfValidatorFile(array(
'required' => false,
'path' => '/'.$uploadDirName,
'max_size' => '10240000', // bytes (1MB)
'mime_types' => array(
'image/gif',
'image/jpeg',
'image/png'
)
)));
$this->setValidator($picFieldName.'_delete', new sfValidatorPass()); // pic_delete
} // if pic
} // foreach pics
// default labels
$labels = array(
'pic' => 'Картинка',
'picture' => 'Картинка',
'img' => 'Изображение',
'image' => 'Изображение',
'photo' => 'Фото',
'avatar' => 'Аватар',
'published_at' => 'Дата публикации',
'name' => 'Название',
'title' => 'Заголовок',
'content' => 'Содержимое',
'is_published' => 'Опубликовано?',
'is_archive' => 'Архивный?',
'slug' => 'Ссылка',
'extract' => 'Анонс',
'announce' => 'Анонс',
'note' => 'Примечание',
);
foreach ($labels as $fieldName => $label)
{
if (isSet($this[$fieldName]))
{
$this->widgetSchema->setLabel($fieldName, $label);
}
} // foreach ($labels as $fieldName)
//mark required fields
$this->markRequiredFields();
} // setup
public function markRequiredFields()
{
//mark required fields
foreach($this->getFormFieldSchema()->getWidget()->getFields() as $key => $object)
{
$label = $this->getFormFieldSchema()->offsetGet($key)->renderLabelName();
if ($this->validatorSchema[$key])
if ($this->validatorSchema[$key]->getOption('required') == true)
{
$this->widgetSchema->setLabel($key, $label."<span class='required'>*</span>");
if (!in_array($key, $this->contentFields)) // do not set attr "required" to hidden textarea
{
$this->widgetSchema[$key]->setAttribute('required', true);
}
}
} //-- mark required fields
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment