Property structure reorder in Octobercms - php

I am using Octobercms/Wintercms and I identified in the documentation (https://docs.octobercms.com/2.x/backend/reorder.html#configuring-a-behavior) that there is the possibility of reordering the records of a relationship, adding only one structure option in the config_relation.yaml file, however I have already added this configuration in N ways and it does not work, it does not appear in the list of the data the drag and drop option.
Attempts I made:
# ===================================
# Relation Behavior Config
# ===================================
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
Attempts 2:
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
Attempts 3
tickets:
label: 'cashless.events::lang.relations.tickets'
view:
list: $/cashless/events/models/ticket/columns.yaml
toolbarButtons: create|delete
manage:
form: $/cashless/events/models/ticket/fields.yaml
recordsPerPage: 10
structure:
showTree: true
showReorder: true
maxDepth: 2
dragRow: true
Nothing works.

Related

Create new list field type for EasyAdmin

With EasyAdmin Symfony bundle, with Symfony 4.2, how create a new list field type?
Use case
"I want to display a link to show page in the list table"
(not a form type, a list type):
easy_admin:
entities:
offer:
class: App\Entity\Offer
list:
fields:
- { property: name, type: MY_TYPE??? }
You have 2 solutions i believe :
If the url is stored in your object there is a custom type for this :
https://symfony.com/doc/2.x/bundles/EasyAdminBundle/book/list-search-show-configuration.html#url-data-type
It allows you to display an url :
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
class: App\Entity\User
list:
fields:
- { property: 'blogUrl', type: 'url' }
If you don't have the full url you can try by using a custom template :
https://symfony.com/doc/2.x/bundles/EasyAdminBundle/tutorials/custom-property-options.html#custom-property-options
This way you can define a custom template to generate your url and pass a param if you need :
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
class: App\Entity\Product
list:
fields:
# ...
- { property: 'tags', template: 'admin/tag_collection.html.twig',
label_colors: ['primary', 'success', 'info'] }

Seed multiple tables from one backend/builder form?

I'm trying to insert data into multiple related tables from one (builder) back end form/model - but it seems I'm doing something wrong.
t1 posts (used for model Posts.php)
id, flag1, flag2 , created_at, updated_at
t2 post_content (used for model Posts_content.php)
id, content
I've tried expanding the model (Posts.php) used for the form as explained in the relations documentation of octobercms like so:
public $hasOne = ['content' => ['Test\Gcn\Models\Post_content', 'key' => 'id', 'otherKey' => 'id']];
While this does not produce an error when a record is created via the backend controller, no data is actually written to posts_content.
I've also tried to solve it with a proxy field
fields.yaml (of model Posts.php)
post_content[content]:
label: 'Post here'
size: ''
mode: tab
span: full
type: markdown
Posts.php (with the proxy field)
public function formExtendModel($model)
{
/*
* Init proxy field model if we are creating the model
*/
if ($this->action == 'create') {
$model->post_content = new Post_content;
}
return $model;
}
According to the error message, the array needs to be set to be jsonable. But even after that it looks like it's trying to insert the data in the wrong table.
What is the proper way to achieve this?
I'm trying to have one form where a user can enter some flags (checkboxes and the like) and a text field which should be inserted into post_content table with the correct id.
I appreciate your time and help, thank you!
From my opinion, you have to do many changes in your structure like below:
1) Add content_id in your posts table
id, flag1, flag2, content_id, created_at, updated_at
And second table should be contents table
id, content
content_id will be used to create one to one relationship between Post and Content. For more information about relations click here.
2) Then, you have to define models Post for posts and Content for all contents.
In Content model give One to one relationship.
public $hasOne = [
'post' => 'Test\Gcn\Models\Post'
];
If you give this type of relation, this will find content_id in your Post model so it set direct relationship between Post and Content.
3) Your form field should be like
In Post model fields.yaml should be
fields:
flag1:
label: 'Flag 1'
oc.commentPosition: ''
span: left
type: text
flag2:
label: 'Flag 2'
oc.commentPosition: ''
span: auto
type: text
And In Content model fields.yaml should be
fields:
post:
label: Post
oc.commentPosition: ''
nameFrom: flag1
descriptionFrom: description
span: auto
type: relation
content:
label: Content
size: small
oc.commentPosition: ''
span: left
type: textarea
and columns.yaml should be
columns:
content:
label: Content
type: text
searchable: true
sortable: true
post:
label: Post
select: flag1
relation: post
searchable: true
sortable: true
For more details about the backend, you can go here...1) form relation 2) column relation.
This all is from my side. Now analyze all this and try your code. And tell me if you have any query.
Thank you.

OctoberCMS - Accessing model data in back-end... Returning null?

The problem that I'm having is, I cannot access certain information from the File Model such as File Path, File Name, and other information stored in the system_files table. I believe I have pinpointed where the issue is coming from, but I'm not sure how to fix it. Below I'll show some of the code.
1) File.php ~ File Model
<?php namespace Compuflex\Downloads\Models;
use Model;
use October\Rain\Database\Attach\File as FileBase;
/**
* Model
*/
class File extends FileBase
{
use \October\Rain\Database\Traits\Validation;
/*
* Validation
*/
public $rules = [
];
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* #var string The database table used by the model.
*/
public $table = 'compuflex_downloads_files';
public $attachOne = [
'file' => ['System\Models\File',
'key' => 'id']
];
public $belongsToMany = [
'user' => ['RainLab\User\Models\User']
];
}
In this file, you can see there are two relationships identified: $attachOne which attaches the file upload to the File Model as well as $belongsToMany which identifies the many-to-many relationship between users and files. (Many files can belong to Many users, or one user can have many files, but one file can also belong to many users). This is the file that sets up the file and user relation which you will see in the columns.yaml file. Just a side note, the only reason I set the 'key' =>'id' was to ensure that it was actually identifying the 'key' as 'id'.
2) Columns and Fields
I don't think the fields.yaml file is necessary, but just in case, you can find it here:
fields.yaml
columns.yaml
columns:
file_name:
label: 'compuflex.downloads::lang.file.name'
type: text
searchable: true
sortable: true
full_name:
label: 'compuflex.downloads::lang.file.username'
type: text
searchable: true
sortable: true
select: name
relation: user
email:
label: 'compuflex.downloads::lang.file.email'
type: text
searchable: true
sortable: true
select: email
relation: user
file:
label: 'compuflex.downloads::lang.file.path'
type: text
searchable: true
sortable: true
select: file_name
relation: file
I'm more concerned with the columns.yaml file because that is what I am trying to fix is the back-end controller that displays the list of file information as well as information about the users they're "attached" to.
Here is a screenshot of what the view looks like:
As you can see, the "File" Tab is not displaying any information about the file, and it is supposed to be displaying the file_name column from the system_files table or at least from the File Model, but it displays nothing.
Now, what I find interesting, if I change the last entry in the columns.yaml file from the original to:
file:
label: 'compuflex.downloads::lang.file.path'
type: text
searchable: true
sortable: true
Then the following is output in the back-end controller:
Therefore, as you can see, the information IS there (allegedly), but I'm just not sure how to access it properly.
So, I have one last test that I did that I will show you, I knew that if the select: attribute in the columns.yaml file is set to a column name that's not in the system_files table it should produce an SQL Error of some sort (such as column name not found). And I was right... So, I set it to 'ile_name' instead of file_name, just for testing purposes.
Here is what the error message was:
SQLSTATE[HY000]: General error: 1 no such column: ile_name (SQL: select "compuflex_downloads_files".*, (select group_concat(name, ', ')
from "users" inner join "file_user" on "users"."id" = "file_user"."user_id" where "file_user"."file_id" =
"compuflex_downloads_files"."id") as "full_name", (select group_concat(email, ', ') from "users" inner join "file_user" on "users"."id" =
"file_user"."user_id" where "file_user"."file_id" = "compuflex_downloads_files"."id") as "email", (select ile_name from "system_files"
where "system_files"."attachment_id" = "compuflex_downloads_files"."id" and "system_files"."attachment_type" = ? and "field" = ?) as
"file" from "compuflex_downloads_files" order by "file_name" desc)
Now, to pinpoint the issue more, I believe the issue might be coming from:
(select ile_name from "system_files" where "system_files"."attachment_id" = "compuflex_downloads_files"."id" and "system_files"."attachment_type" = ? and "field" = ?)
I think that those ? are causing this query to return NULL results from the table in turn causing there to be no output. I'm not 100% on this, it's just one possibility. The problem is, I don't know where OctoberCMS builds these queries or if there is any simple way to fix that without touching the actual code for October (for obvious reasons).
Just a note: I apologize if I've included TOO much information here, but I have tried to cut it down. I just wanted to show you that I DID try to troubleshoot and solve this myself, but to no avail.
You are too close, you just have to change type: text to type: relation, also you have to set nameFrom attribute:
file:
label: 'compuflex.downloads::lang.file.path'
type: relation
searchable: true
sortable: true
nameFrom: file_name
relation: file

OCTOBERCMS Dropdown options dependent on selected value on other dropdown

I'm stuck with this problem and i can't figure out how to solve after some time searching for a example.
The two dropdowns options are table dependent on their values.
I have the one table with 'area' values (nested with simple tree working ok) with the following structure on fields.yaml file:
fields:
id:
label: Número
oc.commentPosition: ''
span: auto
disabled: 1
type: number
area_id:
label: 'Parente de'
oc.commentPosition: ''
emptyOption: 'Sem valor'
span: auto
type: dropdown
area:
label: Área
oc.commentPosition: ''
span: full
required: 1
type: text
I also have another table 'modulos' values with the following structure in fields.yaml:
fields:
modulo:
label: Módulo
oc.commentPosition: ''
span: auto
required: 1
type: text
area:
label: Área
oc.commentPosition: ''
nameFrom: area
emptyOption: 'Sem valor'
span: auto
descriptionFrom: id
type: relation
In the 'Area' model I have:
...
public $hasMany = [
'modulos' => ['JML\Gkb\Models\Modulos']
];
In the 'Modulos' model I have
....
public $belongsTo = [
'area' => ['\JML\Gkb\Models\Area']
];
I have other model that have relations with previous fields and two dropdown fields working ok without any filter, and the troubleshoting field (modulos) where I can't find a way to filter based on the values of 'Area' dropdown I have the following in fields.yaml.
....
modulo_id:
label: mod
oc.commentPosition: ''
emptyOption: 'Sem valor'
span: auto
required: 1
dependsOn:
area
type: dropdown
tab: Geral
In my models PHP file where I have the dropdowns defined, I have:
public function getModuloIdOptions() {
return Modulos::where('area_id', '=', $this->area)->lists('modulo', 'id');
}
That to me seems logical (maybe not) and I tried with DB also and many more other. I tried with dd() to see if I can get the values from the first dropdown to no avail. If I try to filter the values, no value appears at all (except an empty value).
Any help out there ???
TIA
JL
The dataset is passed as the second argument to get the "getOptions" method. Here is an alternative approach that may work:
public function getModuloIdOptions($value, $data) {
return Modulos::where('area_id', '=', array_get($data, 'area'))->lists('modulo', 'id');
}
You may also want to try accessing the area_id value:
public function getModuloIdOptions(){
return Modulos::where('area_id', '=', $this->area_id)->lists('modulo', 'id');
}
Or less efficiently the area->id value (may require exception handling):
public function getModuloIdOptions(){
return Modulos::where('area_id', '=', $this->area->id)->lists('modulo', 'id');
}
I solved the problem with that dropdown and others with the same objective with the following steps:
I have the the 'Relation' widget on them. Changed them to 'Dropdown' widget.
Defined the 'Depends on' field.
Defined the 'Preset' field to the one above. I think that is the missing link to the problem solution not documented anywhere and i get there on try/error basis (may be it is valuable to add this to the October documentation).
Filter options with the snipet code on the end of my question or the second snipet of Samuel answer.
That solved my problem.
Thanks all.

Symfony backend: Load data on combo box depending on another

Im starting with symfony and im a little lost here. Of course nothing that a good tutorial can manage. However i've come to a point that my tutorial doesn't cover.
Creating the backend i write this generator.yml
...product/config/generator.yml
generator:
class: sfDoctrineGenerator
param:
model_class: Product
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: product
with_doctrine_route: true
actions_base_class: sfActions
config:
actions:
fields:
product_type_id: {label: Product Type}
product_name: {label: Product Name}
list:
display: [product_type_id, category, product_name, created_at, updated_at]
filter: ~
form: ~
edit: ~
new:
display: [product_type_id, product_name, url, description, _category]
The last field is rendered throught a custom file _category.php
<?php
echo '<HTML CODE><select><option>...</option>';
$query = Doctrine_Core::getTable('category')
->createQuery('c')
->where("id <> 1");
$quer= $query->fetchArray();
foreach($quer as $q) {
echo '<option value="'.$q['id'].'">'.$q['category_name'].'</option>';
}
echo '</select> </HTML CODE>'
?>
It give a simple form to introduce the data, and its ok. But i was trying that the first combo box on the "new" action (product_type_id) allow me to modify the query of categories to display only those that match.
I wonder if i can access the value of product_type_id invoking some sfFuction or doctrine... or something.
Thanks in advance for your help.

Categories