PhpStorm function with array parameter alignment - phpstorm

I have this:
$client->setHeaders(array(
'Date' => $date,
'X-Amzn-Authorization' => $authKey,
));
PhpStorm wants to autoformat it as this:
$client->setHeaders(
array(
'Date' => $date,
'X-Amzn-Authorization' => $authKey,
));
Which settings do I need to change in the code format options in order to get the former not the latter?

go to Setttings > Editor > Code style > PHP
Select the Wrapping and Braces tab
Under the "Function call arguments" Category uncheck "New Line after '('"

Related

How to do a breakline in yii2?

I'm trying to combine two data into one column. I already managed to combine two data in one column. But my problem is I don't know how to do a break line to separate the data. I have tried this way but doesn't work.
This is my code
'NAME_PROG_ENG'.'REMARKS' => ['label' => "Programme Name",'value' => function ($data) {return ($data->NAME_PROG_ENG)."\r\n".nl2br("Previously known as: ".($data->REMARKS));}],
I think you refer to GridView component.
Some explanation:
'NAME_PROG_ENG'.'REMARKS' =>
the array index is the model field name, you can't combine 2 columns just by concatenating the column name. In this way you just define a column in the grid wich is not connected to the model. This is not an error, is just to notice that this does not give you the expected result.
'value' => function ($data) {return ($data->NAME_PROG_ENG)."\r\n".nl2br("Previously known as: ".($data->REMARKS));}
Between $data->NAME_PROG_ENG and $data->REMARKS you put a "\r\n" which is fine, but you apply nl2br() function only to the second part of the string.
Now about Gridview. Text are automatically converted to entities, this is a security feature to avoid script injection from user input. If you plan to use input from an untrusted user, ensure that you clean it using htmlpurifier before saving it in database or before showing it (in your 'value'=>... function)
https://www.yiiframework.com/search?language=en&version=2.0&type=guide&q=htmlpurifier
You can bypass html entity conversion by specifies the output format as raw in grid column option
'format'=>'raw'
So you column definition should be something like
'prog_and_remark_combined' => [
'format' => 'raw',
'label' => "Programme Name",
'value' => function ($data) {
return nl2br(
$data->NAME_PROG_ENG .
"\r\nPreviously known as: " .
$data->REMARKS
);
}
],
Gridview allow a short notation
https://www.yiiframework.com/doc/api/2.0/yii-grid-gridview#$columns-detail
which is like this
<column name>:<fomat>:<label>
your code can be as follow as well
'prog_and_remark_combined:raw:Programme Name' => [
'value' => function ($data) {
return nl2br(
$data->NAME_PROG_ENG .
"\r\nPreviously known as: " .
$data->REMARKS
);
}
],
Last note, if the fields does not contain new line to be converted, just concatenate them without using nltobr() function
return $data->NAME_PROG_ENG . "<br>Previously known as: " . $data->REMARKS

Saving data to the join table using control options in CakePHP 3.x

I learned here how one can save the data to the fields of join table CoursesMemberships while adding or editing a student in CakePHP 3.x. In order to add grades for many courses I can do this in my add and edit forms:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
echo $this->Form->control('courses.1.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.1._joinData.grade');
echo $this->Form->control('courses.2.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.2._joinData.grade');
...
but this form:
has a fixed number of courses for each student;
requires to select the course id from the list ('type' => 'select');
adds all courses to the student record even if not attended (well, the corresponding grade field can be kept empty, but still).
Is there a way to have a simpler form, where all courses are listed and one can only checkbox the course attended and enter the corresponding grade? I found it very challenging using control...
EDIT:
After #ndm suggested a very nice method below, I implemented it in the add.ctp:
foreach ($courses as $key => $course) {
echo $this->Form->control('courses.'.$key.'.id', ['type' => 'checkbox', 'hiddenField' => false, 'value' => $key,
'label' => $key]);
echo $this->Form->control('courses.'.$key.'._joinData.grades');
}
and corrected StudentsTable.php accordingly. And it runs with no problems.
However, if I do the same in edit.ctp, the previously saved records (e.g. for 1, 3, 5 and 7 courses are now listed as 1, 2 and 3 showing the grades for former 3rd 5th and 7th courses and the form forces me to check those three boxes. I understand that the first record disappeared because my courses start with id=1 (and so does the $key in the loop) and 'courses.0.id' is thus missing, but the general problem is that the empty fields removed by beforeMarshal function are no longer recognized in edit.ctp form and I cannot find a reasonable way to edit the student's record.
There is no build in support for what you are trying to achieve, you'll have to come up with a custom solution, which will likely either require a mixture of form and marshalling logic, or JavaScript.
You could create for example a list of checkboxes, and use the id value (wich will be zero in case the checkbox isn't checked, or the ID in case it is checked) to remove unchecked entries from the submitted data before marshalling, something like this:
echo $this->Form->control('courses.0.id', [
'type' => 'checkbox',
'value' => $courses[0]->id,
'label' => $courses[0]->title
]);
echo $this->Form->control('courses.0._joinData.grade');
echo $this->Form->control('courses.1.id', [
'type' => 'checkbox',
'value' => $courses[1]->id,
'label' => $courses[1]->title
]);
echo $this->Form->control('courses.1._joinData.grade');
// ...
// in the `StudentsTable` class
public function beforeMarshal(\Cake\Event\Event $event, \ArrayObject $data, \ArrayObject $options)
{
forach ($data['courses'] as $key => $course) {
if (empty($course['id'])) {
unset($data['courses'][$key])
}
}
}
Alternatively you could use JavaScript to disable the controls related to the checkbox so that they aren't being submitted in the first place. For this to work properly you'll need to make sure that you disable the hidden field that is by default being generated for checkboxes (see the hiddenField option), as otherwise zero will be sent for unchecked checkboxes.
Here's a quick, untested jQuery example to illustrate the principle:
echo $this->Form->control('courses.0.id', [
'class' => 'course-checkbox',
'data-join-data-input' => '#course-join-data-0',
'type' => 'checkbox',
'hiddenField' => false, // no fallback, unchecked boxes aren't being submitted
'value' => $courses[0]->id,
'label' => $courses[0]->title
]);
echo $this->Form->control('courses.0._joinData.grade', [
'id' => 'course-join-data-0',
'disabled' => true
]);
// ...
$('.course-checkbox').each(function () {
var $checkbox = $(this);
var $joinDataInput = $($checkbox.data('join-data-input'));
$checkbox.on('change', function () {
$joinDataInput.prop('disabled', !$checkbox.prop('checked'));
});
});
See also
Cookbook > Database Access & ORM > Saving Data > Modifying Request Data Before Building Entities
Cookbook > Views > Helpers > Form > Creating Select, Checkbox and Radio Controls > Options for Control
Cookbook > Views > Helpers > Form > Creating Select, Checkbox and Radio Controls > Creating Checkboxes

kartik Select2 FilterType in gridview doesn't work

I have an issue about kartik select2 filterType that doesn't work , this is a portion of my code :
[
'format' => ['link',$idmodel],
'attribute' =>$attribute,
'filterType'=>GridView::FILTER_SELECT2,
'filterWidgetOptions' => [
'data' => \yii\helpers\ArrayHelper::map($model_::find()->all(),$attribute,'nom_'.strtolower($idmodel)),
'options' => ['placeholder' => '-'.$idmodel.'-'],
'pluginOptions' => ['allowClear' => true],
'theme' => Select2::THEME_KRAJEE,
],
When i comment this line :
'filterType'=>GridView::FILTER_SELECT2,
a text field appear in my filter grid view.
Can any one help me ??
Thanks in advance
As I see it from your conversation with Edvin Tenovimas your problem is that no jQuery Javascript file is loaded. Did you installed the Select2 package normally with composer?
$ php composer.phar require kartik-v/yii2-widget-select2 "#dev"
In that case you have to see the file vendor/bower/jquery/dist/jquery.min.js. The file has to be copied into your assets directory under web/assets automatically. You could remove the content of the assets directories to force the creation of the assets again.
$ rm -rf web/assets/*
I believe by default GridView enables filtering unless you manually disable it. When you comment that out, it uses default text input instead of Select2. To also disable that one, use this on columns you want to disable filtering:
'filter' => false,

Sublime text nice indents

Is there a plugin or built in function that changes
'test' => $test,
'test123' => $test123,
'something_else' => $something_else
to
'test' => $test,
'test123' => $test123,
'something_else' => $something_else
?
There are several alignment plugins available on Package Control, my personal favourite is AlignTab.
You can select code that you need format
Edit-> Line -> Reintend
But This is not more accurate

PHPStorm php-code indentation

I need these chunks of code to be indented properly like this:
$this->render('rights', array(
'admin' => $admin,
'editor' => $editor,
'author' => $author,
));
and widget snippet:
<?php $this->widget('zii.widgets.CMenu', array(
'items' => array(
array('label' => 'label', 'url' => 'url')
)
)); ?>
With default PHPStorm settings it indents this code like this:
$this->render('rights', array(
'admin' => $admin,
'editor' => $editor,
'author' => $author,
));
I went to Settings->Code Style->Wrapping and Braces and changed following options:
Array initializer -> Align when multiple (uncheck)
Method call arguments -> Align when multiple (uncheck)
The result is:
$this->render('rights', array(
'admin' => $admin,
'editor' => $editor,
'author' => $author,
));
Still not the style I want, but that's all I managed to accomplish. Can you please point me to the option I need to change?
Try selecting all the code and clicking :
Ctrl + Alt + I
It's auto indentation shortcut ...
It seems a to be a known issue. Please watch/vote or add your comments there.
I think this will help you in formatting
Your code
https://laracasts.com/series/how-to-be-awesome-in-phpstorm/episodes/8
I've found that unchecking the following option solves the issue for me:
Preferences > Editor > Code Style > PHP > Tab 'Wrapping and Braces' > Function/constructor call arguments > Align when multiline
This changes the following code:
var $numbers = $this->thing(array(
"one",
"two",
"three",
"four",
"five",
"six"
));
To be formatted like:
var $numbers = $this->thing(array(
"one",
"two",
"three",
"four",
"five",
"six"
));