Drupal Upgrade:: Need to rewrite pagerquery to version 7 - drupal

I need to rewrite pagerquery, i have tried out several options adding tags, extend(PagerDefault) but nothing worked for me:
Please help.
My version 6 code is:
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
dsm($result);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;
}

Without knowing the name of your join table this isn't complete, but should get you started:
$query = db_select('node', 'n')
->fields('n', array('nid', 'sticky', 'title', 'created'))
->condition('n.uid', $user->uid)
->condition('n.type', 'case')
->condition('n.status', 1)
->extend('PagerDefault')
->limit(variable_get('default_nodes_main', 10))
->addTag('node_access')
->orderBy('col_name');
$query->join('table_to_join', 'table_alias', 'table_alias.nid = n.nid');
foreach ($query->execute() as $row) {
// Do what you need to
}

if ($vidw == 'my_cases' or $vidw == 'my') { // including private
switch ($sort_by) {
case 'rated':
$order = 'n.sticky DESC, vc.value DESC';
$sort_join = 'LEFT OUTER JOIN {votingapi_cache} vc ON n.nid = vc.content_id AND vc.content_type = "node" AND vc.function = "average"';
break;
case 'discussed':
$order = 'n.sticky DESC, nc.comment_count DESC';
$sort_join = 'LEFT OUTER JOIN {node_comment_statistics} nc ON n.nid = nc.nid';
break;
case 'viewed':
$order = 'n.sticky DESC, nc.totalcount DESC';
$sort_join = 'LEFT OUTER JOIN {node_counter} nc ON n.nid = nc.nid';
break;
case 'recent':
default:
$order = 'n.sticky DESC, n.created DESC';
$sort_join = '';
break;
}
// from taxonomy_select_nodes
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '.$sort_join.' WHERE n.uid = %d AND n.type = "case" AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n WHERE n.uid = %d AND n.type = "case" AND n.status = 1';
$args = array('uid' => $user->uid);
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
// $output .= taxonomy_render_nodes($result);
$num_rows = FALSE;
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
$num_rows = TRUE;dsm($output);
}
if ($num_rows) {
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
}
else {
$output .= '<p>'. t('There are currently no visible cases in this category.') .'</p>';
}
}
In above drupal 6 code, i have updated the query which gives me result but node_view() and theme() does not work.

Related

display the picture of the products category

I want to display the picture of the products category on the map.
I want to show the category image
I tried many of the following WordPress functions and when I got vardump it gave me an array
this is the code of my map
In this section I want to show the image instead of the icon
<?php
$args = array('post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1);
$wp_query = new WP_Query($args);
?>
<?php if ($wp_query->have_posts()) :
$posts = $wp_query->posts;
$GLOBALS['all_ads']=$posts;
$lenght = count($posts); ?>
<ul class="row items ad-listing">
<?php for ($i = 0; $i < $lenght; $i++) {
$single_post = $posts[$i];
$terms = wp_get_post_terms($single_post->ID, 'city_categories', array('order' => 'DESC'));
$parent_id = $terms[0]->parent;
$parent_id_1 = $terms[1]->parent;
$city_term = get_term_by('id', $parent_id, 'city_categories');
$name = $city_term->name;
$term_id = $terms[0]->term_id;
$term_id_two = $terms[1]->term_id;
$ad_type = get_post_meta($single_post->ID, 'ad_type','true');
$parent_id = $parent_id ? $parent_id :$parent_id_1;
if ($city_term_id == $term_id || $city_term_id == $term_id_two){
$data['lat'] = get_post_meta($single_post->ID, '_ad_latitude', true);
$data['long'] = get_post_meta($single_post->ID, '_ad_longitude', true);
$phone = get_post_meta($single_post->ID , 'shmarhtmascabt_v_shmarhtmascabt_m' , true);
$address = trim(get_post_meta($single_post->ID , 'adrs_v_shmarhtmascabt_m' , true));
$type = wp_get_post_terms($single_post->ID, 'product_cat')[0]->name;
$title = $single_post->post_title;
$ad_link = get_permalink($single_post->ID);
$sc_studi_cat_icon = get_term_meta($term_id->term_id, array( 1680,470 ), true);
print_r($sc_studi_cat_icon);
$img_cat=wp_get_attachment_image($sc_studi_cat_icon,array('71', '76'));
$src = wp_get_attachment_image_src( get_post_thumbnail_id($single_post->ID), '', false, '' );
$sc_studi_cat_icon = get_term_meta($type->term_id, 'sc_studi_cat_icon', true);
$img_cat=wp_get_attachment_image($sc_studi_cat_icon,array('71', '76') , false , 'src');
var_dump($img_cat);
$data['text']="<div class='map-div-container'><div><a href='{$ad_link}'><h4>{$title}</h4></a></div>";
if ($phone){$data['text'].="<div><i class='fas fa-phone' style='font-size: 10px;'></i><b>تلÙÙ†:</b> {$phone}</div>";}
if ($address){$data['text'].="<div><i class='fas fa-map-marker-alt'></i><b>آدرس:</b> {$address}</div>";}
if ($type){$data['text'].="<div><i class='far fa-folder'></i><b>نوع:</b> {$type}</div></div>";}
$data['text']=urldecode($data['text']);
$map_data[$single_post->ID] = $data;
}
if (($state_term_id === $parent_id || $state_term_id[0] === $parent_id) && ($ad_type == 1936)):
It will be a great help if you could guide me how to do this.
Thanks in advance.
$type3 = wp_get_post_terms($single_post->ID, 'product_cat')[0];
$category_thumbnail = get_woocommerce_term_meta($type3->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);

Symfony 4 Match Against sort by relevanece

I want to sort by revelance that mean if i have in db:
FooBa
FooBar
FooB
Foo
and i want to type foo it will be sorted:
Foo
FooB
FooBa
FooBar
public function findBySearchQueryPagin2(string $rawQuery, int $limit = 999, string $sort = "DESC", string $sortBy = "relevance", int $page = 1): pagerfanta
{
$query = $this->sanitizeSearchQuery($rawQuery);
$sort = $this->sanitizeSearchQuery($sort);
$sortBy = $this->sanitizeSearchQuery($sortBy);
$limit = $this->sanitizeSearchQuery($limit);
$page = $this->sanitizeSearchQuery($page);
if(($sort != 'ASC')&&($sort != 'DESC')) $sort = 'DESC';
if(($sortBy != 'date')&&($sortBy != 'name')&&($sortBy != 'relevance')){
$sortBy = null;
} else {
if($sortBy=="name") $sortBy = "primarytitle";
if($sortBy=="date") $sortBy = "startyear";
if($sortBy=="relevance") $sortBy = "relevance";
}
$queryBuilder = $this->createQueryBuilder('p')
->andWhere("MATCH_AGAINST (p.primarytitle, :searchTerms) > 1")
->setParameter('searchTerms', $query)
->setMaxResults($limit);
var_dump($query);
if($sortBy !== "relevance"){
$queryBuilder
->orderBy('p.' . $sortBy, $sort);
}
return $this->createPaginator($queryBuilder->getQuery(), $page);
}

How to Get all terms by post type

I need to get all items in a custom taxonomy in custom post type
here is my try:
function gat_all_terms($taxonomy){
$terms = get_terms( $taxonomy, 'orderby=count&hide_empty=0' );
$count = count($terms);
$out ='';
if ( $count > 0 ){
foreach ( $terms as $term ) {
$out .= "<li class='item'> <a href='#'>" . $term->name . "</a></li>";
}
}
return $out;
}
But i cant detect for a custom post type.
If you're trying to get the terms of a custom post type you can check out the answer here:
https://wordpress.stackexchange.com/questions/14331/get-terms-by-taxonomy-and-post-type
static public function get_terms_by_post_type( $taxonomies, $post_types ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
join( "', '", $post_types ),
join( "', '", $taxonomies )
);
$results = $wpdb->get_results( $query );
return $results;
}
Which you could convert to something simpler like:
function get_terms_by_post_type( $taxonomy, $post_type ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT t.*, COUNT(*) from $wpdb->terms AS t
INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
GROUP BY t.term_id",
$post_type,
$taxonomy
);
$results = $wpdb->get_results( $query );
return $results;
}
get_terms_by_post_type( 'taxonomy', 'type' );

Doctrine doesn't find the rows just flushed

I have 3 entities: Subjects, Sections, Questions. Everyone has relations with the rest. I am trying to get the questions of the current $user. If there are none, I make a copy of the default ones with user = NULL.
But Doctrine gives me headaches. Initially it wouldn't get questions with null user. Now after flushing the new questions in the database it won't get them (they are inserted correctly).
Probably there is something that I don't see.
Why won't it retrieve the new questions?
<?php
public function getProfileQuestionsForUser( $user ) {
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, sc, q' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 'sc.questions', 'q', 'WITH', 'q.user = :USER' )
->where( 's.type = :TYPE' )
->setParameter( 'USER', $user )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
/** #var SubjectEntity $result */
$result = $query->getOneOrNullResult();
$hasQuestions = false;
foreach ( $result->getSections() as $section ) {
$qs = count($section->getQuestions());
if ( count( $section->getQuestions() ) ) {
$hasQuestions = true;
break;
}
}
if ( $hasQuestions == false ) {
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, q, sc' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 's.questions', 'q', 'WITH', 'q.user IS NULL' )
->where( 's.type = :TYPE' )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
/** #var SubjectEntity $subject */
$subject = $query->getOneOrNullResult();
if ( ! empty( $subject ) ) {
/** #var QuestionEntity $question */
$qs = count( $subject->getQuestions() );
foreach ( $subject->getQuestions() as $question ) {
$new_question = clone $question;
$new_question->setUser( $user );
$new_question->setAnswers( new ArrayCollection() );
$new_question->setQuestionStatuses( new ArrayCollection() );
$new_question->setOptions( new ArrayCollection() );
$this->getEntityManager()->persist( $new_question );
}
$this->getEntityManager()->flush();
// Retrieve new questions from DB
$queryBuilder = $this->createQueryBuilder( 's' );
$query = $queryBuilder
->select( 's, sc, q' )
->leftJoin( 's.sections', 'sc' )
->leftJoin( 'sc.questions', 'q', 'WITH', 'q.user = :USER' )
->where( 's.type = :TYPE' )
->setParameter( 'USER', $user )
->setParameter( 'TYPE', 'profile' )
->getQuery();
$sql = $query->getSQL();
$result = $query->getOneOrNullResult();
}
}
return $result;
}
Maybe something like this will work:
//In your Question entity add this
public function __clone() {
$this->id = null;
}
And modify your code like this:
foreach ( $subject->getQuestions() as $question ) {
$new_question = clone $question;
$this->getEntityManager()->detach($new_question);
$new_question->setUser( $user );
$new_question->setAnswers( new ArrayCollection() );
$new_question->setQuestionStatuses( new ArrayCollection() );
$new_question->setOptions( new ArrayCollection() );
$this->getEntityManager()->persist( $new_question );
}
$this->getEntityManager()->flush();
UPDATE
Hum, Here there is a more complete answer

simple html dom - cannot redeclair str_get_html()

Im trying to make a class to help with SEO and also compare google API with actual results
class:
<?php
class true_seo {
public $string, $amount;
private $arr;
public function __construct(){}
public function set_g_key( $key ) {
$this->g_key = $key;
}
public function set_phrase( $string ){
if( is_string ( $string ) ) {
$string = array( $string );
}
if( is_array ( $string ) ) {
$this->phrases = $string;
}else{
Throw new exception("incorect input for phrase, string or array");
}
}
public function get_sites_use_spider( $amount ) {
require "simple_html_dom.php";
$main_result = array();
foreach( $this->phrases as $phrase ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => 0, "maxResults" => $amount, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
$data = true_seo::google_search_api( $APIparams, 'http://www.google.co.uk/search', false );
new simple_html_dom();
$html = str_get_html( $data );
$result = array();
foreach( $html->find('li.g h3 a') as $g ) {
$data = $g->parent()->nextSibling();
$other = $data->find('span a');
$x = 0;
foreach( $other as $d ) {
( $x == 0 ? $cache = $d->href : $simular = $d->href );
$x++;
}
$excess_span = $data->find('span',0)->outertext;
if( isset( $data->find('div',0)->tag ) ) {
$excess_div = $data->find('div',0)->outertext;
$title = str_replace( array( $excess_span, $excess_div, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','','' ), $data->outertext );
}else{
$title = str_replace( array( $excess_span, '<em>', '</em>', '<br>', '<b>', '</b>' ), array( '','','','','','' ), $data->outertext );
}
$result[] = array( 'link' => $g->href, 'title' => strip_tags( $title ), 'cache' => $cache, 'simular' => 'http://www.google.co.uk' . $simular );
}
$main_result[$phrase] = $result;
$html->clear();
}
$this->non_api_data = $main_result;
}
public function get_sites_use_api( $amount ) {
$arr = array();
foreach( $this->phrases as $phrase ) {
if( $amount > 4 ) {
$times = $amount / 4;
}else{
$times = 1;
}
$arg = array();
for($x = 0; $x < $times; $x++ ) {
$APIparams = array("key" => $this->g_key, "q" => $phrase, "start" => ($x * 4), "maxResults" => 4, "filter" => true, "restrict" => "", "safeSearch" => false, "lr" => "lang_en", "ie" => "", "oe" => "");
if( $data = true_seo::google_search_api( $APIparams, 'http://ajax.googleapis.com/ajax/services/search/web' ) ) {
$arg = array_merge($arg, $data->responseData->results);
}else{
Throw new exception("Request error: no results returned from Google.");
}
}
$arg = array_reverse( $arg );
$remove = $amount % 4;
if( $amount < 4 ) {
$remove = 4 - $amount;
}
for( $x=0; $x < $remove; $x++ ) {
unset( $arg[$x] );
}
$arg = array_reverse( $arg );
foreach( $arg as $g ) {
$result = array( 'link' => $g->url, 'title' => strip_tags( $g->content ), 'cache' => $g->cacheUrl, 'simular' => 'na' );
$arr[$phrase][] = $result;
}
}
$this->api_data = $arr;
}
public function google_search_api($args, $url, $api = true){
if ( !array_key_exists('v', $args) ) {
$args['v'] = '1.0';
}
$url .= '?'.http_build_query($args, '', '&');
if( $result = #file_get_contents($url) ) {
if( $api == true ) {
return json_decode($result);
}else{
return $result;
}
}else{
Throw new exception("No data returned from url: $url");
}
}
public function set_get_actual( $string ) {
$this->actual->name = $string;
$this->actual->data = file_get_contents( $string );
}
public function get_actual_description(){
require_once "simple_html_dom.php";
new simple_html_dom();
$html = str_get_html( $this->actual->data );
return $html->find('head meta[name=description]',0)->content;
$html->clear();
}
}
?>
called by :
<?php
try{
require "./classes/class_true_seo.php";
$seo = new true_seo();
$seo->set_g_key('ABQIAAAAsWzmZ4RXdIk0a-LqpqKCBRSl_WmKnmsXGmN0kkjN2wkrfEOY-hT2sL-_x5v4NtT3DgElKNsR7FDJDQ');
$seo->set_phrase(array("web design mansfield"));
$seo->get_sites_use_api(10);
ob_start();
foreach( $seo->api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
$seo->set_get_actual( $results['link'] );
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
#echo "<p>" . $seo->get_actual_title() . "</p>";
echo "<p>" . $seo->get_actual_description() . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('p') . "</p>";
#echo "<p>" . $seo->get_actual_amount_of('h2') . "</p>";
}
}
$api_return = ob_get_clean();
ob_start();
$seo->get_sites_use_spider(10);
foreach( $seo->non_api_data as $key => $phrase_return ){
echo "<h2>" . $key . "</h2>";
foreach( $phrase_return as $rank => $results ){
echo "<p class=\"link-head\"><strong>#" . ( $rank + 1 ) . "</strong> " . $results['link'] . "</p>";
echo "<p>" . $results['title'] . "</p>";
}
}
$non_api_return = ob_get_clean();
}catch(Exception $err){
$error = $err->getMessage();
}
?>
My problem being that I keep getting the error:
Fatal error: Cannot redeclare file_get_html() (previously declared in C:\wamp\www\seo\classes\simple_html_dom.php:37) in C:\wamp\www\seo\classes\simple_html_dom.php on line 41
which is due to the last function in the class get_actual_description().
Can anyone see where im cocking up?
regards,
Phil
put require_once "simple_html_dom.php" outside of function

Resources