Making a dynamic Form in Prestashop - prestashop

What I want to achieve Image! Hi I am working on a module in prestashop. What I want is that there should be a button (any-name: Add new field) in the backoffice-(Module Configuration page the first page that comes when you configure your module) that the user presses and it should add a new input field in the form. This all should be done using the helper classes. How can I achieve that? Some code would be grateful! I have uploaded an image. I have tried it using jQuery and it works but I need this done using helperForms in prestashop! In jQuery if I press the add new field button it adds an input field dynamically but the helper-classes are not used in that case.

Here is a link to the documentation :
http://doc.prestashop.com/display/PS16/Using+the+Helper+classes
the documentation is for PS 1.6 but it should also work with PS 1.7
Here is an example of code that generate a form with Helper Classes :
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Edit carrier'),
'image' => '../img/admin/icon_to_display.gif'
),
'input' => array(
array(
'type' => 'text',
'name' => 'shipping_method',
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
I don’t know if you can use Helper class for custom forms that update custom datas. I’m sure you can use them to update module configuration and prestashop object (customer, etc.)
Helper classes enable you to generate standard HTML elements for the
back office as well as for module configuration pages.

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.

Using Yii2 Bootstrap widgets with Smarty

I've installed Yii2 together with Smarty for my views.
Smarty itself is working, but I can't figure out how to use the bootstrap widgets with smarty, nor find any examples.
With the default Yii renderer the widgets work great, and lots of examples are available. But when using Smarty templates documentation is nearly non existing.
How would I define this example with Smarty ?
echo Alert::widget([
'options' => [
'class' => 'alert-info',
],
'body' => 'Alert widget',
]);
Obviously the first thing would be
{use class="yii\bootstrap\Alert"}
But I can't find an example of how to define the widget itself.
You should simply try this :
{use class='#yii\bootstrap\Alert' type='function'}
{Alert body='Alert widget' options=['class' => 'alert-info']}
Read more : http://www.yiiframework.com/doc-2.0/guide-tutorial-template-engines.html#importing-static-classes-using-widgets-as-functions-and-blocks
After changing config/web.php and adding:
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
in the view section, it works.
{use class='yii\bootstrap\Alert' type='function'}
{Alert body='Alert' options=['class' => 'alert-info']}
So without the # soju suggested.

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 form builder

I've been working on Yii-based application. And I've faced with weird thing...
What I'm trying to do is to add input(type=file) into form. Form is created via form builder(CForm class). But input is not going to appear.
My code. Controller/action:
$model=MyModel::model()->findByPk(700);
$model->scenario='my-scenario';
$form=new CForm('path.to.forms.my-form', $model);
$this->render('view', array('form'=>$form));
View:
echo $form
Form config:
return array(
'attributes' => array(
'enctype' => 'multipart/form-data',
),
'elements' => array(
'name' => array(
'type' => 'text',
),
'image' => array(
'type' => 'file',
),
),
'buttons' => array(
'save' => array(
'type' => 'submit',
'label' => 'Save',
),
),
);
Model:
//.....
public $image;
// ....
public function rules()
{
return array(
//...
array('image', 'file', 'types'=>'png', 'on'=>'my-scenario'),
);
}
With code above I expected to see two fields - text and file. But only text one appears.
If I change file validator to, say, required - it works, but I need file validator.
I'm using Yii version 1.1.13.
The most intresting that code above works as expected with earlier Yii(1.1.9). Is this a known bug in new version? If yes - is there a solution? or do I have to rollback to previous version?
Thanks in advance.
UPDATE:
If you add a second validator (one for file and one for required) does it work?
No, it doesn't. I believe I found why. See bellow.
It seems to be caused by this line in CForm..
Yes, correct. Yesterday, armed with debugger I went deeper:)
CFormElement::getVisible() eventually calls CModel::isAttributeSafe() and CModel::getSafeAttributeNames().
CForm::getSafeAttributeNames() gets all model validators and leaves only safe ones. As we can see CFileValidator is not safe.
So, it doesn't matter how many safe validators(required or any other) have attribute assigned. CForm::getSafeAttributeNames() removes it from whitelist if there is at least one unsafe(file).
File validator is unsafe since 1.1.12 version. That is why it worked perfectly for me in 1.1.9 :)
Hence the problem is in CFileValidator(or at least connected with it) and not in CForm.
The only one solution I can see so far is creating own validator extended from CFileValidator marked safe and using it instead of built in. But I can't even imagine what problems it may cause(I believe Yii developers had a good reason for making it unsafe).
I hope this will be helpful for somebody.
UPDATE 2
array('image', 'file', 'safe'=>true, 'types'=>'png', 'on'=>'my-scenario')
this validation rule(explicit safe=true) also works.
If you add a second validator (one for file and one for required) does it work? I ran into this recently myself.
It seems to be caused by this line in CForm:
if($element->getVisible())
{
...
}
The getVisible checks the active validators for this current model scenario and leaves out any inputs that aren't used in the current validator rules. I've ended up commenting things out in our custom CForm model for our system, but if let me know if you find something that works better for you.
Issue has been posted on github
https://github.com/yiisoft/yii/issues/2089

Using Elfinder extension in Yii

I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget