Zend Framework 2 - Get content from other module - php

My project looks like this:
- root<br>
- module
- Application
- view
- application
- index
- index.phtml
- ZfcUser
- view
- zfc-user
- user
- login.phtml
(Sorry if isn't readable it's my first post I tried my best)
When I go to (from my web browser) http://site.local/user and I'm authenticated
ZfcUser display login.phtml
Now I want to show some information coming from my module application but I don't want to write specific code on login.phtml or even on any ZfcUser controller since I think it isn't a good practice.
The good practice(I guess) should be to have a prepared controller on application where I give some content to the caller.
Can I get that directly from login.phtml?
Sorry for my English. It isn't my main language if you have trouble understanding my problem feel free to reply and asking for more clarification.
I'm looking for the best method to do this to really understand how it have to be done.
Thanks for paying attention to my problem.

Thanks for the grammar correction.
Since I got some negative feedbacks (-3) I post my solution here maybe if someone got the same problem it will be helpful...
I have put on the login.phtml something like:
<?php
use Application\Controller\News
$news = new News();<br>
$newsContent = $news->getNewsTable();
?>
Then I can use the result from this function on my view.
It works and I don't think it's a good practice and should be avoided but at least it works.... :|

Here is my solution (The previous one was wrong but I let it for informal/logical purpose about the answers I got)
Instead of going into ZfcUser after login I have route the request to ZfcDataGrid.
To do that simply edit the UserController.php on ZfcUser/Controller/
and modify indexAction to looks like:
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}
return $this->redirect()->toRoute('ZfcDatagrid');
Here we check if we are authenticated, if we are we go to ZfcDataGrid, if not we go to default zfcuser login.
On my Data file I used this: (on ZfcDataGrid Data file provides the content used to render the grid)
$sm = $this->getServiceLocator();
$this->newsTable = $sm->get('Application\Model\News');
$news = $this->newsTable->fetchAllToArray();
foreach ($news as $index=>$value) {
$data[$index] = $value;
}
return $data;
Then on my .phtml
<div name="zfcuser" style="position:relative; top:10px; left:80%;">
<div style="float:left; padding-right:16px;">
<?php echo $this->gravatar($this->zfcUserIdentity()->getEmail()) ?></div>
<h3><?php echo $this->zfcUserDisplayName() ?>!</h3>
[Sign Out]
<div style="clear:both;"></div>
</div>
<a class="btn btn-primary" href="<?php echo $this->url('ZfcDatagrid') ?>"
<?php echo $this->translate('cancelback') ?>
role="button" class="btn" data-toggle="modal">Cancel and back to previous page</a>
<?php
echo '
<h2>Title : ' . $new[0]['title'] . '</h2>'; ?>
<form method="post" action="somepage">
<textarea name="content" style="width:100%">
<?php echo '<p>' . $new[0]['content'] . '</p>
'?></textarea>
</form><a class="btn btn-primary" href="<?php echo
$this->url("news")."save";?>">
<?php echo $this->translate('Validate'); ?>
</a>
So the answer I found it's to use model you need on the other module controller (use Application\Model\NewsModel;)
Then on the controller get the data from your model and pass the results to your view.
Then on your view show the data.
If someone can tell me if it's the right way to do what I wanted or if needed more explanation feel free to ask.
Thanks again for your help Carlos, I really appreciate it.
Happy new year :D

Related

Get content from WordPress to php

I've been trying to get information from WP for a while now and I still can't get it to work. What I'm trying to do is to get the information from an online resume and display it on the site.
The problem is that I haven't worked with WP a lot before and I can't figure out if there is a better way than SQL extraction to get that information.
This is the code I have now, it's very simple but doesn't work.
$output = '
<html>
<head>
<link rel="stylesheet" type="text/css" href="style1.css">
</head>
<body>
<div id="header">
<h1 class="title">'.the_custom_field('_candidate_name', 144).'</h1>
<ul class="contactInformation">
<li id="fullName">//This is where the name should go</li>
<li id="adress">//I want to fetch the adress and echo it here etc.</li>
<li id="phoneNumber"></li>
</ul>
<div id="resumePicture">
<img src="mVqghXM.jpg" height="auto" width="100px">
</div>
</div>
<div id="education">
<ul>
</ul>
</div>
<div id="experience">
</div>
</body>
</html>';
I've tried using get_custom_field as well as get_resume_field and placing them into a variable but i get an error that says it's undefined, and I presume that is because I don't have the WP Job Manager Field Editor plugin.
I would rather find another way to do this, either via some other WP compatible function or via SQL, instead of having to buy that plugin.
I would also love one of you to explain to me briefly how the WP system/database is built, because that would help immensely.
Thank you!
EDIT: I found the way of fetching the data from the database. It was get_resume_field, but what I'd done was copy the meta_key straight off the database, being "_candidate_name", but when I took the first underscore away, it worked. Thank you both for helping with the two problems I had. Cheers!
Use three files in your custom php file to get and use all wordpress function. Try this.
include_once("/path/to/wordpress/wp-config.php");
include_once("/path/to/wordpress/wp-load.php");
include_once("/path/to/wordpress/wp-includes/wp-db.php");
You have made to collect all the HTML into a single variable as per you need.
The major important thing is that you have to echo/ return the variable to print the output.
This will do the trick if you return it from a function
function get_page()
{
$output = '<p>Welcome</p>';
$output .= '<p>Again</p>';
return $output;
}
After the return you have to echo the statement by calling the function
$test = get_page();
echo $test;
Output:
Welcome
Again

Cakephp, adding a view to a sidebar

I'm struggling trying to understand cakephp's views, blocks and layouts.
I need everypage to show a left and right sidebar which content might change. At this moment I have the right sidebar defined in /pages/home.ctp but I'm guessing it would be better to extend that sidebar since it has to appear in everypage. Correct me if that thought is wrong.
Then, I have this view add.ctp for the 'usuarios' table, it practically shows the fields login and password. I want to show this view in the sidebar, but I'm really lost as how to do that.
Thanks in advance.
Lets make this thing easy. Like #patrick said, there is a lots of way.
Start with layout file. Rearrange your default.ctp layout like-
default.ctp layout
<div id="container">
<div id="header">
<?php echo $this->element('header');?>
</div>
<div id="left-sidebar">
<?php echo $this->element('left-sidebar');?>
</div>
<div id="content">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
</div>
<div id="right-sidebar">
<?php echo $this->element('right-sidebar');?>
</div>
<div id="footer">
<?php echo $this->element('footer');?>
</div>
</div>
Now create elements ctp files as header.ctp, left-sidebar.ctp, right-sidebar.ctp and so on and place them to app/View/Elements.
Your left-sidebar.ctp file may looks like this...
left-sidebar.ctp
// to show login form //
if you just need to show on view.ctp place few logic here for login form.
//end login form//
show other sidebar contents
There are a couple ways to do it, depending on your Cake version. If you're using >=2.1 (which I assume you are since you asked about blocks), then you should try those to see if they work for your setup. The way I usually do things is that if all views for a controller need common markup then those view files would extend a base view within the Controller directory, e.g.
#/View/Posts/index.ctp
<?php
$this->extend('_skel'); //arbitrary filename, I use '_skel' since that makes sense
echo $this->Html->para(null, 'Hello');
#/View/Posts/_skel.ctp
<?php
echo $this->Html->div('sidebar', 'Sidebar for posts...');
echo $this->fetch('content'); // This gets all output from the Posts/index.ctp view
Then all your Posts views which extend _skel will have the sidebar automatically.
Your login module might make sense as an element - something that could be used anywhere in your views.

Can a Joomla module “know” what position it's in? (2)

This is my very first question at StackOverflow.
I was trying to post comment in the post Can a Joomla module "know" what position it's in?, but could find no way to do that so I have to post another question here. Any tips regarding how to do this properly is greatly apprieciated.
Anyway, here is my questions:
I tried the code mentioned in the above post but it seemed didn't work. I'm not only quite new to PHP, but also coding, so I'm not sure if it's me or the code. here's what I did to the module's default.php file:
1)to ensure I insert the code at the right place, I insert
<?php echo(print_r($module)); ?>
and it output 1 at the right position;
2)the position-name that I need to determine is "showcase-a", so I insert code
<?php if ($module->position == 'showcase-a'):?>test<?php endif;?>
to the above place, but this time it does't show anything;
3)then I tried this code:
<?php if ($module):?><span>test</span><?php endif; ?>
But still it does't display "test" at the position as I expected.
4)I tried
<?php if (1):?><span>test</span><?php endif; ?>
and the test "test" displays as good. So I didn't code the IF statement wrong.
Now I'm totally lost. Since print_r($module) outputs 1, $module must be positive, why PHP ignore test in 3)? This is just a side question for the sake of PHP learning. What I still need to solve is let the module determine which position itself is in.
Please help. Thank you!
I'm not sure about using it in the template (although it doesn't seem wrong altogether) but my modules oftentimes access the position like this:
$module->position
in the module (so mod_something.php) so try to put it there, if it's available just set a variable and it will be available in the view too.
If $module->position didn't work for you. $module->position may only work for joomla 1.7, 2.5 and + but i'm not sure. $module->position works in joomla 2.5.
Otherwise you need to make a module.php function
Check out the file
/templates/you-template/html/modules.php
For example you make your position the following in your template (for example index.php)
<jdoc:include type="modules" name="header" style="includeposition" />
inside of
/templates/you-template/html/modules.php
You make a function like this :
<?php
function modChrome_includeposition($module, &$params, &$attribs){
//Set a value to $module->position
$module->position='WHATEVER YOUWANT';
?>
<div class="moduletable <?php echo $params->get('moduleclass_sfx').' '.$module->name; ?>">
<?php
if($module->showtitle){
?>
<div class="modtitle">
<div class="fleche">
<span>
<?php
echo $module->title;
?>
</span>
</div>
</div>
<?php
}
?>
<div class="modcontent">
<?php echo $module->content; ?>
<div class="clear">
</div>
</div>
</div>
<?php
}
?>
make different functions names that sets different values if needed.
the function name will match the syle you set:
modChrome_includeposition($module, &$params, &$attribs)
because
style="includeposition"
inside of your jdoc.

Redirecting to landing page or home page index based on User Group

I tried Googling and using forums.joomla.org, because I wanted to make sure I wasn't wasting people's time on here, but unfortunately I have had no luck. Here's my issue:
For my website, I have both a landing page which only has the link to register for free to become a member, and a proper index page which users access after they become registered. I set up the landing page as kanadax.ca/ because I want people to land there when they first come to the page. My real index page is located at kanadax.ca/index.php. However, now when users are already logged in and they click on the website logo to go to the root domain, they get a page not found error.
So what I want to do is:
- If it's a non-registered guest user, have the logo redirect to the landing page located at kanadax.ca/.
- Once a user becomes registered, anytime they go to kanadax.ca/index.php it should redirect to example.com/index.php.
Basically I'm looking for conditional redirecting based on user groups. Any ideas?
Now obviously I would actually prefer example.com/ url as main index page rather than the less attractive example.com/index.php but that's a second step after I fix the redirection. I believe it can be done with .htaccess but with the more complicated landing page - index page structure that I have for guest and registered users I think it may be a bit difficult. However, please do give feedback if you have any in this area as well.
I should mention I'm not super comfortable with php but I'm happy to try any suggestions if you'd like to give me a step by step. After all, my first goal in making this website is to learn!
Thanks a heap!
PS if necessary, I'm happy to provide a test account so you can play around with the website yourselves.
**Edit:
Hey #Adam, I really appreciate the input and the step by step, it's exactly what I needed. Now, I have put the first section at the beginning of index.php, however for the latter part, I'm not quite sure where to put it. When I search for anything logo related within index.php, two separate sections come up:
// Logo file or site title param
if ($this->params->get('logoFile'))
{
$logo = '<img src="'. JURI::root() . $this->params->get('logoFile') .'" alt="'. $sitename .'" />';
}
elseif ($this->params->get('sitetitle'))
{
$logo = '<span class="site-title" title="'. $sitename .'">'. htmlspecialchars($this->params->get('sitetitle')) .'</span>';
}
else
{
$logo = '<span class="site-title" title="'. $sitename .'">'. $sitename .'</span>';
}
and
<div class="header">
<div class="header-inner clearfix">
<a class="brand pull-left" href="http://www.kanadax.ca/index.php/anasayfa">
<?php echo $logo;?> <?php if ($this->params->get('sitedescription')) { echo '<div class="site-description">'. htmlspecialchars($this->params->get('sitedescription')) .'</div>'; } ?>
</a>
<div class="header-search pull-right">
<jdoc:include type="modules" name="position-0" style="none" />
</div>
</div>
</div>
second part being under header. Do you think either of these is the right section? If so, should I delete what is in there and replace with your suggestion, or keep it?
Try to modify your template index.php file in the following way:
Add this somewhere at the begin of the code
$user = JFactory::getUser();
if(JFactory::getUser()->id)
{
//link to page for registered users
$link = JRoute::_('index.php?option=com_content&view=article&id=3');
}else{
//link to page for default users
$link = JRoute::_('index.php?option=com_content&view=article&id=1');
}
In the place where the logo should be displayed
<a href="<?php echo $link; ?>">
<img src="images/path_to_image_name"/>
</a>
And that is it!
Latter when you are more familiar with Joomla and PHP you may create a custom module instead of injecting the template directly.
If you don't want to hack anything, you could also try sticking some redirect code in a custom html module in a debug or other position, but use Advanced Module Manager to only load it for guests or only load it for logged in users. You can do all kinds of tricks with AMM to load and not load things based on a zillion parameters.
Adam's answer is cleaner, but some people are really hesitant to hack their template (or if it's a commercial template it might be really messy to find the right spot to put the code in).
You can read more about AMM here:
http://www.nonumber.nl

New(relatively) to code igniter : some basic questions

I'm typing this on an iPad so forgive me if being concise is a bit rude. My question is:
Is it ever ok to have simple logic inside a view? For instance,
<HTML>
<!-- ... Stuff
-->
<?php if($this->session->userdata('authorized'): ?>
<p>You are authorized</p>
<?php else: ?>
<p>You are not authorized</p>
<?php endif; ?> // Question 1, is this the proper use of an endif:?
<!-- .. Stuff
->>
</HTML>
/* Starting to type the rest of message on my laptop. Big thank yous to the coders on
this site who made my pc login transfer my unsaved, half typed iPad post */
The above was just a leftover comment in the code that would have made the limited php use look ugly. I wanted to let it be seen though.
Anyways, on to question #2:
Is it even proper to use a simple conditional like this in a view?
Thanks for reading, and hello again.
In answer to the first question:
That is the proper use of an endif, all is valid and recommended way by codeigniter.
In terms of the second question, this method can be used in a view file; however I would recommend using it in the $data array passed to the page meaning it will be accessed as $authorised; I say this as it will make more sense to a front end designer.
Information on the $data array can be found here, just navigate to "Adding logic to the controller".
I hope this is of help to you.
I recommend you to use differents views in the controller for each case:
// In the controller
if($this->session->userdata('authorized')
$this->load->view('not_autorized.php');
else
$this->load->view('view.php');
So you get clean code an views.
Use the Language class to store your 'You are authorized' and 'You are not authorized' text. Do your session check in the controller, and pass the correct language value to the view in the data array.
Edit: Additional question from STONYFTW:
What approach should one take with a bit of more complex code, such as:?
<?php if(!$this->session->userdata('isLoggedIn')): ?>
<div id="login_form">
<?php echo form_open('login/validateCredentials'); ?>
<?php echo form_input('username', 'Username'); ?>
<?php echo form_password('password', 'Password'); ?>
<?php echo form_submit('submit', 'Log In'); ?>
<div id="login_form_link_container">
<?php echo anchor('login/register', 'Register')." ".anchor('login/recover','Forgot Pass?'); ?>
</div>
</div>
<?php endif; ?>

Categories