i have a problem with one 3rd party component for Joomla 3. Unfortunately i'm not an advanced php developer and the component owner does not support this for now, so i'm completely on my own =)
In advance - i have read all related topics there and was not able to make it ont he right way.
My problem is to convert this line:
return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate);
with preg_replace_callback(), since in php 5.5 /e parameter is deprecated.
Thanks a lot in advance.
Edit:
There is the whole code part:
public function loadRowtemplate ($item)
{
$table = $this->params->get('table');
if(!$this->rowtemplate) {
$rowtemplate = $table['row'][0] ? "<td><p>" . nl2br($table['row'][0]) . "</p></td>" : "";
$rowtemplate .= $table['row'][1] ? "<td><p>" . nl2br($table['row'][1]) . "</p></td>" : "";
$rowtemplate .= $table['row'][2] ? "<td><p>" . nl2br($table['row'][2]) . "</p></td>" : "";
$rowtemplate .= $table['row'][3] ? "<td><p>" . nl2br($table['row'][3]) . "</p></td>" : "";
$rowtemplate .= $table['row'][4] ? "<td><p>" . nl2br($table['row'][4]) . "</p></td>" : "";
$this->rowtemplate = str_replace(",", "<br/>", $rowtemplate);
}
**return preg_replace('/\{([a-zA-Z_]+)\}/e', '$item->\\1', $this->rowtemplate);**
}
Edit 2:
There is correct working solution for Joomla 3 and Profiler by Harold Prins Extension (com_profiler) with PHP 5.5:
return preg_replace_callback(
'/\{([a-zA-Z_]+)\}/',
function ($match) use ($item) {
if (isset($item->{$match[1]})) {
return $item->{$match[1]};
}
return "";
},
$this->rowtemplate
);
Thanks a lot to Matteo Tassinari for solution.
What you want should look like:
return preg_replace_callback(
'/\{([a-zA-Z_]+)\}/',
function ($match) use ($item) {
if (isset($item->{$match[1]})) {
return $item->{$match[1]};
}
return "";
},
$this->rowtemplate
);
see also the docs for the function itself: http://php.net/manual/en/function.preg-replace-callback.php
Related
SELECT name
FROM tx_snippethighlightsyntax_domain_model_snippets
WHERE (MATCH(name, description, code, comment) AGAINST ('css'));
This query works in phpMyAdmin with MariaDB. Now my "problem" is to adapt this in TYPO3 with QueryBuilder. I don't see any MATCH or AGAINST operator.
So far, my function start with this:
private $tx = 'tx_snippethighlightsyntax_domain_model_snippets';
public function ftsSearch()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$ftsQueryBuilder = $connectionPool->getQueryBuilderForTable($this->tx);
$fts = $ftsQueryBuilder
->select($this->tx . '.name')
->from($this->tx)
->where($ftsQueryBuilder->expr()->eq(
MAGIC HAPPENS HERE ?
)
->execute()
->fetchAll();
return $fts;
}
The extension Indexed Search in the TYPO3 core uses MATCH and AGAINST in queries.
The following code taken from IndexSearchRepository should help you building up your query
$searchBoolean = '';
if ($searchData['searchBoolean']) {
$searchBoolean = ' IN BOOLEAN MODE';
}
$queryBuilder->andWhere(
'MATCH (' . $queryBuilder->quoteIdentifier($searchData['fulltextIndex']) . ')'
. ' AGAINST (' . $queryBuilder->createNamedParameter($searchData['searchString'])
. $searchBoolean
. ')'
);
private $tx = 'tx_snippethighlightsyntax_domain_model_snippets';
public function ftsSearch()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$ftsQueryBuilder = $connectionPool->getQueryBuilderForTable($this->tx);
$fts = $ftsQueryBuilder
->select($this->tx . '.name')
->from($this->tx)
->where('MATCH('
. $this->tx .'.name,'
. $this->tx .'.description,'
. $this->tx .'.code,'
. $this->tx .'.comment)'
. ' AGAINST(' . $ftsQueryBuilder->createNamedParameter('put_search_here')
. ')')
->execute()
->fetchAll();
return $fts;
}
This one works for me. Thank you !
For example have a look at the below code, It return links based on status it get from database.
function favourite_store_link ($store_id, $user_id)
{
(string) $display_output = null;
if ($user_id)
{
$is_favourite = $this->count_rows('favourite_stores', "WHERE store_id='" . $store_id . "' AND user_id='" . $user_id . "'");
$fav_store = ($is_favourite) ? 'remove' : 'add';
$fav_store_msg = ($is_favourite) ? MSG_ADD_TO_FAVOURITE_STORES : MSG_REMOVE_FROM_FAVOURITE_STORES;
$display_output = ' [ ' . $fav_store_msg . ' ]';
}
return $display_output;
}
I want to ask if it is OK to do so or I must return the state of affairs and let template handle the rest.
It is much better to split logic and view to different parts of code.
If you just return data from your function you will have flexibility to use this function in other places, different views etc.
Dividing responsibilities is the one of main principles of good maintainable code. You can read more about it here
I'm looking to be able to pluralize "Slide" in the below function:
// Changes the default download button text
function ps_download_button($args) {
$download_text = 'Download ' . '(' . get_field('no_slides') . ' Slide)';
$args['text'] = $download_text;
return $args;
}
add_filter( 'edd_purchase_link_args', 'ps_download_button' );
This is my first stab at writing custom PHP functions. I've managed to find related code but I'm not sure how to integrate it with the above:
function plural( $amount, $singular = '', $plural = 's' ) {
if ( $amount === 1 ) {
return $singular;
}
return $plural;
}
Well you can use ternary for that.
function ps_download_button($args) {
$amount = intval(get_field('no_slides'));
$download_text = 'Download ' . '(' . $amount . ') Slide'. (($amount>1)?'s':'');
$args['text'] = $download_text;
return $args;
}
That's the simplest way, and no need for a function. If you don't understand how ternary works, take a look at this question.
I'm creating a search-function for my PHP-based file manager. I'm getting this error: 'Catchable fatal error: Object of class Closure could not be converted to string' on the following line:
if ($data->input_ext)
{
$data_ext = ($begun ? ($data->input_logic ? ' OR ' : ' AND ') :
function ()
{
$begun = true;
return "";
}) . 'ext = "' . $data->input_ext . '"';
$data_string.= $data_ext;
}
That's part of what builds the SQL query. $begun_files simply determines whether or not to put 'OR' or 'AND' at the beginning based on whether or not the user input a name or anything that comes before this to match. I have a feeling that I'm not allowed to include anonymous functions in ternary expressions but what should I do instead?
Thanks!
You can't use anonymous functions for inline flow control; just use a regular if statement and don't shun writing things on multiple lines:
if ($data->input_size) {
if ($begun_files) {
$str .= $data->input_logic ? ' OR ' : ' AND ';
$begun_files = true;
}
$str .= sprintf('size %s "%f"',
$data->input_size_op ? '<=' : '>=',
$data->input_size * pow(1024,$data->input_size_unit)
);
}
Building off of the previous answer I ended up going with this:
if ($data->input_ext) {
if ($begun) { $logic = $data->input_logic ? ' OR ' : ' AND '; } else { $logic = ""; $begun = true; }
$data_ext = $logic.'ext = "'.$data->input_ext.'"'; $data_string .= $data_ext;
}
if ($data->input_size) {
if ($begun) { $logic = $data->input_logic ? ' OR ' : ' AND '; } else { $logic = ""; $begun = true; }
$data_size = $logic.'size '.($data->input_size_op ? '<=' : '>=').' '.($data->input_size * pow(1024,$data->input_size_unit)); $data_string .= $data_size;
}
Thanks!
I am trying to work this function in wordpress theme the way where I can get value only what I want and not all to gather. I am not much familiar with using array in function so I need you expert help to make it works and I would really appreciate that.
Here is my code
function gallery_artist($arg=null, $arg2=null, $arg3=null){
global $png_gallery_meta;
$png_gallery_meta->the_meta();
$gallery = get_post_meta(get_the_ID(), $png_gallery_meta->get_the_id(), TRUE);
$gallery_value = $gallery['image_artist'];
$artist_info = $gallery_value;
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
$author_full_name = $author_first_name . ' ' . $author_last_name;
$user_id = '<a href=" ' . get_author_posts_url(basename($string)) . ' " title="more submissions of ' . $author_first_name .' '. $author_last_name . ' " >' . $artist . '</a>';
$arg = $author_first_name;
echo $arg;
$arg2 = $author_last_name;
echo $arg2;
$arg3 = $user_id;
echo $arg3;
}
After than I want to use this function to get any value I want and not unnecessary to render all three to gather. Means if I want only first name than I pass value only for that and it will render only first name so on..
Here how I want to use something. but you can suggest any code and type of function I have no issue.
<?php call_user_func_array('gallery_artist', array('first_name','last_name','artist_id') ) ?>
Big Big thanks to you all..
Try replacing the bottom section with this:
if(!is_null($arg))
{
$arg = $author_first_name;
echo $arg;
}
if(!is_null($arg2))
{
$arg2 = $author_last_name;
echo $arg2;
}
if(!is_null($arg3))
{
$arg3 = $user_id;
echo $arg3;
}