I'm using the bundle "JMSSerializerBundle" for export retrieved entity data. I can export the results as json, xml or yml successfully.
But i need also a csv export for this results. This bundle can't handle csv export. But i'm also not sure how can i convert json,xml or yml to csv, because csv is a flat file.
Have anyone solve this issue before?
Update
Now i create an solution for my problem, i'm partially satisfied with my solution, because it is not recursive. I will show it.
if ($format == 'csv') {
$json = $serializer->serialize($query, 'json', SerializationContext::create()->enableMaxDepthChecks());
return $this->toCsv(json_decode($json, true));
}
/**
* #todo write an recursive function for deeper levels
*
* #param $data
* #return array
*/
protected function toCsv($data)
{
$headers = array();
$outerCounter = 0;
foreach ($data as $key => $value) {
foreach ($value as $i => $j) {
if (!is_array($j)) {
$headers[] = $i;
$result[$outerCounter][$i] = $j;
} else {
foreach ($j as $k => $m) {
if (!is_array($m)) {
$headers[] = $i. '_'.$k;
$result[$outerCounter][$i. '_'.$k] = $m;
} else {
foreach ($m as $n => $l) {
if (!is_array($l)) {
$headers[] = $i.'_'.$k.'_'.$n;
$result[$outerCounter][$i.'_'.$k.'_'.$n] = $l;
}
}
}
}
}
}
$outerCounter++;
}
asort($headers);
$headers = array_unique($headers);
return array('headers' => $headers, 'data' => $result);
}
Related
who can hellp with this, i need create post from code, by wp_insert_post
function, so what is going on:
I creating post on each existing laguages, then i update all needed fields and i need to set post term depened of current language.
I have terms (shelters) https://prnt.sc/pklSXojB2jZj - i need that post set to current lang shelter while create, but after creating i got - https://prnt.sc/pz2jFEq1OAMP , 2 posts set in one lang terms. Maybe who know whats goes wrong, below i provided 2 functions who do these actions, Thanks in advance:
function add_pet($form_data, $ready_data, $update_meta = false) {
$allLang = pll_languages_list();
if ($allLang) {
$postsArr = array();
foreach ($allLang as $lang) {
//Add new pet
$post_id = wp_insert_post(array(
'post_title' => $form_data['pet_name'],
'post_status' => 'publish',
'post_author' => $form_data['user_id'],
'post_type' => 'pets',
));
//Check if post created, update all needed fields, and update post terms (shelter)
if ($post_id) {
//Get all translation of term
$shelter_id = pll_get_term(intval($form_data['shelter']), $lang);
$update_meta['shelter'] = $shelter_id;
//In this function we update those terms
$update_success = update_pets_fields($post_id, $ready_data, $update_meta);
$postsArr[$lang] = $post_id;
pll_set_post_language($post_id, $lang);
$postDate = get_post($post_id)->post_date;
$unixDate = strtotime($postDate);
update_post_meta($post_id, 'pet_update_date', $unixDate);
}
}
//Save post translation
if ($postsArr) {
pll_save_post_translations($postsArr);
}
//Old code
// foreach ($allLang as $lang) {
// $cat_id = pll_get_term(intval($form_data['kind_of_animal']), $lang);
// $shelter_id = pll_get_term(intval($form_data['shelter']), $lang);
// $post_id = $postsArr[$lang];
// $update_meta['post_category'] = $cat_id;
// $update_meta['shelter'] = $shelter_id;
// $update_success = update_pets_fields($post_id, $ready_data, $update_meta);
// }
}
if (is_wp_error($post_id)) {
wp_send_json_error($post_id->get_error_message());
}
if ($update_success) {
wp_send_json_success('Post was created.');
} else {
wp_send_json_error('Post was not created.');
}
}
And function who update field
/Update pets fields
function update_pets_fields($post_id, $data, $update_meta = false) {
if (!$post_id) return;
//Update post meta data not acf
if (isset($update_meta) && !empty($update_meta['title'])) {
$post_update = array(
'ID' => $post_id,
'post_title' => $update_meta['title']
);
$result = wp_update_post($post_update);
if (is_wp_error($result)) {
wp_send_json_error('Fields was not updated');
}
}
if (isset($update_meta['shelter']) && !empty($update_meta['shelter']) && intval($update_meta['shelter'])) {
wp_remove_object_terms($post_id, 'uncategorized', 'sholters');
$shelter_id = intval($update_meta['shelter']);
wp_set_post_terms($post_id, array($shelter_id), 'sholters');
if (is_wp_error($result)) {
wp_send_json_error('Term not updated');
}
}
if (isset($update_meta['post_category']) && !empty($update_meta['post_category']) && intval($update_meta['post_category'])) {
wp_remove_object_terms($post_id, 'uncategorized', 'category');
wp_set_post_categories($post_id, intval($update_meta['post_category']));
if (is_wp_error($result)) {
wp_send_json_error('Category not updated');
}
}
if (isset($update_meta['thumbnail']) && !empty($update_meta['thumbnail']) && intval($update_meta['thumbnail'])) {
set_post_thumbnail($post_id, intval($update_meta['thumbnail']));
}
if (is_null($data)) {
wp_send_json_error('No data.');
}
foreach ($data as $field) {
if ($field[3] === 'where_is') {
$field_array['field_62169d3cffb5b'] = array(
$field[2] => $field[1],
);
} else {
$field_array = array(
$field[2] => $field[1],
);
}
if (!empty($field[1])) {
$result = update_field('field_621696a7ffb4e', $field_array, $post_id);
}
}
if ($result) {
return false;
} else {
return true;
}
}
I try update it in diferent variants but it does't work, i check ids that come in args all ids is right and all posts ids is right.But on at the exit we got 2 posts in 1 term
I'm trying to figure out how to add pipe with gzip compression to the mysqldump command using Symfony Process Component. What I have, and what doesn't seem to work is:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
$this->ignoreTables(),
'--user='.$this->dbConfig['username'],
'--password='.$this->dbConfig['password'],
$this->dbConfig['database'],
];
if ($this->compress) {
$command[] = '| gzip -9';
$fileName = $fileName.'.gz';
}
$file = $this->backupPath($fileName);
$process = new Process(array_merge(
$command,
['--result-file='.$file]
));
$process->run();
The error I'm getting is
mysqldump: Couldn't find table: "| gzip -9"
The ignoreTables() method
private function ignoreTables(): ?string
{
if (empty($this->ignoreTables)) {
return null;
}
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'--ignore-table=%s.%s',
$this->dbConfig['database'],
$table
)
)->implode(' ');
}
Any idea how I can pass piped gzip flag to the command?
You can still build your command as a string and run it in a shell with Process::fromShellCommandline.
I think your command won't work as written, though, so I'm making the changes I think are necessary (you can also clean this up and not use arrays to build the command).
Main:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$file = $this->backupPath($fileName);
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
'--user="${:USER}"',
'--password="${:PASSWORD}"',
'"${:DATABASE}"',
];
$command = array_merge($command, $this->getIgnoredTablesArguments());
if ($this->compress) {
$command[] = ' | gzip -9 > "${:OUTPUT_FILE}"';
$file .= '.gz';
} else {
$command[] = '--result-file="${:OUTPUT_FILE}"';
}
$process = Process::fromShellCommandline(implode(' ', $command));
$parameters = [
'USER' => $this->dbConfig['username'],
'PASSWORD' => $this->dbConfig['password'],
'DATABASE' => $this->dbConfig['database'],
'OUTPUT_FILE' => $file,
];
$parameters = array_merge($parameters, $this->getIgnoredTablesParameters());
$process->run(null, $parameters);
Helper methods:
private function ignoreTables(): array
{
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'%s.%s',
$this->dbConfig['database'],
$table
)
);
}
private function getIgnoredTablesArguments(): array
{
$arguments = [];
foreach ($this->ignoreTables as $k => $v) {
$argName = sprintf('${:IGNORE_%s}', $k);
$arguments[] = sprintf('--ignore-table="%s"', $argName);
}
return $arguments;
}
private function getIgnoredTablesParameters(): array
{
$envs = [];
foreach ($this->ignoreTables() as $k => $v) {
$argName = sprintf('IGNORE_%s', $k);
$envs = array_merge($envs, [$argName => $v]);
}
return $envs;
}
I am trying to make a function to check if the gform has some entries, has all entries or is empty and return $status according to status.
I have looped through the entries and checked if they are infact shown as an empty string if empty but I only ever get partial or empty.
function set_form_status($form_id) {
$entries = GFAPI::get_entries( $form_id, entry_search_criteria());
$status = '';
if (count($entries) > 0) {
foreach($entries as $entry) {
$keys = array_keys($entry);
foreach($keys as $key) {
if ($entry[$key] === '') {
$status = 'partial';
}
if ($entry[$key] !== '') {
$status = 'filled';
}
}
}
} else {
$status = 'empty';
}
return $status;
}
I'd suggest running it like below and seeing what gets returned. I did remove the entry_search_criteria function but other than that I'm just echoing the key/value.
What I found was even if I'm not using purchasing in a form, there are fields created for it and they are empty. They don't show on the back end entry view even if I select show empty fields. You may have a similar scenario.
function set_form_status($form_id) {
$entries = GFAPI::get_entries( $form_id);
$status = '';
if (count($entries) > 0) {
foreach($entries as $entry) {
$keys = array_keys($entry);
foreach($keys as $key) {
echo $key . ' - ' . $entry[$key] . '<br>';
if ($entry[$key] === '') {
$status = 'partial';
}
if ($entry[$key] !== '') {
$status = 'filled';
}
}
}
} else {
$status = 'empty';
}
return $status;
}
Did some refactoring and debugging together with a co-worker, there was HTML fields on all of the forms and all of them returned an empty string. This is the working version.
function set_form_status($form) {
define('FORM_EMPTY', 0);
define('FORM_PARTIAL', 1);
define('FORM_FILLED', 2);
//checks if the form is my special kind of form
if (is_eligible_form($form)) {
$earlier_entry = get_last_entry($form['id']);
if ($earlier_entry === false) {
return STATUS_EMPTY;
} else {
foreach($form['fields'] as $key => $field) {
if ($earlier_entry[$field->id] === '' && $field->type !== 'html') {
return FORM_PARTIAL;
}
}
}
}
return FORM_FILLED;
}
A simple question:
I have one form, it returns one number and I need create this number of labels in Controller.
I try:
$form2 = $this->createFormBuilder();
for($i = 0; $i < $num; $i++) {
$name = 'column'.$i;
$form2->add($name,'number');
}
$form2->getForm();
I think it should very simple, but i can't..
Yes, you can do it with an array / hash map instead of a real object.
Here is an example :
// Create the array
$dataObj = array();
$dataObj['data1'] = '';
$dataObj['data2'] = 'default';
// ... do a loop here
$dataObj['data6'] = 'Hello';
// Create the form
$formBuilder = $this->createFormBuilder($dataObj);
foreach($dataObj as $key => $val)
{
$fieldType = 'text'; // Here, everything is a text, but you can change it based on $key, or something else
$formBuilder->add($key, $fieldType);
}
$form = $formBuilder->getForm();
// Process the form
$request = $this->get('request');
if($request->getMethod() == 'POST')
{
$form->bind($request); // For symfony 2.1.x
// $form->bind($this->get('request')->request->get('form')); // For symfony 2.0.x
if($form->isValid())
{
$dataObj = $form->getData();
foreach($dataObj as $key => $val)
{
echo $key . ' = ' . $val . '<br />';
}
exit('Done');
}
}
// Render
return $this->render('Aaa:Bbb:ccc.html.twig', array(
'requestForm' => $form->createView()));
I'm working on a WordPress plugin, and part of that plugin requires extending WP_List_Table and storing any of the items which are checked in that table to an option. I've managed to figure out how to properly setup and display the required table, but how do I handle storing the checked options?
Here's what I've got so far...
class TDBar_List_Table extends WP_List_Table {
// Reference parent constructor
function __construct() {
global $status, $page;
// Set defaults
parent::__construct( array(
'singular' => 'theme',
'plural' => 'themes',
'ajax' => false
));
}
// Set table classes
function get_table_classes() {
return array('widefat', 'wp-list-table', 'themes');
}
// Setup default column
function column_default($item, $column_name) {
switch($column_name) {
case 'Title':
case 'URI':
case'Description':
return $item[$column_name];
default:
return print_r($item, true);
}
}
// Displaying checkboxes!
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="%1$s" id="%2$s" value="checked" />',
//$this->_args['singular'],
$item['Stylesheet'] . '_status',
$item['Stylesheet'] . '_status'
);
}
// Display theme title
function column_title($item) {
return sprintf(
'<strong>%1$s</strong>',
$item['Title']
);
}
// Display theme preview
function column_preview($item) {
if (file_exists(get_theme_root() . '/' . $item['Stylesheet'] . '/screenshot.png')) {
$preview = get_theme_root_uri() . '/' . $item['Stylesheet'] . '/screenshot.png';
} else {
$preview = '';
}
return sprintf(
'<img src="%3$s" style="width: 150px;" />',
$preview,
$item['Title'],
$preview
);
}
// Display theme description
function column_description($item) {
if (isset($item['Version'])) {
$version = 'Version ' . $item['Version'];
if (isset($item['Author']) || isset($item['URI']))
$version .= ' | ';
} else {
$version = '';
}
if (isset($item['Author'])) {
$author = 'By ' . $item['Author'];
if (isset($item['URI']))
$author .= ' | ';
} else {
$author = '';
}
if (isset($item['URI'])) {
$uri = $item['URI'];
} else {
$uri = '';
}
return sprintf(
'<div class="theme-description"><p>%1$s</p></div><div class="second theme-version-author-uri">%2$s%3$s%4$s',
$item['Description'],
$version,
$author,
$uri
);
}
// Setup columns
function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Theme',
'preview' => 'Preview',
'description' => 'Description'
);
return $columns;
}
// Make title column sortable
function get_sortable_columns() {
$sortable_columns = array(
'title' => array('Title', true)
);
return $sortable_columns;
}
// Setup bulk actions
function get_bulk_actions() {
$actions = array(
'update' => 'Update'
);
return $actions;
}
// Handle bulk actions
function process_bulk_action() {
// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}
if ('update' === $this->current_action()) {
foreach ($themes as $theme) {
if ($theme['Stylesheet'] . '_status' == 'checked') {
// Do stuff - here's the problem
}
}
}
}
// Handle data preparation
function prepare_items() {
// How many records per page?
$per_page = 10;
// Define column headers
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
// Build the array
$this->_column_headers = array($columns, $hidden, $sortable);
// Pass off bulk action
$this->process_bulk_action();
// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}
// Handle sorting
function usort_reorder($a,$b) {
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'Title';
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
$result = strcmp($a[$orderby], $b[$orderby]);
return ($order === 'asc') ? $result : -$result;
}
usort($themes, 'usort_reorder');
//MAIN STUFF HERE
//for ($i = 0; i < count($themes); $i++) {
//}
// Figure out the current page and how many items there are
$current_page = $this->get_pagenum();
$total_items = count($themes);
// Only show the current page
$themes = array_slice($themes,(($current_page-1)*$per_page),$per_page);
// Display sorted data
$this->items = $themes;
// Register pagination options
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items/$per_page)
));
}
}
Problem is, I can't get it to save properly. I select the rows I want, hit save and it just resets.
I assume you are talking about the checkboxes in your table listing, so this will be how to process bulk actions.
All you need to do is add two new methods to your class and initialize it in the prepare_items method. I use the code below in one of my plugins to delete or export, but you can just as easily run an update.
/**
* Define our bulk actions
*
* #since 1.2
* #returns array() $actions Bulk actions
*/
function get_bulk_actions() {
$actions = array(
'delete' => __( 'Delete' , 'visual-form-builder'),
'export-all' => __( 'Export All' , 'visual-form-builder'),
'export-selected' => __( 'Export Selected' , 'visual-form-builder')
);
return $actions;
}
/**
* Process our bulk actions
*
* #since 1.2
*/
function process_bulk_action() {
$entry_id = ( is_array( $_REQUEST['entry'] ) ) ? $_REQUEST['entry'] : array( $_REQUEST['entry'] );
if ( 'delete' === $this->current_action() ) {
global $wpdb;
foreach ( $entry_id as $id ) {
$id = absint( $id );
$wpdb->query( "DELETE FROM $this->entries_table_name WHERE entries_id = $id" );
}
}
}
Now, call this method inside prepare_items() like so:
function prepare_items() {
//Do other stuff in here
/* Handle our bulk actions */
$this->process_bulk_action();
}
There's a fantastic helper plugin called Custom List Table Example that makes figuring out the WP_List_Table class much easier.