I'd like to create a link in my grid to download uploaded files.
There is already several topics on it and the only solution that worked for me is: https://groups.google.com/forum/#!msg/agile-toolkit-devel/degomDwwe1s/gGtcap-T27sJ
But I'd like more of an abstract solution which could work whatever the field name is. So I searched for adding formatter : ATK4 How to set up custom formatters?
But it is never used...
I tried many things (v.4.2.5):
//in my model
$this->add('filestore\Field_File', 'file_id')
->display(array('form'=>'upload','grid'=>myField));
$this->hasOne('filestore\File', 'file_id', 'id')
->display(array('form'=>'upload', 'grid'=>'myField'));
//in filestore\FIELD_FILE
$this->display(array('form'=>'upload', 'grid'=>'myField'));
//my grid
class GridFile extends Grid
{
function format_myField($field)
{
$fm = $this->add('filestore/Model_File');
$fm->load($this->current_row[$field]);
$src = $fm->getPath();
$name = $fm->get('original_filename');
$this->current_row_html[$field] = "<a href='$src'>$name</a>";
}
}
Model:
$this->addField("ipv4")
->display(["grid"=>"foo"]);
Page:
$g=$this->add("CRUD", ["grid_class"=>"XGrid"]);
XGrid:
class XGrid extends Grid {
function format_foo($a,$b,$c=null){
$this->current_row_html[$a] = "<b>".$this->current_row[$a]."</b>";
}
}
Works like charm - perhaps your crud/grid not using the extended version. P.s. using latest atk version (master branch from git)
Related
Hi i am working on a project in laravel 7.0, in back-end i have a table called Posts which contains 2 text language input one in french and the other is arabic added by the back-end application.
what i am trying to do is when the user uses the French Language i want the title_fr to be displayed on the view and same thing in Arabic language the title should be title_ar.
P.S data are stored in French and Arabic
I have tried the similar solution given in an other similar question but none of it worked in my case!
Any idea how i might get this to work ?
Thanks in advance.
You can do something similar to below. We have a model Post, this model has an attribute title. I also assume that you have an attribute that will return user's language from the User model.
class Post extends Model
{
public function getTitleAttribute(): string
{
return Auth::user()->language === 'fr' ? $this->title_fr : $this->title_ar;
}
}
FYI above is just a demo on what can be done. For a full blow solution I would recommend decorator pattern.
Also it might be worth considering using morph for things like that. You can have a service provider that will initiate the morph map for you post model relevant to the language that user has, I.e.
Class ModelProvider {
Protected $models = [
‘fr’ => [
‘post’ => App/Models/Fr/Post::class,
],
‘ar’ => [
‘post’ => App/Models/Ar/Post::class,
]
];
Public function boot() {
$language = Auth::user()->Settings->language;
Relation::morphMap($This->models[$language]);
}
}
Afterwards you just need to call to Relation::getMorphModel(‘post’) to grab Post class that will return correct language.
I.e. App/Models/Fr/Post can have a an attribute title:
Public function getTitleAttribute(): string {
Return $this->title_fr;
}
For example above you would also want to utilise interfaces to make sure that all models follow the same contract, something below would do the trick:
Interface I18nPostInterface {
Public function getTitleAttribute(): string
}
Also, depending on the database you use, to store titles (and other language data) in a JSON format in the database. MySQL 8 has an improve support for JSON data, but there are limitations with that.
So I was Able to fetch data from my database based on the Language selected by the user.
Like i said before I have a table called Posts and has columns id,title_fr and title_ar. I am using laravel Localization.
Inside my PostController in the index function i added this code:
public function index()
{
//
$post = Post::all();
$Frtitle = post::get()->pluck('title_fr');
$Artitle = post::get()->pluck('title_ar');
return view('post.index',compact('post','Frtitle','Artitle'));
}
if anyone has a better way then mine please let me know, i am sure
there is a better way.
we are using subdirectories in our projects no separete views and controllers but in models we didn’t learn yet. Recently I’ve found this https://github.com/cakephp/cakephp/issues/60451 and actually routes and plugins we are already using, we just want to separete our models like this:
Model
-Entity
–Financial
—Money.php
-Table
–Financial
—MoneyTable.php
I’ve tryed put like this then controller is not able to find his model. How can I do to organize it, and make it work?
Things that we've tried:
Use $this->setAlias('TableModel');
Call in controller:
$this->TableModel = $this->loadModel('Subfolder/TableModel');
didn't work for SQL build, and other classes.
CakePHP uses the TableRegister to load models. That class can be configured to use a class that implements the LocatorInterface, and CakePHP uses the TableLocator as the default.
The only thing you can do is configure your own LocatorInterface instance in your bootstrap.php. You would have to create your MyTableLocator and have it change the className for tables to point to subdirectories. What rules for this class name rewritting are used is purely up to you.
bootstrap.php:
TableRegister::setTableLocator(new MyTableLocator());
MyTableLocator.php:
class MyTableLocator extends TableLocator {
protected function _getClassName($alias, array $options = [])
{
if($alias === 'Subfolder/TableModel') {
return TableModel::class;
}
return parent::_getClassName($alias, $options);
}
}
The above isn't working code.
I'm just demonstrating what the function is you need to override, and that you need logic in place to return a different class name.
You can check if the $alias contains the / character, and if so. Return a class name by extracting the subfolder name from the $alias. Take a look at the TableLocator to see how it's using the App::className function.
I am using Telerik's dropdownlist in my MVC application View. I am facing two problems:
1) When I run my application, I find every value of kendo dropdownlist is "Undefined".
This is the code for my View:
#model IEnumerable<EulenMgrKendoUIMvcApplication.Dominio.Tablas.DelegacionProductoUsuario>
#(Html.Kendo().DropDownListFor(d=>d)
.Name("IdDelegacionProductoDrpDwn").HtmlAttributes(new { #style = "font-size:12px" })
.DataTextField("IdDelegacionProducto")
.DataValueField("IdDelegacionProducto")
**.BindTo((System.Collections.IEnumerable)ViewData["IdDelegacionProducto"]))**
This is my controller, where I populate the dropdownlist:
public class DelegacionProductoUsuarioController : Controller
public ViewResult List()
{
IEnumerable<DelegacionProductoUsuario> delegaciones = DelegacionProductoUsuario.GetAll();
**PopulateDelegacionProducto();**
return View(delegaciones);
}
private void PopulateDelegacionProducto()
{
List<Int64> IdDelegacionProductoList = new List<Int64>();
foreach( DelegacionProductoUsuario d in DelegacionProductoUsuario.GetAll()){
IdDelegacionProductoList.Add(d.IdDelegacionProducto);
}
ViewData["IdDelegacionProducto"] =IdDelegacionProductoList ;
}
}
>I am debugging the application and the controller is passing to the view the proper values,so I don't understand why it doesn't show them.
2) Second problem: I insert this Dropdownlist in one of the columns of a kendo grid with no success.
In it's place it appears a common label. Here is the code for my Grid, I mark in Bold the column where I try to show my dropdownList:
#(Html.Kendo().Grid(Model)
.Name("Grid")
.Columns(columns=>
{
columns.Bound(d => d.BorradoLogico).Title("Borrado logico");
columns.Bound(d => d.FTick).Title("Ftick");
**columns.Bound(d => d.IdDelegacionProducto).Title("IdDelegacionProducto").EditorTemplateName("IdDelegacionProductoDrpDwn");**
columns.Bound(d => d.IdUsuario).Title("IdUsuario");
})
How does that 'DelegacionProductoUsuario' class look like? Does it have property named 'IdDelegacionProducto' ? It looks like you have not set the dataValueField correctly.
As for the second question, where did you put that EditorTemplate (is it in the Shared/EditorTemplate or in a EditorTemplates folder? More info about editor template can be found here.
Dear Petur: thanks a lot for answering. On regard to your answer:
My class DelegacionProductoUsuario does have a property called IdDelegacionProducto. On regard to your question "where I place the EditorTemplate" , I don't understand what you mean, I place it in the view that Lists all of my DelegacionProductoUsuario . Please keep on helping me. Thanks a lot Petur.
I know that I can replace the flash markup by creating something like custom_flash.ctp in Elements folder and call it like:
$this->Session->setFlash('Hello', custom_flash)
But how can I use custom layout when not adding the second parameter?
$this->Session->setFlash('Hello')
I thought I can replace the default by having a file named default.ctp inside Elements folder. But I can't.
I want to keep the code as short as possible. That's why I'm looking a way to do this
Any solution? Thanks
Try to create your Component:
class MySessionComponent extends Session {
public function setFlash($message) {
return $this->setFlash($message, 'custom_flash');
}
}
and than in your controller just use:
public $components = array('MySession');
$this->MySession->setFlash('Hello');
I found the answer from this question.
We need to add this codes in app/Controller/AppController.php
function beforeRender(){
if ($this->Session->check('Message.flash')) {
$flash = $this->Session->read('Message.flash');
if ($flash['element'] == 'default') {
$flash['element'] = 'fileNameOfYourCustomFlash';
$this->Session->write('Message.flash', $flash);
}
}
}
It basically add element parameter in flash when it doesn't exist yet.
This is explained on the cakephp website here
My app has sales listing functionality that will allow the user to add 1 or more photos for the product that they want to sell.
I'm attempting to use the upload/filestore_image of ATK with a Join table to create the relationship - my models:
class Model_Listing extends Model_Table {
public $entity_code='listing';
function init(){
parent::init();
$this->addField('name');
$this->addField('body')->type('text');
$this->addField('status');
$this->addField('showStatus')->calculated(true);
}
function calculate_showStatus(){
return ($this->status == 1) ? "Sold" : "For Sale" ;
}
}
class Model_listingimages extends Model_Table {
public $entity_code='listing_images';
function init(){
parent::init();
$this->addField('listing_id')->refModel('Model_Listing');
$this->addField('filestore_image_id')->refModel('Model_Filestore_Image');
}
}
In my page manager class I have added the file upload to the crud:
class page_manager extends Page {
function init(){
parent::init();
$tabs=$this->add('Tabs');
$s = $tabs->addTab('Sales')->add('CRUD');
$s->setModel('Listing',array('name','body','status'),array('name','status'));
if ($s->form) {
$f = $s->form;
$f->addField('upload','Add Photos')->setModel('Filestore_Image');
$f->add('FileGrid')->setModel('Filestore_Image');
}
}
}
My questions:
I am getting a "Unable to include FileGrid.php" error - I want the user to be able to see the images that they have uploaded and hoped that this would be the best way to do so - by adding the file grid to bottom of the form. - EDIT - ignore this question, I created a FileGrid class based on the code in the example link below - that fixed the issue.
How do I make the association between the CRUD form so that a submit will save the uploaded files and create entries in the join table?
I have installed the latest release of ATK4, added the 4 filestore tables to the db and referenced the following page in the documentation http://codepad.agiletoolkit.org/image
TIA
PG
By creating model based on Filestore_File
You need to specify a proper model. By proper I mean:
It must be extending Model_Filestore_File
It must have MasterField set to link it with your entry
In this case, however you must know the referenced ID when the images are being uploaded, so it won't work if you upload image before creating record. Just to give you idea the code would look
$mymodel=$this->add('Model_listingimages');
$mymodel->setMasterField('listing_id',$listing_id);
$upload_field->setModel($mymodel);
$upload_field->allowMultiple();
This way all the images uploaded through the field will automatically be associated with your listing. You will need to inherit model from Model_Filestore_File. The Model_Filestore_Image is a really great example which you can use. You should add related entity (join) and define fields in that table.
There is other way too:
By doing some extra work in linking images
When form is submitted, you can retrieve list of file IDs by simply getting them.
$form->get('add_photos')
Inside form submission handler you can perform some manual insertion into listingimages.
$form->onSubmit(function($form) uses($listing_id){
$photos = explode(',',$form->get('add_photos'));
$m=$form->add('Model_listingimages');
foreach($photos as $photo_id){
$m->unloadDdata()->set('listing_id',$listing_id)
->set('filestore_image_id',$photo_id)->update();
}
}); // I'm not sure if this will be called by CRUD, which has
// it's own form submit handler, but give it a try.
You must be careful, through, if you use global model inside the upload field without restrictions, then user can access or delete images uploaded by other users. If you use file model with MVCGrid you should see what files they can theoretically get access to. That's normal and that's why I recommend using the first method described above.
NOTE: you should not use spaces in file name, 2nd argument to addField, it breaks javascript.