Yii2 - Uploading multiple images in the form using kartik fileInput - file-upload

Now the problem with kartik widget is in the form view after you upload the first photo( before submiting the form) for the other next ones it just gets overwrite on the previous ones and the only preview in the field is for the last uploaded one.
<?= $form->field($model, 'image')->widget(FileInput::classname(), [
'name'=> 'ads_photos[]',
'options'=>['accept'=>'image/*', 'multiple'=>true, ],
'pluginOptions'=>[
'allowedFileExtensions'=>['jpg','gif','png'],
'overwriteInitial'=>false,
]
]);
?>
as you can see I've set 'multiple'=>true, and 'overwriteInitial'=>false, as far as you can see in the guide it has said the same.
but what's the problem?

You need to read the guide more careful:
Note:
When using the widget to upload multiple files you must name the HTML input in array format by appending [] to the input/attribute name. Check the examples for usage.

Did you tried by setting default initial preview. suppose if your widget dont have any previous images then set the default image like we put default avatar for profile photo.
Try this by setting initial preview property
<?= $form->field($model, 'image')->widget(FileInput::classname(), [
'name'=> 'ads_photos[]',
'options'=>['accept'=>'image/*', 'multiple'=>true, ],
'pluginOptions'=>[
'allowedFileExtensions'=>['jpg','gif','png'],
'initialPreview'=>[
"http://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/631px-FullMoon2010.jpg",
],
'overwriteInitial'=>false,
'initialPreviewAsData'=>true,
]
]);
?>
Hope it will help you

Related

Yii2 register Js set Type and remove jquery

i want to have script tag like this for each View
<script type="application/ld+json">
/****** my code
</script>
with
$this->registerJs(....)
i get this kind of code:
<script>jQuery(function ($) {
.....
});</script>
how to add diffrent type and how to remove jQuery..?
By default registerJs() is using $position = View::POS_READY and this one is registering automatically jQuery asset. If you don't want this you can use:
registerJs($js, \yii\web\View::POS_HEAD) - to place JS in the head part
registerJs($js, \yii\web\View::POS_BEGIN) - to place JS in the beginning of body part
registerJs($js, \yii\web\View::POS_END) - to place JS in the end of body part
Unfortunately all these will add your script in the standard <script> tag without the type.
To achieve this you must add it manually either by placing the <script...> by yourself or by calling \yii\helpers\Html::script($js, ['type' => 'application/ld+json']) in your view or layout file.
I use this in the layout. Using blocks allows me to replace this for other schema.org in other pages.
<?= Html::script(isset($this->blocks['schema'])
? $this->blocks['schema']
: \yii\helpers\Json::encode([
'#context' => 'https://schema.org',
'#type' => 'WebSite',
'name' => Yii::$app->name,
'image' => $this->image,
'url' => $this->url,
'descriptions' => $this->description,
'author' => [
'#type' => 'Organization',
'name' => Yii::$app->name,
'url' => Yii::$app->homeUrl,
'telephone' => Yii::$app->params['phone'],
]
]), [
'type' => 'application/ld+json',
]) ?>
Simply use Html::script($js, $options).
Consider the usage of Json::encode() to avoid writing JS in a PHP
file. It's prettier for me that way, and also makes variable
interpolation easier.
In this example you see a lot of $this->image|description|url
because I'm using https://github.com/daxslab/yii2-taggedview to extend the
yii\web\View with more attributes in order to make automate Opengraph
and Twitter Cards tags generation.

How to display PDF file on yii2

I have a audit table and audit_report field.Field type is text .I saved pdf files into folder and saved name to database.I tried to display the pdf on view page. but the box with image sign only getting. I could display jpeg and png files nicely.How to display PDF on view page of yii2 framework.
This would work,
return Yii::$app->response->sendFile($completePath, $filename, ['inline'=>true]);
Input the function with third param as array of value 'inline'=>true to open the file within the browser window.
See the documentation here sendFile()
You could add a button that opens the file in a new tab, but make it link to an action in your controller that returns the file instead of the direct path to the file:
In your view:
<?= Html::a('PDF', [
'controller/pdf',
'id' => $model->id,
], [
'class' => 'btn btn-primary',
'target' => '_blank',
]); ?>
In your controller:
public function actionPdf($id) {
$model = ModelClass::findOne($id);
// This will need to be the path relative to the root of your app.
$filePath = '/your/file/path';
// Might need to change '#app' for another alias
$completePath = Yii::getAlias('#app'.$filePath.'/'.$model->fileName);
return Yii::$app->response->sendFile($completePath, $model->fileName);
}
Aliases - Key Concepts - The Definitive Guide to Yii 2.0
Although the question has been answered there is one thing that needs to be addressed if you have the file content/stream instead of the file path, like for instance you are using Dropbox API and you receive a stream from the API and want to display that file instead of forcing the browser to download.
For this case you can use the sendContentAsFile when attempting to display the PDF file in the browser you will need to specify the mimeType option too along with the "inline"=>true because the default mimeType value is set to application/octet-stream which is used to download a file and to display inline in browser you need to change it to application/pdf.
return Yii::$app->response->sendContentAsFile(
$fileContent,
$filename,
['inline' => true, 'mimeType' => 'application/pdf']
);

Bootstrap Dropdown not working with Kartik ExportMenu widget in yii2

When I use Kartik ExportMenu widget in my code, all dropdown stop working..
Here's a sample of code I am using,
echo ExportMenu::widget([ 'dataProvider' => $dataProvider, 'columns' => $gridColumns ]);
the widget is affecting only that single page, dropdowns on other pages are working..
After some Googling, I found that its conflicting with bootstrap js file.. which I am including in my Asset 'bootstrap.min.js',
if I remove bootstrap.min.js, obviously.. all dropdown should stop working.. but the dropdown on the page I have widget are working..
In my project I need to use both of them...
Is there any solution, anyone can think off??
for that use unset concept in that particular page.
add following lines at the end of your code.
<?php unset($this->assetBundles['yii\bootstrap\BootstrapAsset']); ?>
Add a showColumnSelector to the widget options and assign value true to it. as in:
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns'=>$gridColumn,'showColumnSelector' => \true,
]);

enabling layout disables cjuidatepicker yii

I have a small view which renders cjuidatepicker widget.
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'flat'=>FALSE,
// 'model' => $model,
'attribute' => 'start_date',
// 'value' => $model->start_date,
'name'=>'dateSelect',
'options' => array(
'showButtonPanel' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
));?>
while this renders nicely without using a layout, but when I put this view in my project layout, the datepicker refuses to show up.
I have no ideas where to look for this, all the css and js are loading similarly in layout and without layout.
Any help would be greatly appreciated.
Thanks
Having dealt my fair share with the datepicker, I'm willing to bet money on that the root to your problem is an error in your javascript. Try to comment out all included javascript in your layout and see what happens. For example, maybe you have jQuery included twice, or some code section missing a closing bracket. Basically any js error will throw datepicker off it's game. Just check the js console in your favorite browser.
I had multiple versions of jquery being loaded and that created quite a mess. Yii jquery was different from the template jquery version being used. Brought both of them to an agreement and boom!!

yii-auth set theme

I want use in yii-auth module my layout. But in that layout i have
<?php echo Yii::app()->theme->baseUrl; ?>
because i need load some css files and images. Module yii-auth doesnt have theme set, so i get error because theme "panel" is not set.
Yii-auth settings let me set only layout, not theme.
I can easy set this by modify yii-auth module, but then i have to remember about this while yii-auth update. Its dirty :)
The question is: How can i change theme for yii-auth without tuching modules/auth folder?
Edit:
My default config is other then "panel". I cant set 'theme'=>'panel' in config/main.php.
As you can define a custom layout for the module you can do this (eg. file views/layouts/panel.php):
<?php Yii::app()->theme = 'panel'; ?>
<?php include(dirname(__FILE__).'/main.php') ?>
Which will set another theme and then just use the main layout file.
In your config you should set //layouts/panel as the default layout for your auth module.
You can also do this one a per file/view bases, see this file for a sample implementation.
[UPDATE]
Please have a look at https://github.com/schmunk42/multi-theme
You can define a theme per controller route.
In my case setting
'appLayout' => 'webroot.themes.bootstrap.views.layouts.main', // the layout used by bootstrap theme.
in /config/main.php did the trick
'auth' => array(
'strictMode' => true, // when enabled authorization items cannot be assigned children of the same type.
'userClass' => 'User', // the name of the user model class.
'userIdColumn' => 'id', // the name of the user id column.
'userNameColumn' => 'username', // the name of the user name column.
'defaultLayout' => 'application.views.layouts.main', // the layout used by the module.
'appLayout' => 'webroot.themes.bootstrap.views.layouts.main', // the layout used by bootstrap theme.
'viewDir' => null, // the path to view files to use with this module.
),