I want to open PDF document in new TAB while pressing button "Download PDF".
Found a solution on SO that you have to add 'target' => "_blank", but in my case it's not working and I don't have an idea why. Tried setLinkAttribute, setChildAttribute, setAttribute and none of these are working. Also, content disposition inline already added.
$menu
->addChild(
'download_file',
[
'route' => 'admin_download_file',
'routeParameters' => [
'token' => $admin->getToken(),
],
]
)
->setAttribute('type', 'link')
->setLinkAttribute('target', '_blank');
->setLinkAttributes(['target', '_blank']);
or I think on child level something like:
->addChild(
'download_file',
[
'route' => 'admin_download_file',
'routeParameters' => [
'token' => $admin->getToken(),
],
'linkAttributes' => ['target' => '_blank']
]
)
Related
I am using the cakephp-upload plugin and I managed to upload images to my server:
WorkersTable:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('workers');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'photo_dir'
]
]
]);
}
view.ctp:
echo $this->Form->create($newWorker, ['type' => 'file']);
echo $this->Form->input('avatar', ['type' => 'file']);
echo $this->Form->input('photo_dir', ['type' => 'hidden']);
Now the avatar images are uploaded, but they are not put into the photo_dir subdirectory.
What am I missing? It works without any problems in my CakePHP 2.8.x application.
Author of the plugin here.
The fields.dir attribute does not state what the subdirectory should be. It is a reference to the column in your database table where we should save the director where we saved the file.
If you want to change the place where you save files on disk, you should instead use the path option. Here is an example where i use the photo_dir subdirectory:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'path' => 'webroot{DS}files{DS}{model}{DS}{field}{DS}photo_dir{DS}'
]
]);
The default value for the path option is webroot{DS}files{DS}{model}{DS}{field}{DS}.
Shouldn't it be:
$this->addBehavior('Josegonzalez/Upload.Upload', [
'avatar' => [
'fields' => [
'dir' => 'avatar_dir'
]
]
]);
echo $this->Form->input('avatar_dir', ['type' => 'hidden']);
If you want to use better option than you can use below plugin which has better option for upload the file rather than Josegonzalez/Upload.Upload plugin.
I have used below one in my project.
Utils Plugin for Cake 3.x
Link for this plugin : https://github.com/cakemanager/cakephp-utils
Documentation : http://cakemanager.org/docs/utils/1.0/behaviors/uploadable/
And this is the configuration :
$this->addBehavior('Utils.Uploadable', [
'image' => [
'path' => '{ROOT}{DS}{WEBROOT}{DS}uploads{DS}{field}{DS}',
'fileName' => md5(rand(1000, 5000000)) . '.{extension}',
'removeFileOnDelete' => true,
'removeFileOnUpdate' => FALSE
],
]);
Here you can customise it. Let me know if you have any question regarding this.
I want to make search in my project. I use typeahead but it's not working. This is my code:
<?php
echo '<label class="control-label">Select Repository</label>';
$template = '<div><p class="repo-language">{{no_telepon}}</p>' .
'<p class="repo-name">{{nama}}</p>' .
'<p class="repo-description">{{email}}</p></div>';
echo Typeahead::widget([
'name' => 'twitter_oss',
'options' => ['placeholder' => 'Filter as you type ...'],
'dataset' => [
[
'prefetch' => Penerima::find()->all(),
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
'display' => 'value',
'templates' => [
'notFound' => '<div class="text-danger" style="padding:0 8px">Unable to find repositories for selected query.</div>',
'suggestion' => new JsExpression("Handlebars.compile('{$template}')")
]
]
]
]);
?>
This question was asked long time a go.
I also faced the same problem, but i could figure-out this.
for future reference i add this post.
in your controller
$result = SampleModel::find()
->select('Attribute_name')
->where('name LIKE "%' . $searchParameter .'%"')
->asArray()
->all();
return Json::encode($result);
here you need to get the database value as "associative array", you can get that from using "asArray()".
then as you see return value as Json encode.
in your "View"
<?php
echo Typeahead::widget([
'name' => 'sampleName',
'options' => ['placeholder' => 'Filtering data ...'],
'scrollable' => true,
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'remote' => [
'url' => Yii::$app->urlManager->createUrl(['sample/action']) .
'?searchParameter=%QUERY',
'wildcard' => '%QUERY'
],
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('Atribute_name')",
'display' => 'Atribute_name',
'limit' => 10,
],
],
'pluginEvents' => [
'typeahead:select' => 'function(e, s) {
EnableUserDetailsTypeAhead(s);
}',
]
]);
?>
here few things to be consider.
calling to the controller action. you can do that.
Yii::$app->urlManager->createUrl(['sample/action']) .
'?searchParameter=%QUERY',
'wildcard' => '%QUERY'
],
the below lines inside data set must be provide.
'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('Atribute_name')",
'display' => 'Atribute_name',
you will get the expected data.
this sample code i have tested and this is working
From the docs:
prefetch: array, configuration for the prefetch options object. Refer documentation for the options you can set for this parameter. The return data must be Json encoded and converted to an associative array of the format [['value' => 'data1'], ['value' => 'data2'],...], where value is the fixed key set in display
You are passing an array of objects instead of an array of key value pairs. You can use asArray to create a list of objects. You will need to change display to the name of the field containing the data:
'prefetch' => Penerima::find()->select('title')->asArray()->all(),
I'm an getting a list of files in a folder. The response contains a iconLink for every file returned. This icon is 16x16 pixels.
Does anyone know a way to retrieve a retina image? Or another way to retrieve a bigger icon image?
https://developers.google.com/drive/v2/reference/files
top: Google Drive UI
bottom: Google Drive API integration
The good news is although not officailly documented driver does have 2x resolution icons. The bad news is they have inconsistent file names; for example the icon you linked in the comments has a 32px version availabel here: ssl.gstatic.com/docs/doclist/images/mediatype/icon_3_pdf_x32.png
Now here is my soltion, it's not perfect but it will do the job for a while:
function getIcons($file_type)
{
$icons = [
'pdf' => [
'icon' => 'icon_12_pdf_list.png',
'retina' => 'icon_3_pdf_x32.png'
],
'document' => [
'icon' => 'icon_1_document_x16.png',
'retina' => 'icon_1_document_x32.png'
],
'image' => [
'icon' => 'con_1_image_x16.png',
'retina' => 'icon_1_image_x32.png'
],
'word' => [
'icon' => 'icon_1_word_x16.png',
'retina' => 'icon_1_word_x32.png'
],
'text' => [
'icon' => 'icon_1_text_x16.png',
'retina' => 'icon_1_text_x32.png'
],
'spreadsheet' => [
'icon' => 'icon_1_spreadsheet_x16.png',
'retina' => 'icon_1_spreadsheet_x32.png'
],
'form' => [
'icon' => 'icon_2_form_x16.png',
'retina' => 'icon_2_form_x32.png'
],
'audio' => [
'icon' => 'icon_1_audio_x16.png',
'retina' => 'icon_1_audio_x32.png'
]
];
return isset($icons[$file_type]) ? $icons[$file_type] : $icons['text'];
}
The reasion I say it will work for a while is that I'm asuming the _3_ in pdf icon file name for instance is the version number. So if Google updates it's icons again in the future this solution may brake.
I'm using drive rest api and what i observed was that iconLink attribute had a definite pattern.
"https://drive-thirdparty.googleusercontent.com/" + size + mimetype
By default size is 16. So, before adding your icon to Image, use this:
String iconLink = (String) jsonObject.get("iconLink");
iconLink=iconLink.replace("16","128");
check out these both links:
https://drive-thirdparty.googleusercontent.com/128/type/application/pdf
https://drive-thirdparty.googleusercontent.com/16/type/application/pdf
Looks like images with x128 also added/present for various versions:
Ver. 1
Ver. 2
Ver. 3
Better to replace the x16 from the fetched iconLink and replace it with x128.
Summary: Trying to add export in the for csv & pdf download.
Followed documantation to install. Grid view is otherwise working.
Also added as module in config/web.php's $config array -
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
//'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '#kvgrid/messages',
//'forceTranslation' => false
]
]
],
N.B: I am using basic template and new in yii2. I have tried other fixes like composer update etc as suggested in various posts but really stuck with the problem.
the thing causing problem is - Yii::t('kvgrid', 'Reset Grid')
Can somene give me a direction here. I guess it is very simple issue :(
You was got above error because of you have not configure i18n component in web.php file.
You need to configure i18n component like as,
'i18n' => [
'translations' => [
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
],
]
],
Or uncomment the gridview modules's i18n configuration
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#kvgrid/messages',
'forceTranslation' => true
]
]
You have to replace
Yii::t('kvgrid', 'Reset Grid')
to
Yii::t('app', 'Reset Grid')
in your view file
I suggest better to generate CRUD using the Ajax Crud Generator, it will do all the required task for CRUD and export as well...try this
I need getting list of yii2 assets registered in the page for creating an array for response SPF.js requests
I use this code in a new layout named ajax.php
<?php
use yii\helpers\Html;
use app\assets\AppAsset;
use \yii\helpers\Json;
AppAsset::register($this);
$spf_data = [
'title' => Html::encode($this->title),
'head' => $this->beginPage() . Html::csrfMetaTags() . $this->head(),
'body' => ['content' => $this->beginBody() . $content],
"attr" => [
'content' => [
"class" => "container"
]
],
'foot' => $this->endBody() . $this->endPage(),
];
echo Json::htmlEncode($spf_data);
my problem is registered assets to page and i can't get them in my array
i need to get list of meta tags,link tags and script in head index of array
how can I do?
You can get scripts and styles in your controller action with this code
ob_start();
ob_implicit_flush(false);
$this->view->beginPage();
$this->view->head();
$this->view->beginBody();
$this->renderPartial($view, $params);
$this->view->endBody();
$this->view->endPage(true);
$style_script= ob_get_clean();
and use $style_script in $spf_data
$spf_data = [
'title' => Html::encode($this->title),
'head' => $style_script,
'body' => ['content' => $this->renderPartial('viewName')],
"attr" => [
'content' => [
"class" => "container"
]
],
];
this code work for me