Zend Custom ViewScript for Form not rendering file element - zend-framework

I created a custom viewscript for a zend form. In the form, there is a file element. When I run the code echo $this->element->elementname->renderViewHelper() it works fine on all elements except the file element. When I run the following code on my file element within my custom viewscript, i get the following error: Uncaught exception 'Zend_Form_Element_Exception' with message 'Decorator by name ViewHelper does not exist'
<?php if($this->element->avatarimage->hasErrors()):?>
<div class="control-group error">
<?php else: ?>
<div class="control-group">
<?php endif;?>
<label for="avatarimage" class="control-label">Avatar Image</label>
<div class="controls">
<?php echo $this->element->avatarimage->renderViewHelper();?>
<?php if($this->element->avatarimage->hasErrors()):
$messages = $this->element->avatarimage->getMessages();
$messages = array_values($messages);
$message = $messages[0];
echo '<span class="help-inline">'.$message.'</span>';
endif;
?>
</div>
</div>
How can I incorporate my file element into my custom form viewscript?

This should definitely work.
<? echo $this->element->avatarimage->renderFile(); ?>

Try like this:
<?php echo $this->element->avatarimage;?>

Related

wp shortcode breaks get_post_thumbnail

I'm having some trouble with short code's breaking a featured image I have set on my home page in Wordpress (4.0). For some reason, when I paste in my shortcode in the wp-admin > page > edit > content for the home page, suddenly my featured image stops working. It's working until I paste in the shortcode and update the page, but then the image disappears and the_post_thumbnail() returns false. I've also tried get_the_post_thumbnail() without success.
Here's my code excerpt from "front-page.php":
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php if (has_post_thumbnail()) {the_post_thumbnail();} ?>
</div>
And here's the shortcode function from "functions.php":
// [random_testimonial]
function short_code_get_random_testimonial(){
$args = array( 'post_type' => 'testimonial', 'posts_per_page' => 1, 'orderby' => 'rand' );
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
$random_testimonial = get_the_content();
$author = get_the_title();
$rt = '<p class="quote">' . $random_testimonial;
$rt .= '</p><p class="right">– ' . $author . '</p>';
endwhile;
return $rt;
}
// Register the shortcodes.
add_shortcode( 'random_testimonial', 'short_code_get_random_testimonial' );
// Allow text widgets to contain shortcodes.
add_filter('widget_text', 'do_shortcode');
add_filter('the_content', 'do_shortcode');
Any ideas would be greatly appreciated.
I think I may have solved this one. It seems to have something to do with the fact that I was calling a custom post type with the shortcode, so by the time the_post_thumbnail() was called, the query had changed to refer to the custom post type included in the shortcode ("testimonial") rather than the parent post (in this case the home page). I revised my code as follows to catch the image before the shortcode is called, and all is working again:
<div class="small-12 medium-6 columns">
<?php while ( have_posts() ) : the_post(); ?>
<?php
// go ahead and get the post image in case [random_testimonial]
// shortcode is used in page content which will break the query.
if (has_post_thumbnail()) {$image = get_the_post_thumbnail();} ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
<div class="small-12 medium-6 columns">
<?php
// Echo post image from above.
if(isset($image)){echo $image;}
?>
</div>

bootstrap codeigniter form input and submit button

I am trying to go along with bootstrap and Codeigniter, but I am having a little problem, I cant seem to figure out how to align 3 simple elements into one row, so they are aligned perfectly next to each other.
I want my H3 tag, input field and my submit button from my form to be next to each other. I have already tried the span4 on the 3 objects within divs, then also display:inline, but nothing works correctly. I dont know why, but If I want them to be perfectly next to each other, I just have to put there margin-top: -7px and other margins on the other objects but this looks like a not a good approach on this matter at all. Could you please help me ? !
My code:
<div class="well">
<h3>My superb h3 tag!</h3>
<?php echo form_open('search/quick_search'); ?>
<?php echo form_error('search_query'); ?>
<?php echo form_input('search_query','','placeholder="Search query"'); ?>
<?php echo form_submit('submit_quick_search','Search','class="btn"'); ?>
<?php echo form_close(); ?>
How does it look
This is how I've been using then together (Horizontal form)
<?php echo form_open('search/quick_search', 'class="form-horizontal');?>
<div class="control-group <?php echo form_error('search_query')? 'error' : ''; ?>">
<label class="control-label" for="search_query">Search Term:</label>
<div class="controls">
<?php echo form_input('search_query','','placeholder="Search query"'); ?>
<span class="help-inline"><?php echo form_error('search_query'); ?></span>
<span class="help-block"></span>
</div>
</div>
<div class="form-actions">
<?php echo form_submit('submit_quick_search', 'Search', 'class="btn"'); ?>
</div>
<?php echo form_close(); ?>

how to check session in zend layout

layout.phtml:
<?php echo $this->doctype() ?>
<html>
<head>
<?php echo $this->headTitle()."\n"; ?>
<?php echo $this->headLink() ."\n"; ?>
<?php echo $this->headScript(); ?>
</head>
<body>
<div id="wrap">
<div class="header">
<div class="logo"><img src="../../images/logo.gif" alt="" title="" border="0" /></div>
<div id="menu">
<ul>
<li class="selected">home</li>
<li>about us</li>
<li>books</li>
<li>specials books</li>
<li>my accout</li>
<li>register</li>
<li>prices</li>
<li>contact</li>
<li>adddbook </li>
</ul>
</div>
</div>
<div class="center_content">
<div class="left_content">
<?php echo $this->layout()->content ?>
I am new to zend framework here i am facing problem with displaying navigations. in pure php i can serve my navigation by checking sessions. like this:
<?php
if($_SESSION['usertype']=='admin')
{
echo "<li>adddbook </li>";
}
?>
My question is how can i implement this in zend framework. Thanks
You can do this in a myriad of ways like the Zend_Auth, but the closes to the $_SESSION method is using Zend_Session.
First you need to start Zend_Session before any output is sent to the browser, just like a normal php session. I do it in my index.php right after setting all my paths and autoloaders.
Zend_Session::start();
Next step is creating a namespace for the userinformation and adding the relevant information to it, preferably when you authenticate the user.
$userInfo = new Zend_Session_Namespace('userInfo');
$userInfo->userType = 'admin';
This is the equivalent to setting a $_SESSION['userInfo']['userType'] = 'admin';
Last, get the information in your layout:
<?php
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
echo "<li>addbook </li>";
}
?>
Read this link for more information http://framework.zend.com/manual/en/zend.session.html
If you want to check an user role, you could use the class Zend_Auth provided by the framework. You can check the credential of a user and then affect it a role.
To retrieve this role you can check the identity of the user with the Zend_Auth instance :
$identity = Zend_Auth::getInstance()->getIdentity();
if (strcmp($identity->role, "admin") == 0) {
echo '<li>adddbook </li>';
}

Zend Layout Double Div

Hi i want to put two div in a zend view. After i want to have the two div in different position. My problem is that i want to have the normal content adn i want to add another div that rapresent the side menu
For example my view is:
<div id="view-menuscritte">
List menu
</div>
<div id="view-content">
<?php
echo $this->errorMessage;
echo $this->form;
?>
</div>
When I use in my layout
<div id="menu">
<img id="menusopra" src=<?php echo $this->baseUrl().'/img/menusopra.jpg'; ?> >
<div id="menuscrittegenerale">
<?php echo $this->layout()->menuscritte; ?>
</div>
<img id="menusotto" src=<?php echo $this->baseUrl().'/img/menusotto.jpg'; ?>
</div>
<div id="contentgenerale">
<?php echo $this->layout()->content; ?>
</div>
The problem is that the two div (->content and ->menuscritte) are in the same div.
view-menuscritte is not in menuscrittegenerale where it could be.
Inside folder of layout.phtml , create menu.phtml
cut and paste following content from your view into menu.phtml
<div id="view-menuscritte">
List menu
</div>
Now in your layout.phtml replace
<?php echo $this->layout()->menuscritte; ?>
with
<?php echo $this->partial('menu.phtml);?>

Place an image for photo form button in Zend form

I have created a form by Zend form that uploads pictures in jpg and png format. however, the form generates a default button and text that reads "no file chosen". How can I place my own designed button image and remove the default text for this form?
$photo = new Zend_Form_Element_File('photo');
$photo->addValidator('Count', false, 1)
->addValidator('Size', false, 200*1024)
->addValidator('Extension', false, 'jpg,png');
It looks like you need to use Zend_Form_Element_Buttom if you want to use an image.
Just make sure you pass in type=submit and you can use the style="" attribute to set your image.
You can pass in these using:
setAttribs(array('type'=>'submit', 'style'=> array('image' => 'image.png')));
I think this syntax is correct, but I'm sure I have the incorrect stlye attributes.
MDN Button Reference
$photo->setDecorators(array(
array('ViewScript', array('viewScript' => 'my_upload_button.phtml', 'abc' => $some_parameter)
);
then inside
my_upload_button.phtml do your own style, here is mine
<?php
$image = $this->element->getAttrib('class');//prendi il nome del file
$this->element->setAttrib('class', '');//cancella la class
?>
<td class="nome_campo">
<label for="<?php echo $this->element->getName(); ?>"><?php echo $this->element->getLabel(); ?></label></td>
<td class="valore_campo" colspan="1">
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<span class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</span>
<?php endif; ?>
<p class="hint"><?php echo $this->element->getDescription() ?></p>
<b>
<?php