Admin generator - visible or not action - php

config:
list:
object_actions:
extend: ~
_edit: ~
_delete: ~
Is possible to show action extend only if in database in this same record is set field visible on 1?
For example:
config:
list:
object_actions:
extend: if(this.status == 1 then SHOW else if (this.status == 0) then NOT SHOW
_edit: ~
_delete: ~
in database i have for example:
id | name | extend | visible
1 | aaa | 0 | 0
2 | bbb | 0 | 1
3 | ccc | 0 | 1
4 | aaa | 0 | 0
5 | bbb | 0 | 1
6 | ccc | 0 | 1
so if visible == 1 then should show me action extend in admin generator

Remember that you can overwrite any template auto-generated. Maybe it's not the best way, but that trick may be helpful many times.
Just browse the backend cache generated templates, copy to the template folder of your module and change whatever you want.
For example, assuming that your entity's name is "Entity", copy
*cache/backend/dev/modules/auto Entity/templates/_list_td_actions.php*
to your
apps/backend/modules/entity/templates folder.
As you can see, in that file there's a $entity var to add your logic.

Related

Fill array in PHP using Mysql

I'm developing an android application that uses PHP and makes requests with Mysql. Initially I tested all the features on the local server to then migrate it to an online server, in this case, Hostinger.
One of my PHP files returns the value of an array correctly on the local server, however, when using the same PHP file on the online server with the same database, the array only responds to TRUE when I run echo.
I tried doing the echo of each line and it is working perfectly, just not being able to associate the values with the keys in the array.
$result= mysqli_query($connect, $sql);
while ($patients= $result->fetch_assoc()){
echo $patients['group']; //prints OK
echo $patients['exercise_name']; //prints OK
echo $patients['use_exercise']; //prints OK
$insert[] = array("group" => $patients['group'],
"name" => $patients['exercise_name'],
"use" => $pacientes['use_exercise']
);
}
echo json_encode($insert); //prints TRUE
My mysql output from $result:
+------------------------------------------+
| group exercise_name use_exercise |
+------------------------------------------+
| Knee Knee _1 1 |
| Knee Knee _2 1 |
| Knee Knee _3 0 |
| Knee Knee _4 0 |
| Knee Knee _5 0 |
| Shoulder Shoulder_1 1 |
| Shoulder Shoulder_2 0 |
| Shoulder Shoulder_3 0 |
| Shoulder Shoulder_4 0 |
| Shoulder Shoulder_5 0 |
| Ankle Ankle _1 1 |
| Ankle Ankle _2 0 |
| Ankle Ankle _3 0 |
+------------------------------------------+
The rest of the PHP code is really not relevant to this. I ended up getting this result from it. My only problem is passing everything to the array. I've tested with another PHP file and it printed how it was supposed to, so I don't know what is happening.

Where to define Field values for CakePHP tables

I have a CakePHP application that uses Fields to store values like 0, 1, 2.
Table:
+----+--------+----------------+
| id | status | title |
+----+--------+----------------+
| 1 | 2 | something new |
| 2 | 1 | nsfw |
| 3 | 1 | a potato |
| 5 | 0 | the real thing |
+----+--------+----------------+
Entity/ Array:
$data = [
0 => 'not published',
1 => 'published',
2 => 'draft',
9 => 'option',
];
// Some public methods to get the data..
Template Form dropdown:
+----+---------------+
| id | value |
+----+---------------+
| 0 | not published |
| 1 | published |
| 2 | draft |
| 9 | option |
+----+---------------+
What I use in template:
echo $this->Form->input('status', ['options' => $article->getArticleStatusList()]);
Example:
articles table with a status field. The default values are: 0 not published, 1 published, etc. Defining those in Entity/Article. There is an array with the default values, so in the template file I call an Entity method that uses the array for the options Form input.
Time ago I was using a configuration array for this.
Is this a good way to accomplish the task? It should be stored in a ini file? Or in a Table/Model?
All works but I want to follow the MVC pattern. Thanks.
Put them in src\Model\ArticleStatus.php. At least for me a status is a list of one or more things that don't change very often. No need to put them in a DB table. These lists are data and clearly belong into the model layer of the MVC pattern.
IMHO it is good practice to use constants for them because you'll do a lot checks in the code against these values. String values are prone to typos and hard to distinguish from other domains. For example if you have two tables using a status of the same name but with a different meaning the code can become tricky to understand and also a search and replace won't work very well because you'll change both types for both domains.
For example we have a countries table with a lot additional info per country but use a list of constants of our ~18 most used countries we have to do conditional checks on because of our business. So we have src\Model\Table\CountriesTable.php but as well src\Model\Country.php. The reason for this is it becomes much much more readable and easier to understand what goes in the the code if you can write Country::GERMANY instead of just using an id like 5. I personally consider it as very bad practice to use hard coded id's everywhere in the code.
if ($country === 41 && $status === 3)
vs
if ($country === Country::GERMANY && $status === ArticleStatus::PUBLISHED)
I think we can agree on that readable and easy to understand code is much better than typing a few characters less. Honestly, people whining about a few characters should learn to type faster. I consider it just as a bad excuse. ;) Also using an IDE will autocomplete the class constants any way for you. It won't do that for an integer.
Here is an example that would even allow you to generate your list with translated labels:
<?php
namespace App\Model;
class ArticleStatus {
const PUBLISHED = 'published';
const DRAFT = 'draft';
// Add more as you like
public static function getStatuses() {
return [
static::PUBLISHED ,
static::DRAFT
];
}
public static function getKeyValueList() {
return [
static::PUBLISHED => __d('app', 'Published'),
static::DRAFT=> __d('app', 'Draft')
];
}
}
Use it in your controller and set it to your view or directly use it in the view.

Box/Spout questions

This is my first time using Box/Spout library. I am using WAMP server.
My question is the following:
require_once('./spout-master/src/Spout/Autoloader/autoload.php');
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Common\Type;
$filePath = 'test.xlsx';
$writer = WriterFactory::create(Type::XLSX);
$writer->openToFile($filePath);
[X]
$writer->addRow(['a'], $style);
$writer->close();
(1)
When I am running above code, I get the following error message:
Warning: rmdir(C:\WINDOWS\TEMP/xlsx560f58d588ceb): Permission denied in
C:\wamp\www\1300.revenue.com.my\public_html\spoutmaster\src\Spout\Common\Helper\FileSystemHelper.php on line 113
What is the errors means and how should I modify it to prevent this error message appeared?
(2) I want to make expected output like below:
But I didn't know how to write it on [X] part. How to write it in order to get the expected output?
It looks like the default temp folder used to generate the XLSX file cannot be deleted. You can verify it by checking the permissions on C:\WINDOWS\TEMP/xlsx560f58d588ceb.
To solve this issue, you can either manually fix the permissions on the temp folder (C:\WINDOWS\TEMP) or use another temporary folder, as specified here: https://github.com/box/spout#using-custom-temporary-folder
Regarding 2), there is no straightforward way to do this whith Spout. Spout does not support merging cells. The only thing you can do is:
| 1 | 2 | | 3 | |
|---|---|---|---|---|
| | A | B | A | B |
|---|---|---|---|---|
Or alternatively (if that makes more sense):
| 1 | 2 | 2 | 3 | 3 |
|---|---|---|---|---|
| 1 | A | B | A | B |
|---|---|---|---|---|
Either way, you'll have to format the rows as shown above: [[1,2,'',3',''], ['', 'A','B','A','B']] or [[1,2,2,3,3], [1, 'A','B','A','B']]

Session data doesn't persist - php configuration issue?

I have the following code snippet in my file:-
session_start();
$_SESSION['fromPanel'] = "abcd";
<form method="post" action="/m/xyz.php">
<div class="text inputcaption"><b><?= _("Email")?></b></div>
<input type="email" name="username" value="<?=$objMobile->GetEmail()?>">
</div>
</form>
When the file xyz.php is called on post request, the $_SESSION array is coming empty. Please note session_start(); was given in xyz.php before printing the session array. Why is the session array getting empty by itself.
Code for xyz.php:
<?php
session_start();
print "<pre>";
print_r($_SESSION);
print "<post>";
?>
Please find the session settings obtained through phpinfo();
|---------------------------------|---------------------|---------------------|
| Session Support | enabled | |
| Registered | save handlers | files user |
| Registered | serializer handlers | php php_binary wddx |
| | | |
|---------------------------------|---------------------|---------------------|
| Directive | Local Value | Master Value |
|---------------------------------|---------------------|---------------------|
| session.auto_start | Off | Off |
| session.bug_compat_42 | On | On |
| session.bug_compat_warn | On | On |
| session.cache_expire | 180 | 180 |
| session.cache_limiter | nocache | nocache |
| session.cookie_domain | no value | no value |
| session.cookie_httponly | Off | Off |
| session.cookie_lifetime | 0 | 0 |
| session.cookie_path | / | / |
| session.cookie_secure | Off | Off |
| session.entropy_file | no value | no value |
| session.entropy_length | 0 | 0 |
| session.gc_divisor | 100 | 100 |
| session.gc_maxlifetime | 1440 | 1440 |
| session.gc_probability | 1 | 1 |
| session.hash_bits_per_character | 4 | 4 |
| session.hash_function | 0 | 0 |
| session.name | PHPSESSID | PHPSESSID |
| session.referer_check | no value | no value |
| session.save_handler | files | files |
| session.save_path | C:\Windows\Temp | C:\Windows\Temp |
| session.serialize_handler | php | php |
| session.use_cookies | On | On |
| session.use_only_cookies | Off | Off |
| session.use_trans_sid | 0 | 0 |
|---------------------------------|---------------------|---------------------|
On enabling error_reporting, following warnings were seen:
Warning: Unknown: open(C:\Windows\Temp\sess_1037ca26d3ebb62017eddc9cbfb107e2, O_RDWR) failed: Invalid argument (22) in Unknown on line 0
Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\Windows\Temp) in Unknown on line 0
I converted your snippet to working code and it runs like a charm.
Please provide a complete non-working example so we can help you.
a.php:
<?php
session_start();
$_SESSION['fromPanel'] = "abcd";
?>
<form method="post" action="./b.php">
<input type="email" name="username" value="john#example.com">
</form>
b.php:
<?php
session_start();
var_dump( $_SESSION['fromPanel'] );
var_dump( $_POST['username'] );
echo 'back to form';
result output after sending the form (with enter in input):
string 'abcd' (length=4)
string 'john#example.com' (length=16)
back to form
N.B: Use <label for="id-of-form">caption</label> instead of <div class="text inputcaption"></div>
More things to check:
Does the browser you are using accept cookies?
Clear cache and cookies, call session_regenerate_id() after session_start() to make sure it's not some old testing stuff that gives you trouble.
Is the session.save_path set and writeable? Check your phpinfo(); output.
Is xyz.php on the same domain? Cookies not allowed crosss domain.
Post your session.xyz settings.
Is this your own server? Try it on a hosted webspace or vice versa.
make sure there is no whitespace before the session_start() call.
call error_reporting(E_ALL) first thing.
make your example publicly available so others can check, too.
Go and have a beer. It will work next week. ;)
If you are including a common file in all the views like header.php or common.php just write session_start() at the top.

Receive results of select in shell

Ok, I have such script
#!/bin/bash
keyOrPass=$1
intercom=$2
flat=$3
number=$4
mysql -ulogin -ppass db_name -e "select cli.codeGuestEmail, cli.codePrivateEmail, cliKey.rf_id, cliKey.emailNotification from mbus_clients as cli join mbusClientKeys as cliKey on cliKey.id_client=cli.id WHERE cli.flat=${flat} and cli.domophone=${intercom};";
php -q sendNotifications.php
It works fine, but I should pass some result fields from select into php arguments. Any ideas how to do it?
OUTPUT:
+----------------+------------------+------------+-------------------+
| codeGuestEmail | codePrivateEmail | rf_id | emailNotification |
+----------------+------------------+------------+-------------------+
| 1 | 0 | 2774490192 | 0 |
| 1 | 0 | 2774490193 | 0 |
| 1 | 0 | 2774490194 | 0 |
| 1 | 0 | 2774490195 | 1 |
+----------------+------------------+------------+-------------------+
mysql is capable of generating output formatted differently. With the -B or --batch option, mysql produces the output with TAB as the column separator. The special characters in the fields are escaped (e.g. TAB is output as "\t") so you can use cut to extract fields.
Many times in cases like this it's helpful to use the -N or --skip-column-names option as well, in order to remove column names from the output.

Categories