hi all i faced problems here
after i find a way to retrieve id data from gridview, now my problem is export the data to excel. here is my code in controller
public function actionCetakdispo()
{
$selection=(array)Yii::$app->request->post('selection');
$template = \Yii::getAlias("#webroot") . "/template/dispo.xlsx";
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template);
$worksheet = $spreadsheet->getActiveSheet();//typecasting
foreach($selection as $id){
$suratmasuks = Smwp::findOne((int)$id);
$baserow = 5;
$no = 1;
foreach($suratmasuks as $suratmasuk){
$row = $no + $baserow;
$worksheet->getCell('A'. $row)->setValue($no);
$worksheet->getCell('B'. $row)->setValue($suratmasuk->jenis_surat);
$worksheet->getCell('C'. $row)->setValue($suratmasuk->perihal);
$no++;
}
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="dispo.xlsx"');
header('Cache-Control: max-age=0');
Yii::$app->session->setFlash('success', 'berhasil download!');
$writer->save('php://output');
exit;
}
its show an error : Trying to get property 'jenis_surat' of non-object. if i print_r($surat_masuks), its show like this :
app\models\SmWp Object (
[_attributes:yii\db\BaseActiveRecord:private] => Array (
[id] => 4
[id_mfwp] => 1
[id_user] => 3
[tgl_surat] => 2019-10-08 00:00:00
[tgl_terima] => 2019-10-15 00:00:00
[tgl_cetak] => 2019-10-14 00:00:00
[nomor_surat] => kksksk
[no_agenda] => 992929
[jenis_surat] => 3
[perihal] => balasan surat kami
[lokasi_scan] =>
[ket_dispo] => kerjakan
[index_cetak_dispo] => 0
[index_cetak_reg] => 0
)
[_oldAttributes:yii\db\BaseActiveRecord:private] => Array (
[id] => 4
[id_mfwp] => 1
[id_user] => 3
[tgl_surat] => 2019-10-08 00:00:00
[tgl_terima] => 2019-10-15 00:00:00
[tgl_cetak] => 2019-10-14 00:00:00
[nomor_surat] => kksksk
[no_agenda] => 992929
[jenis_surat] => 3
[perihal] => balasan surat kami
[lokasi_scan] =>
[ket_dispo] => kerjakan
[index_cetak_dispo] => 0
[index_cetak_reg] => 0
)
[_related:yii\db\BaseActiveRecord:private] => Array ( )
[_relationsDependencies:yii\db\BaseActiveRecord:private] => Array ( )
[_errors:yii\base\Model:private] =>
[_validators:yii\base\Model:private] =>
[_scenario:yii\base\Model:private] => default
[_events:yii\base\Component:private] => Array ( )
[_eventWildcards:yii\base\Component:private] => Array ( )
[_behaviors:yii\base\Component:private] => Array ( )
)
thx for explanation..
Edit : this is my gridview code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn',],
'id',
'kategori',
'nama_wp',
'nama',
'nomor_surat',
'tgl_surat',
'perihal',
'ket',
['class' => 'yii\grid\CheckboxColumn', 'checkboxOptions' => function($model, $key, $index, $widget) {
return ['value' => $model['id'] ];
},],
],
]); ?>
Method findOne will always return only one instance of Smwp model. Not an array of multiple models.
$suratmasuks = Smwp::findOne((int)$id);
Because \yii\db\ActiveRecord implements IteratorAgregate interface (see documentation) you can use it in foreach like this:
foreach ($suratmasuks as $suratmasuk) {
// ...
}
But in $suratmasuk won't be instances of Smwp but values of attributes of that one instance loaded by findOne.
You can load all models with following find and get rid of outer foreach.
$suratmasuks = Smwp::find()
->where(['id' => $selection])
->all();
Your whole code can look like
public function actionCetakdispo()
{
$selection=(array)Yii::$app->request->post('selection');
$template = \Yii::getAlias("#webroot") . "/template/dispo.xlsx";
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($template);
$worksheet = $spreadsheet->getActiveSheet();//typecasting
$suratmasuks = Smwp::find()
->where(['id' => $selection])
->all();
$baserow = 5;
$no = 1;
foreach($suratmasuks as $suratmasuk){
$row = $no + $baserow;
$worksheet->getCell('A'. $row)->setValue($no);
$worksheet->getCell('B'. $row)->setValue($suratmasuk->jenis_surat);
$worksheet->getCell('C'. $row)->setValue($suratmasuk->perihal);
$no++;
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="dispo.xlsx"');
header('Cache-Control: max-age=0');
Yii::$app->session->setFlash('success', 'berhasil download!');
$writer->save('php://output');
exit;
}
If you wanted to keep loading the model by one you should get rid of the inner foreach instead and output it like this:
$baserow = 5;
$no = 1;
foreach($selection as $id){
$suratmasuks = Smwp::findOne((int)$id);
$row = $no + $baserow;
$worksheet->getCell('A'. $row)->setValue($no);
$worksheet->getCell('B'. $row)->setValue($suratmasuks->jenis_surat);
$worksheet->getCell('C'. $row)->setValue($suratmasuks->perihal);
$no++;
}
But this will mean your script will do much more queries to DB so the first way is better.
Related
I'm trying to implement a multiple models form using the form-wizard widget, with Profile as main model and few others as linked ones. When I select the entity type field for the main model, I would like to change the linked model for the next step, basing on the value of entity type field.
I have tried with this code:
Create Form Code
$modelUrlReletedModelsCreate = Profile::urlRelatedModelCreate();
$urlLinkedProfile = Url::to(['create']);
echo FormWizard::widget([
'formOptions'=>[
'id'=>'profile_form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'validationUrl' => Url::to(['profile-models-validation'])
],
'theme' => FormWizard::THEME_MATERIAL_V,
'steps' => [
//step 1
[
'model' => $model,
'title' => \Yii::t('app', 'Profile'),
'fieldConfig' => [
'only' => ['entity_type_id', 'profile_type_id'],
'entity_type_id' => [
'widget' => Select2::class,
'options' => [
'data' => EntityType::arrayNamesList(),
'options' => ['placeholder' => \Yii::t('app','Select an element')],
'pluginEvents' => ['select2:select'=>'function(e){;'
. 'var type = $("#pr-ty-sel").val();'
. 'var profile= "'.($model->profile_id ? : "no").'";'
. '$.ajax({ method: "GET",'
. 'url:"'.$urlLinkedProfile.'",'
. 'data:{ entity_text : e.params.data.text,'
. 'entity_id : e.params.data.id,'
. 'profile_type_id : type,'
. 'profile_id : profile},'
. 'success: function(data){'
. '$("#profile_form").html(data);'
. '}'
. '});}'],
],
],
'profile_type_id' =>[
'widget' => Select2::class,
'options' => [
'data' => \app\models\ProfileType::arrayNamesList(),
'options' => ['placeholder' => \Yii::t('app','Select an element'),'id'=>'pr-ty-sel'],
],
]
],
'description' => \Yii::t('app', 'Add Profile Type Data'),
'formInfoText' => \Yii::t('app', 'Fill all fields'),
],
//step 2 I want ot change here $linkedModel
[
'model' => [$model,$linkedModel],
'title' => \Yii::t('app', 'Personal Data'),
'description' => \Yii::t('app', 'Insert Personal Data'),
],
]
]);
Controller Action Create Code
public function actionCreate($profileId = NULL)
{
if($profileId AND $profileId !== 'no'){
$model = $this->findModel($profileId);
}else{
$model = new Profile();
}
$profileLinkedModel = new ProfilePrivate();
$renderMethod = 'render';
if (Yii::$app->request->isAjax) {
$entityText = \Yii::$app->request->get('entity_text');
$entity_id = \Yii::$app->request->get('entity_id');
$profileTypeId = \Yii::$app->request->get('profile_type_id');
$profileId = \Yii::$app->request->get('profile_id');
//Utility function to clean the entity text (remove number and special characters)
$entityTextCleaned = \app\components\Utility::cleanString($entityText);
if ($entityTextCleaned == 'private') {
$profileLinkedModel = new ProfilePrivate();
} elseif ($entityText == 'company') {
$profileLinkedModel = new ProfileCompany();
} else {
//#TODO
return FALSE;
}
$model->entity_type_id = $entity_id;
$model->profile_type_id = $profileTypeId;
$profileLinkedModel->profile_id = $model->profile_id;
Yii::$app->response->format = Response::FORMAT_JSON;
$renderMethod = 'renderAjax';
}
//extra table field used to enable custom rule in model
$model->createFullProfile = TRUE;
if ($model->load(Yii::$app->request->post())) {
return $this->redirect(['view', 'id' => $model->profile_id]);
}
return $this->$renderMethod('create', [
'model' => $model,
'profileLinkedModel' => $profileLinkedModel,
]);
}
When I select a field for entity type field, the server runs the ajax request on select event, but when it ends the other form field is not more selectable. So, in other words, after the ajax request I'm unable to select the profile type field. If I try to select the profile type field before the entity type field I can go to next step, but load always the default model.
I got an error message, when i want to load back saved elements on update form.
Controller
$contentCategory = ContentCategory::find()
->where(['content_id' => $id])->all();
View
<?= $form->field($contentCategory, 'category_id')
->dropdownList(ArrayHelper::map(Category::find()->all(),'id','title'),
['prompt'=>'Select Category', 'multiple' => 'multiple']
)->label('Add categories'); ?>
The error message.
Call to a member function isAttributeRequired() on array
If i change the all() method to one() it's works but select only one element (of course).
Update:
#scaisEdge I'm using a content_category junction table to insert relations contents with categories.
content_id category_id
1 2
1 3
Model
public function rules()
{
return [
[['content_id', 'category_id'], 'integer'],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[['content_id'], 'exist', 'skipOnError' => true, 'targetClass' => Content::className(), 'targetAttribute' => ['content_id' => 'id']],
];
}
The trouble in that you use $contentCategory array of founded models as form field model.
I think you should just change $contentCategory variable from your view on your certain model like $model = ContentCategory::findOne($id);
#Csaba Faragó
$arr = array();
$colorarr=array();
foreach ($detmodel as $key => $detailm){
//$detailm;
$id = $detailm['productid'];
$sid = $detailm['sizeid'];
$sidex= explode(",",$sid);
$i = 0;
foreach($sidex as $size_id)
{
$val = $sidex[$i];
$val = (int)$val;
array_push($arr, $val);
$i++;
}
<?php $sizelist = ArrayHelper::map(Size::find()->all(),'id','size');
// $idd = '4';
?>
<?php
$detailm->sizeid = $arr;
// print_r($detailm->sizeid);
?>
<?= $form->field($detailm, 'sizeid')->widget(Select2::classname(), [
'data' => $sizelist,
'language' => 'de',
'options' => ['placeholder' => 'Select sizes ...','multiple' => true],
'pluginOptions' => [
'tags' => true,
'tokenSeparators' => [',', ' '],
'maximumInputLength' => 10
],
]);
?>
do all this in view page
I have found a solution here. He is perfectly solve my issue.
I'm encoding in JSON a bunch of data from a WP Query:
$args = array(
'posts_per_page' => 20,
'post_type' => 'post',
'category' => 6,
'meta_key' => 'custom_total_hits',
'tag' => 'indie-pop',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'after' => date('Y-m-d', strtotime('-40 days'))
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$output[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'count' => $post->custom_total_hits,
'soundcloud_url' => $post->soundcloud_song,
'soundcloud_id' => $post->soundcloud_ids,
'link' => get_permalink($post),
);
}
echo json_encode($output);
I would like to display in my JSON a key corrisponding to the src of the medium size of the attached image. If I use 'images' => get_attached_media('image', $post->ID) it retrives an array of multiple data which I can not access since I don't know the ID of the attached image when I process the data of my JSON. How can I do to retrieve a first level key - value where the value is the src of the attached image?
get_post_thumbnail_id : Get post thumbnail ID
wp_get_attachment_url : Get attachment URL by attachment id
'images' => parse_url( wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ) );
Would you please try above code?
Try this solution:
$images = array();
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
foreach (get_intermediate_image_sizes() as $size) {
$images[$size] = wp_get_attachment_image_src($post_thumbnail_id, $size);
}
//end
'images' => $images // type_of_size => image_url
I have used use kartik\file\FileInput; (extension) for saving multiple images from single form submit.
The images are save locally but are not saving in the database.
Here is my Model Code media.php
namespace app\models;
use yii\web\UploadedFile;
class Media extends \yii\db\ActiveRecord
{
public function rules(){
return [
[['title'], 'file', 'skipOnEmpty' => false, 'extensions' => ['gif', 'jpg', 'png', 'jpeg', 'JPG', 'JPEG', 'PNG', 'GIF'], 'checkExtensionByMimeType' => false ,'maxFiles' => 4, 'maxSize' => 1024 * 1024 * 1024],
[['extension'], 'string', 'max' => 6],
];
}
Controller code:
if ($mediamodel->load ( Yii::$app->request->post () )) {
$title = UploadedFile::getInstances ( $mediamodel, 'title' );
foreach ($title as $key => $file) {
$file->saveAs(Yii::$app->basePath . '/web/images/hotel/'. $file->baseName . '.' . $file->extension);}
foreach ($title as $key => $file){
echo $mediamodel->title."*********";
$mediamodel->title = $file->baseName . '.' . $file->extension;
echo " \Title: ".$mediamodel->title;
$mediamodel->save ();
}
}
My view code:
use kartik\file\FileInput;
$form = ActiveForm::begin([ 'layout' => 'horizontal', {label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'fieldConfig' => ['horizontalCssClasses' => ['label' => 'col-md-3','offset' => 'col-md-offset-2','wrapper' => 'col-md-4', 'error' => '','hint' => '',],],'
options' => [ 'class' => 'form-horizontal', 'enctype' => 'multipart/form-data', ], ]);?>
<?php echo $form->field($mediamodel, 'title[]')->widget(FileInput::classname(), ['options'=>['multiple' => true]]);
Html::submitButton($model->isNewRecord ? 'Add' : 'Update');
use
$mediamodel->save (false);
instead of
$mediamodel->save ();
// You should handle errors like this.
if(!$mediamodel->save()){
// handle the errors of model.
var_dump($mediamodel->getErrors());
}
I can (finally!!!) set a query to return results for a checkbox(es) set within a collapsible fieldset in Drupal 7- however when I try to put it into a tableselect, I get no results. Would someone be able to check out this code and see if you can tell me why? I also have a screen shot of both results- but since I am new here it won't allow me to post it.
$form = array();
$secnum = 1;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$options = array();
foreach ($result as $record) {
$options[$record->secser_id] = $record->ser_name;
}
$form['secser']['1'] = array(
'#title' => t('Basic Sanitation'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$form['secser']['1']['secser'] = array(
'#title' => t('Choices'),
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => $options,
'#description' => t('choose!'),
);
$form['secser']['2'] = array(
'#title' => t('Community Systems'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
);
$secnum = 2;
$result = db_query('SELECT s.secser_id, s.ser_name FROM {secser} s WHERE s.sec_num = :secnum', array(':secnum' => $secnum));
$opt2 = array();
foreach ($result as $record) {
$opt2[$record->secser_id] = $record->ser_name;
}
$header = array(
'ser_name' => t('Choose Service(s)'),
);
$form['secser']['2']['secser'] = array(
'#type' => 'tableselect',
'#title' => t('Community Systems'),
'#header' => $header,
'#options' => array($opt2),
'#multiple' => TRUE,
);
I haven't tried running your code, but it looks like your options are an array which contains another array.
#options' => array($opt2),
because $opt2 is an array, this should be:
#options' => $opt2,
the same way you have the options set up in your checkboxes