This commit is contained in:
buddha87 2016-08-04 23:18:23 +02:00
commit e182753552
4 changed files with 119 additions and 43 deletions

View File

@ -20,7 +20,7 @@ class HForm extends \yii\base\Component
const EVENT_BEFORE_VALIDATE = 'beforeValidate';
const EVENT_AFTER_VALIDATE = 'afterValidate';
public $showErrorSummary;
protected $form;
public $primaryModel = null;
@ -64,7 +64,7 @@ class HForm extends \yii\base\Component
{
$hasErrors = false;
$this->trigger(self::EVENT_BEFORE_VALIDATE);
if ($this->primaryModel !== null) {
if (!$this->primaryModel->validate()) {
$hasErrors = true;
@ -76,7 +76,7 @@ class HForm extends \yii\base\Component
$hasErrors = true;
}
}
$this->trigger(self::EVENT_AFTER_VALIDATE);
return !$hasErrors;
}
@ -196,16 +196,16 @@ class HForm extends \yii\base\Component
if (isset($definition['id'])) {
$options['id'] = $definition['id'];
}
if (isset($definition['readonly']) && $definition['readonly']) {
$options['readOnly'] = true;
$options['disabled'] = true;
}
if (isset($definition['value'])) {
$options['value'] = $definition['value'];
}
if (isset($definition['prompt']) && $definition['prompt']) {
$options['prompt'] = $definition['prompt'];
}
@ -213,14 +213,13 @@ class HForm extends \yii\base\Component
$options['label'] = $definition['label'];
}
if (isset($definition['type'])) {
switch($definition['type']) {
switch ($definition['type']) {
case 'text':
return $this->form->field($model, $name)->textInput($options);
case 'multiselectdropdown':
$options['class'] = 'form-control multiselect_dropdown';
$options['multiple'] = 'multiple';
return $this->form->field($model, $name)->listBox($definition['items'], $options);
//return $this->form->field($model, $name)->dropDownList($definition['items'], $options);
case 'dropdownlist':
return $this->form->field($model, $name)->dropDownList($definition['items'], $options);
case 'checkbox':
@ -239,14 +238,19 @@ class HForm extends \yii\base\Component
if (isset($definition['format'])) {
$format = $definition['format'];
}
$yearRange = isset($definition['yearRange']) ? $definition['yearRange'] : (date('Y') - 100) . ":" . (date('Y') + 100);
return $this->form->field($model, $name)->widget(\yii\jui\DatePicker::className(), [
'dateFormat' => $format,
'clientOptions' => ['changeYear' => true, 'yearRange' => $yearRange, 'changeMonth' => true, 'disabled' => (isset($options['readOnly']) && $options['readOnly'])],
'options' => ['class' => 'form-control']]);
default:
'dateFormat' => $format,
'clientOptions' => ['changeYear' => true, 'yearRange' => $yearRange, 'changeMonth' => true, 'disabled' => (isset($options['readOnly']) && $options['readOnly'])],
'options' => ['class' => 'form-control']]);
case 'markdown':
$options['id'] = $name;
$returnField = $this->form->field($model, $name)->textarea($options);
$returnField .= \humhub\widgets\MarkdownEditor::widget(array('fieldId' => $name));
return $returnField;
default:
return "Field Type " . $definition['type'] . " not supported by Compat HForm";
}
} else {

View File

@ -10,6 +10,7 @@ namespace humhub\modules\user\models\fieldtype;
use Yii;
use humhub\modules\user\models\Profile;
use humhub\modules\user\models\fieldtype;
/**
* ProfileFieldType is the base class for all Profile Field Types.
@ -82,6 +83,7 @@ class BaseType extends \yii\base\Model
DateTime::className() => Yii::t('UserModule.models_ProfileFieldType', 'Datetime'),
Birthday::className() => Yii::t('UserModule.models_ProfileFieldType', 'Birthday'),
CountrySelect::className() => Yii::t('UserModule.models_ProfileFieldType', 'Country'),
MarkdownEditor::className() => Yii::t('UserModule.models_ProfileFieldType', 'Markdown'),
), $this->fieldTypes);
return $fieldTypes;
}

View File

@ -0,0 +1,71 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2016 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\user\models\fieldtype;
use Yii;
/**
* Markdown Profile Field
*
* @since 1.1
*/
class MarkdownEditor extends BaseType
{
/**
* @inheritdoc
*/
public function getFormDefinition($definition = array())
{
return parent::getFormDefinition(array(
get_class($this) => array(
'type' => 'form',
'title' => Yii::t('UserModule.models_ProfileFieldTypeTextArea', 'Text area field options'),
'elements' => array(
)
)));
}
/**
* @inheritdoc
*/
public function save()
{
$columnName = $this->profileField->internal_name;
if (!\humhub\modules\user\models\Profile::columnExists($columnName)) {
$query = Yii::$app->db->getQueryBuilder()->addColumn(\humhub\modules\user\models\Profile::tableName(), $columnName, 'TEXT');
Yii::$app->db->createCommand($query)->execute();
}
return parent::save();
}
/**
* @inheritdoc
*/
public function getFieldRules($rules = array())
{
$rules[] = array($this->profileField->internal_name, 'safe');
return parent::getFieldRules($rules);
}
/**
* @inheritdoc
*/
public function getFieldFormDefinition()
{
return array($this->profileField->internal_name => array(
'type' => 'markdown',
'class' => 'form-control',
'rows' => '3'
));
}
}
?>

View File

@ -1,59 +1,58 @@
<?php
use yii\helpers\Html;
use yii\helpers\Html;
use humhub\widgets\RichText;
use humhub\modules\user\models\fieldtype\MarkdownEditor;
use humhub\widgets\MarkdownView;
?>
<div class="panel panel-default">
<div
class="panel-heading"><?php echo Yii::t('UserModule.views_profile_about', '<strong>About</strong> this user'); ?></div>
<div class="panel-body">
<?php $firstClass = "active"; ?>
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
<?php foreach ($user->profile->getProfileFieldCategories() as $category): ?>
<li class="<?php echo $firstClass;
$firstClass = ""; ?>"><a href="#profile-category-<?php echo $category->id; ?>"
data-toggle="tab"><?php echo Html::encode(Yii::t($category->getTranslationCategory(), $category->title)); ?></a>
<li class="<?php echo $firstClass; ?>">
<a href="#profile-category-<?php echo $category->id; ?>" data-toggle="tab"><?php echo Html::encode(Yii::t($category->getTranslationCategory(), $category->title)); ?></a>
</li>
<?php endforeach; ?>
<?php
$firstClass = "";
endforeach;
?>
</ul>
<?php $firstClass = "active"; ?>
<div class="tab-content">
<?php foreach ($user->profile->getProfileFieldCategories() as $category): ?>
<div class="tab-pane <?php echo $firstClass;
$firstClass = ""; ?>" id="profile-category-<?php echo $category->id; ?>">
<div class="tab-pane <?php
echo $firstClass;
$firstClass = "";
?>" id="profile-category-<?php echo $category->id; ?>">
<form class="form-horizontal" role="form">
<?php foreach ($user->profile->getProfileFields($category) as $field) : ?>
<div class="form-group">
<label
class="col-sm-3 control-label"><?php echo Html::encode(Yii::t($field->getTranslationCategory(), $field->title)); ?></label>
<?php if (strtolower($field->title) == 'about') { ?>
<label class="col-sm-3 control-label">
<?php echo Html::encode(Yii::t($field->getTranslationCategory(), $field->title)); ?>
</label>
<?php if (strtolower($field->title) == 'about'): ?>
<div class="col-sm-9">
<p class="form-control-static"><?php echo humhub\widgets\RichText::widget(['text' => $field->getUserValue($user, true)]); ?></p>
<p class="form-control-static"><?php echo RichText::widget(['text' => $field->getUserValue($user, true)]); ?></p>
</div>
<?php } else { ?>
<?php else: ?>
<div class="col-sm-9">
<p class="form-control-static"><?php echo $field->getUserValue($user, false); ?></p>
<?php if ($field->field_type_class == MarkdownEditor::className()): ?>
<p class="form-control-static" style="min-height: 0 !important;padding-top:0;">
<?= MarkdownView::widget(['markdown' => $field->getUserValue($user, false)]); ?>
</p>
<?php else: ?>
<p class="form-control-static"><?php echo $field->getUserValue($user, false); ?></p>
<?php endif; ?>
</div>
<?php } ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</form>
</div>
<?php endforeach; ?>
</div>
</div>
</div>