How to know if a link is active in a view script without using Zend\Mvc\Navigation ? ZF2 - zend-framework2

I'm using Zend Framework 2.
I'd like test if a link is the active page in my view script.
For example :
on my view script : view/application/account/user.phtml
<a href="$this->url("account")"
<?echo **if($this->isActive()**){?> class="active"<?php } ?>link</a>
I don't want to set menu with Zend\Mvc\Navigation
Thank you all.

You could do like this:
<?php $current_url = $this->url(); ?>
<a href="<?php echo $this->url('account'); ?>" <?php if ($current_url == $this->url('account')) { echo 'class="active"'; } ?>>link</a>

Related

Appium: App Path only works with a direct link?

I'm using Laravel php and Appium.
I want Appium to download the .apk/.ipa file from a certain route which returns the downloaded file.
App path in Appium: localhost/downloadApp
public function downloadApp(Request $request) {
...
return response()->download($path);
}
If I try this way, it doesn't work and I get an error "[Support] Error: Plist file doesn't exist: '.../Info.plist". I don't know why because if I call localhost/downloadApp in my browser, it downloads the file.
But if I use the direct link (http://localhost/uploads/HelloWorld.ipa) in Appium, it works.
The path must point to an actual file.
You can download the file itself from your code but you need to make sure that:
The file has completed to download before you set up the driver
The path to the downloaded file is correct, make sure the name of the file and the download location is the same as what you set in the capabilities ("localhost/downloadApp" doesn't seem like the correct download path)
I have the same problem, I just use return redirect ($file_path); and that works fine for apk file. For security, I also use the POST method to start download. This is my code:
//blade code :
<li class="nav-item">
<form method="POST" action="{{url('/files/download')}}">
{{ csrf_field()}}
<button class="btn btn-primary fa fa-download"> download </button>
</form>
</li>
//Route code :
Route::post('/files/download', 'FilesController#download');
//Controller code :
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\File;
use App\Http\Controllers\Controller;
class FilesController extends Controller
{
public function download($file)
{
$file_path = 'your folder path/file.apk';
// if your file in public use $path = "/your folder/file.apk"
// if your file is in out of public you may use two points like that: $path = "../your folder/file.apk"
return redirect ($file_path);
}
}

How to get News previous and next news in contao ?

How to i get News previous and next news in contao ?
f.e
< Previous News | Current news ( id - 4) | Next News >
I want Previous and next news link in detail page..
You can use the following extension: [newspagination]
In case you are not using the default mod_newsreader template, you need to add the following line:
<?php echo $this->addNewsPagination() ?>
or just
<?= $this->addNewsPagination() ?>
with PHP 5.4+

Pull in previous and next post featured images into single.php

My previous and next posts are set on an infinite loop so there is always a previous and next post. I.E. for the newest article the "next" post would be go to the oldest post. And if we're viewing the oldest post, the "previous" post would go to the newest post.
I need to pull in the featured images of the next and previous posts to be the background for the pagination navigation.
I've got the images pulling in when there truly is a next or previous post. However, when the infinite loop is necessary, i.e. next being the oldest post, or previous being the newest post, my code breaks down.
My code is below. I believe the issue is that I'm not actually capturing the ID of what I need in $first_id and $last_id. I've tried using get_queried_object_id() $first->post->ID and various combinations to get the post ID of what is being pulled in since .previous-title is working correctly.
<div class="post_nav">
<?php if( get_adjacent_post(false, '', true) ) :
$previous_post = get_previous_post(); ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $previous_post->ID );?></div>
<p class="previous-title"><?php previous_post_link('Previous Story<br> %link','%title');?</p>
<?php else:
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
$first_id = $first->ID; ?>
<div class="previous-img"><?php echo get_the_post_thumbnail( $first_id );?></div>
<?php echo '<p class="previous-title">Previous Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
<?php if( get_adjacent_post(false, '', false) ) :
$next_post = get_next_post(); ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $next_post->ID ); ?></div>
<p class="next-title"><?php next_post_link('Next Story<br> %link','%title');?></p>
<?php else:
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
$last_id = $last->ID; ?>
<div class="next-img"><?php echo get_the_post_thumbnail( $last_id );?></div>
<?php echo '<p class="next-title">Next Story<br>' . the_title() . '</p>';
wp_reset_query(); ?>
<?php endif; ?>
You're right, $first and $last are not posts, but queries. You could either use get_the_ID() or grab the ID directly.
$last_id = get_the_ID();
( since you've made it the active loop )
or
$last_id = $last->posts[0]->ID;
which means you wouldn't need to set up the loop for $last, in which case you'd also need to get the permalink and title via functions taking the ID as a parameter. This is a minor optimization, if you get confused here go with the first option.
Also, your next-title and previous-title in the else paths seem to be switched with each other. Or perhaps the entire code blocks are.

Escape Html Text in ZF2

I have a text like this:
<p><strong>Lorem</strong> ipsur </p>
To see a text without html tags in Zend Framework 2 how can I do?
I tried to put:
<?php echo $this->escapehtml($array['text']); ?>
I read that for security is not good to do:
<?php echo $array['descrizione']; ?>
Im not sure if i understand the question, since i dont know the contents of your arrays.
basicaly if you want to scape the contents of a variable, let's say, $input, you have to call, as you mentioned
$this>escapeHtml($input)
Actually, the PhpRenderer includes a selection of helpers you can use for this purpose: EscapeHtml, EscapeHtmlAttr, EscapeJs, EscapeCss, and EscapeUrl.
You can read about this here
Also, if you want more control, you can use Zend\Escaper, that in 2 lines of code allow you to escape html, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtml($input);
or escape attributes, like this
$escaper = new Zend\Escaper\Escaper('utf-8');
$output = $escaper->escapeHtmlAttr($input);
I recommend you read the 3 links, they are very short and would give you a better undestanding of what you are doing.
At this moment there is no option in escapeHtml to allow certain tags. What you have to do in these specific cases is to modify the returned value:
<?php
$str = $this->escapehtml($array['text']); ?>
$str = preg_replace(
array('#href="(.*)"#', '#<(/?(?:pre|a|b|br|em|u|ul|li|ol|p|strong)(\shref=".*")?/?)>#' ),
array( 'href="\1"', '<\1>' ),
$str
);
echo $str;
You can add/remove tags from the part pre|a|b|br|em|u|ul|li|ol|p|strong to your needs. Also this code will allow anchor tags with href only.

Wordpress displaying custom post types and their fields

I have set up a Custom Post Type called 'RELEASES' - think music cd release.
This post type has fields named 'release_artist', 'release_title', 'release_date', 'release_artwork' and 'release_tracklisting' for entering all of the relevant music cd information.
I am having real trouble actually displaying this information in my Wordpress template.
I have really only had luck outputting a list of the titles and none of the other data.
Any idea what I put in the LOOP to display all of the information? Preferably each in its own LIST item so I can style each separately?
Any thoughts greatly appreciated.
You can use get_post_meta to pull in your fields as needed. Inside your loop, you can start with the following:
<?php
$release_artist = get_post_meta($post->ID, 'release_artist', true);
$release_title = get_post_meta($post->ID, 'release_title', true);
?>
<ul>
<li class="release_artist">
<?php echo $release_artist; ?>
</li>
<li class="release_title">
<?php echo $release_title; ?>
</li>
</ul>
Are those custom fields? If yes, try what codex.wordpress.org is saying. Better yet, try ACF plugin.
-- edit
If you want to display parts of your pages on other ones (eg. on your home), you need to use query_posts. This is fairly simple function. For your loop, try something like this:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'releases'
));
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'Opis nazwy'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
$key is a single value, here set to release_artists. It's purely for testing. If it works - feel free to define your own variables.
You should use:
<?php the_field('field_name') ?>
inside your loop. Hope it helps!
From most of the documentation I've seen online, query_posts shouldn't be the go-to function for creating custom queries and loops. The following code snippet might be a good starting point. You should be able to use this inside or outside of the main loop of your themes template files.
$args = array(
'post_type' => 'release', //remember this is-case sensitive
'posts_per_page' => -1,
);
$releaseQuery = new WP_Query( $args );
if ( $releaseQuery->have_posts() ) :
while ( $releaseQuery->have_posts() ) :
$releaseQuery->the_post();
// Fetching the post ID for demonstration and for use later
$c_id = get_the_ID();
// After running the_post(), alot of the Wordpress functions (not all) can now be used without supplying the post ID.
echo get_the_title();
// You could also have used get_the_title($c_id);
// Then:
echo get_post_meta($c_id, 'release_title', true);
echo get_post_meta($c_id, 'release_artist', true);
endwhile;
endif;
// Return to the current page's main query
wp_reset_query();
// This should now display the page's title
the_title();
About your ID's question:
Every item, like posts and pages in WordPress have an "ID", but they are not normally shown in the lists of them. There are a number of plugins that will add an "ID" column in your admin. Search Wordpress.org for "show ids" and pick one you like. Install it. Activate it. You'll see the ids.
https://wordpress.org/plugins/search.php?q=show+id

Resources