add webcam to customer create/update
This commit is contained in:
31
frontend/assets/CustomerAsset.php
Normal file
31
frontend/assets/CustomerAsset.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace frontend\assets;
|
||||
|
||||
use yii\web\AssetBundle;
|
||||
|
||||
/**
|
||||
* @author Qiang Xue <qiang.xue@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class CustomerAsset extends AssetBundle
|
||||
{
|
||||
public $basePath = '@webroot';
|
||||
public $baseUrl = '@web';
|
||||
public $css = [
|
||||
];
|
||||
public $js = [
|
||||
'js/customer.js',
|
||||
];
|
||||
public $depends = [
|
||||
'frontend\assets\AppAsset',
|
||||
'common\assets\MomentAsset',
|
||||
'common\assets\WebcamjsAsset',
|
||||
'yii\jui\JuiAsset',
|
||||
];
|
||||
}
|
||||
@@ -13,6 +13,7 @@ use yii\base\Object;
|
||||
use common\models\Card;
|
||||
use frontend\models\CustomerUpdate;
|
||||
use frontend\models\CustomerCreate;
|
||||
use common\models\Image;
|
||||
|
||||
/**
|
||||
* CustomerController implements the CRUD actions for Customer model.
|
||||
@@ -119,6 +120,7 @@ class CustomerController extends Controller
|
||||
}
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
||||
$this->saveBinaryImage($model);
|
||||
\Yii::$app->session->setFlash( 'success','Vendég létrehozva!' );
|
||||
return $this->redirect(['update', 'number' => $model->cardNumber]);
|
||||
} else {
|
||||
@@ -160,6 +162,9 @@ class CustomerController extends Controller
|
||||
|
||||
if ($model->load(Yii::$app->request->post()) && $model->save()) {
|
||||
|
||||
$this->saveBinaryImage($model);
|
||||
|
||||
|
||||
\Yii::$app->session->setFlash( 'success','Vendég módosításai elmentve' );
|
||||
|
||||
return $this->redirect(['update', 'number' => $card->number]);
|
||||
@@ -175,6 +180,24 @@ class CustomerController extends Controller
|
||||
}
|
||||
|
||||
|
||||
protected function saveBinaryImage($model){
|
||||
if ( !empty($model->photo_data)){
|
||||
$encoded_data = $model->photo_data;
|
||||
$binary_data = base64_decode( $encoded_data );
|
||||
// save to server (beware of permissions)
|
||||
$path = \common\components\Image::saveBinary($binary_data,'profile');
|
||||
|
||||
$image = new Image();
|
||||
$image->path = $path;
|
||||
$image->save();
|
||||
|
||||
//todo delete old image
|
||||
|
||||
$model->id_image = $image->id_image;
|
||||
$model->save(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an existing Customer model.
|
||||
* If deletion is successful, the browser will be redirected to the 'index' page.
|
||||
|
||||
@@ -100,6 +100,7 @@ class CustomerCreate extends \common\models\Customer
|
||||
[['zip'], 'string', 'max' => 8],
|
||||
|
||||
[['city'], 'string', 'max' => 30],
|
||||
[['photo_data'] ,'safe']
|
||||
|
||||
// [['email','phone'], 'validateEmailOrPhoneRequired' ],
|
||||
];
|
||||
|
||||
@@ -100,7 +100,8 @@ class CustomerUpdate extends \common\models\Customer
|
||||
|
||||
[['zip'], 'string', 'max' => 8],
|
||||
|
||||
[['city'], 'string', 'max' => 30]
|
||||
[['city'], 'string', 'max' => 30],
|
||||
[['photo_data'] ,'safe']
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
29
frontend/views/customer/_camera.php
Normal file
29
frontend/views/customer/_camera.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
use frontend\assets\CustomerAsset;
|
||||
use backend\models\CustomerCreate;
|
||||
CustomerAsset::register($this);
|
||||
$options = [];
|
||||
|
||||
|
||||
if ( $model instanceof frontend\models\CustomerCreate){
|
||||
$options['image_data' ] = 'customercreate-photo_data';
|
||||
}else{
|
||||
}
|
||||
$this->registerJs ( 'new Customer( '. json_encode($options).');' );
|
||||
?>
|
||||
|
||||
<style>
|
||||
.photo{
|
||||
border: 1px solid gray;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<div id="my_camera" class='photo pull-left' style="width: 160px; height: 120px; margin-right: 12px;"></div>
|
||||
<div id="my_result" class='photo pull-left' style="width: 160px; height: 120px;"></div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
</div>
|
||||
</div>
|
||||
<a id="snap" class="btn btn-primary">Fénykép</a>
|
||||
@@ -16,7 +16,7 @@ use kartik\widgets\DatePicker;
|
||||
<div class="customer-form">
|
||||
|
||||
<?php $form = ActiveForm::begin(); ?>
|
||||
|
||||
<?= $form->field($model, 'photo_data')->hiddenInput()->label(false) ?>
|
||||
|
||||
<div class='row'>
|
||||
<div class='col-md-3'>
|
||||
|
||||
@@ -18,7 +18,7 @@ use yii\base\Widget;
|
||||
|
||||
<?php $form = ActiveForm::begin(); ?>
|
||||
|
||||
|
||||
<?= $form->field($model, 'photo_data')->hiddenInput()->label(false) ?>
|
||||
<div class='row'>
|
||||
<div class='col-md-3'>
|
||||
<?php //echo $form->field($model, 'cardNumber')->widget(CardNumberTypeahead::className(),[]) ?>
|
||||
|
||||
@@ -20,7 +20,7 @@ $card = $customer->card;
|
||||
<div class="customer-create">
|
||||
|
||||
<?php echo ReceptionWidget::widget( ['form' => $receptionForm, 'route' => ['customer/create'] ] )?>
|
||||
|
||||
<?php echo $this->render('_camera',['model' => $model]); ?>
|
||||
<h1><?= Html::encode($this->title) ?></h1>
|
||||
|
||||
<?= $this->render('_form_create', [
|
||||
|
||||
@@ -4,10 +4,16 @@ use yii\helpers\Html;
|
||||
use frontend\components\ReceptionMenuWidget;
|
||||
use frontend\components\ReceptionCardNumberWidget;
|
||||
use frontend\components\ReceptionWidget;
|
||||
use common\assets\WebcamjsAsset;
|
||||
use frontend\assets\CustomerAsset;
|
||||
use common\components\Image;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/* @var $this yii\web\View */
|
||||
/* @var $model common\models\Customer */
|
||||
|
||||
|
||||
|
||||
$this->title = Yii::t('common/customer', 'Update customer:' ) . ' ' . $model->name;
|
||||
$this->params['breadcrumbs'][] = ['label' => Yii::t('common/customer', 'Customers'), 'url' => ['index']];
|
||||
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id_customer]];
|
||||
@@ -16,6 +22,8 @@ $this->params['breadcrumbs'][] = Yii::t('common/customer', 'Update');
|
||||
$customer = $model;
|
||||
$card = $customer->card;
|
||||
|
||||
|
||||
|
||||
?>
|
||||
<div class="customer-update">
|
||||
|
||||
@@ -23,9 +31,18 @@ $card = $customer->card;
|
||||
<?php echo ReceptionWidget::widget( ['form' => $receptionForm, 'route' => ['customer/reception'] ] )?>
|
||||
|
||||
|
||||
<?php echo $this->render('_camera',['model' => $model]); ?>
|
||||
|
||||
<h1><?= Html::encode($this->title) ?></h1>
|
||||
|
||||
<?php if ( $model->image1 ){
|
||||
echo Html::img( Url::base( ) . Image::thumb( $model->image1->path,160,120 ));
|
||||
}
|
||||
|
||||
?>
|
||||
<?= $this->render('_form_update', [
|
||||
'model' => $model,
|
||||
]) ?>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
43
frontend/web/js/customer.js
Normal file
43
frontend/web/js/customer.js
Normal file
@@ -0,0 +1,43 @@
|
||||
function Customer(o){
|
||||
|
||||
var defaults = {
|
||||
'image_data' : 'customerupdate-photo_data'
|
||||
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
function init(){
|
||||
|
||||
defaults = $.extend(defaults,o);
|
||||
|
||||
Webcam.set({
|
||||
width: 160,
|
||||
height: 120,
|
||||
dest_width: 320,
|
||||
dest_height: 240,
|
||||
image_format: 'jpeg',
|
||||
jpeg_quality: 90,
|
||||
// force_flash: false,
|
||||
// flip_horiz: true,
|
||||
// fps: 45
|
||||
});
|
||||
|
||||
Webcam.attach( '#my_camera' );
|
||||
|
||||
$("#snap").click(snap);
|
||||
|
||||
}
|
||||
|
||||
function snap(){
|
||||
Webcam.snap( function(data_uri) {
|
||||
document.getElementById('my_result').innerHTML = '<img width="160" height="120" src="'+data_uri+'"/>';
|
||||
|
||||
var raw_image_data = data_uri.replace(/^data\:image\/\w+\;base64\,/, '');
|
||||
|
||||
document.getElementById(defaults.image_data ).value = raw_image_data;
|
||||
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user