How to retrieve the column from relation in YII2 - yii2-advanced-app

I am using ajax to call a method from the server that returns a string data. However I get the following error.
PHP Notice 'yii\base\ErrorException' with message 'Trying to get
property of non-object'
probably this line $first_day=$row->tblStaff2->first_day_service; gives the error.
public function actionGet_loyalty() {
$model = \common\models\staffs\TblStaff::find()->joinWith('tblStaff2')->all();
$string = "<table class='table table-striped'><tr><th>Name</th><th>Number of Years</th></tr>";
foreach ($model as $row) {
$first_day=$row->tblStaff2->first_day_service;
$midname = ucwords($row->midname);
$name = ucwords($row->firstname) . " " . $midname[0] . ". " . ucwords($row->lastname);
$string.="<tr>"
. "<td>" . $name . "</td>"
. "<td>" . $first_day . "</td>"
. "</tr>";
}
$string.="</table>";
return $string;
}
TblStaff relation
public function getTblStaff2() {
return $this->hasOne(TblStaff2::className(), ['staff_id' => 'id']);
}

I have modified your code. Find it below
public function actionGet_loyalty() {
$model = \common\models\staffs\TblStaff::find()->all();
$string = "<table class='table table-striped'><tr><th>Name</th><th>Number of Years</th></tr>";
foreach ($model as $row) {
//What I added
foreach($row->tblStaff2 as $value){
$first_day = $value->first_day_service;
//Here is where it stopped
$midname = ucwords($row->midname);
$name = ucwords($row->firstname) . " " . $midname[0] . ". " . ucwords($row->lastname);
$string.="<tr>"
. "<td>" . $name . "</td>"
. "<td>" . $first_day . "</td>"
. "</tr>";
}
}
$string.="</table>";
return $string;
}
You can remove the comments i added to it when you get it working.

Related

Send action links with buttons in Mail using YII2

I want to put action button in this mail format Using Yii2 ..
So that releveant person can take a certain actions (Accept or Reject) through mail itself.
Is there any way to send encrypted key or anything else so that particular usr can perform their operation using mail it self.?
As of now i am able to send only mail with normal text body , i want to send particular action link with button along with this mail.
How can i achieve this ?
Any help will be highly appriciated.
Thanks in Advance.
My Controller Code.
/* Sending Mail Function */
public function Sendemail($request, $receiver, $subject, $email_body) {
$empmodel = Employee::find()->where('EmployeeNo = "' . $request->createdby . '" ')->all();
$data = ServreqItems::find()->where('srno=' . $request->srno)->all();
$content = "<html><body>";
$content .= "<table align='center' width='70%' bgcolor='#e0e0e0' cellpadding='0' cellspacing='0' border='0'>";
$content .= "<tr><td>";
$content .= "<table align='center' width='100%' cellpadding='0' cellspacing='0' style='border:dashed #FFA500 2px; max-width:650px; background-color:#fff; font-family:Verdana, Geneva, sans-serif;'>";
$content .= "<thead>
<tr height='80'>
<th colspan='4' style='background-color:#f5f5f5; border-bottom:solid 1px #bdbdbd; font-family:Verdana, Geneva, sans-serif; color:#333; font-size:24px;' >" . $request->FormsName . " Details</th>
</tr>
</thead>";
$content .= "<tbody>
<tr align='center' height='10' style='background-color:#FFA500; font-family:Verdana, Geneva, sans-serif;'></tr>";
$empmdl = Employee::find()->where('EmployeeNo = "' . Yii::$app->session['username'] . '" ')->all();
for ($e = 0; $e < count($empmdl); $e++) {
$content.= "<tr>
<td colspan='4' style='padding:15px;'>
<p style='font-size:20px;'>Hi' " . $empmdl[$e]->EmployeeName . " !!! " . $email_body . "</p>
<hr />";
}
for ($i = 0; $i < count($data); $i++) {
$content.= '<tr><td style="padding-left:15px;padding-bottom:15px;">' . $data[$i]->fid->filable . '</td> <td style="padding-right:255px;"> : ' . $data[$i]->fivalue . '</td></tr>';
}
$content .= "<tr height='25'>
<td colspan='4' align='center' style='background-color:#f5f5f5; border-top:dashed #FFA500 2px; font-size:12px; '>
</td>
</tr>
</tbody>";
$content .= "</table>";
$content .= "</td></tr>";
$content .= "</table>";
$content .= "</body></html>";
$sender = 'test#abc.in';
$message = Yii::$app
->mail
->compose()
->setFrom($sender)
->setTo($receiver)
->setSubject($subject)
->setHtmlBody($content, ' text/html')
;
Yii::$app->mail->getTransport()->start();
Yii::$app->mail->send($message);
Yii::$app->mail->getTransport()->stop();
echo '<script>alert("Email Sent Successfuly")</script>';
}
encrypt url like as #kumar Rakesh explained
$id = $emp_id;
$static_key = "afvsdsdjkldfoiuy4uiskahkhsajbjksasdasdgf43gdsddsf";
$ids = $id . "_" . $static_key;
$b_id = base64_encode($ids);
Then you can make two links for Accept and Decline.
$url1 = base_url('Class_name/action_name/') . "/?id=" . $b_id."&action=accept";
$url2 = base_url('Class_name/action_name/') . "/?id=" . $b_id."&action=reject";
And in your Sendemail function do this:
$content .= "<p><a target='_blank' href='" . $url1 . "'>Accept</a></p>";
$content .= "<p><a target='_blank' href='" . $url2 . "'>Reject</a></p>";
As like your question , I have sent a link in my email for forget password.. This link Work for different functionality for different users. I am working it with codeigniter but it also work fine for all php frameworks.
e.g.
$id = $emp_id;
$static_key = "afvsdsdjkldfoiuy4uiskahkhsajbjksasdasdgf43gdsddsf";
$ids = $id . "_" . $static_key;
$b_id = base64_encode($ids);
$url = base_url('Access/newpassword/') . "/?id=" . $b_id;
Now $url is secure encrypted link. And this url send in body part of message e.g.
echo "<p><a target='_blank' href='" . $url . "'>Change Password</a></p>";
Send it in email . After click of user at this link in his/her email.. you can achive this reverse process of all thing eg..
$ids = $this->input->get('id');
$urlData = array('ids'=>$ids);
$iddecoded = base64_decode($ids);
$idsalt = explode('_', $iddecoded);
$id = $idsalt[0];
$salt = $idsalt[1];
Now $id is employe Id.. Try this php code. It will be working Fine for everyone.

A string replace with array

I am trying to create a simple image gallery for joomla 3. I have managed to get the code working. The way it works is that, it detects the string {dGalley}stories{/dGallery} from article. I have done this so far. It works but it only prints one image. Any suggestion? Thanks in advance.
<?php
defined('_JEXEC') or die;
class PlgContentgallery extends JPlugin
{
public function onContentPrepare($content, $article, $params, $limit){
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
$i=0;
foreach ($matches[1] as $value)
{
$result = array();
$dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}','<img src="../images/'.$matches[1][$i].'/'.$value.'"/>
', $article->text);
}
}
}
$i++;
}
}
}
Updated code. It works but a bit dirty solution:
<?php
defined('_JEXEC') or die;
class PlgContentgallery extends JPlugin
{
public function onContentPrepare($content, $article, $params, $limit)
{
$document = JFactory::getDocument();
$document->addScript(JURI::base(). "plugins/content/gallery/jquery.colorbox.js");
$document->addScript(JURI::base(). "plugins/content/gallery/colorboxCall.js");
$document->addStyleSheet(JURI::base(). "plugins/content/gallery/colorbox.css");
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
$i=0;
foreach ($matches[1] as $value)
{
$result .= '';
$dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}else{
$result .='<a class="group4" href="../images/'.$matches[1][$i].'/'.$value.'" title=""><img src="../images/'.$matches[1][$i].'/'.$value.'" class="gallery"/></a>';
}
}
}
$article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}',$result, $article->text);
$i++;
}
}
}
You want something more like this
public function onContentPrepare($content, $article, $params, $limit)
{
preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);
foreach ($matches[0] as $folder)
{
$result = array();
$dir = '/home/juniorwe/public_html/images/' . $folder; // You might want to get the stored value for image path instead of hard coding
if (is_dir($dir))
{
$cdir = JFilesystem::files($dir);
foreach ($cdir as $value)
{
$article->text = str_replace('{dGallery}' . $folder .'{/dGallery}','<img src="/images/' . $folder . '/' . $value. ' "/>', $article->text);
}
}
}
}
Is it that you are trying to get the images from the sub folders also?

openssl_sign and PHP 4 identical signature for different messages

I have to maintain an application in php 4 that must send signed data via openssl_sign. The issue is that for different data of the same size, the signature is always the same.
Eg. this code:
$signature = null;
$message1 = 'foobar';
$privkey1 = openssl_pkey_get_private('file://path/to/private/key/privkey1.pem');
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
openssl_sign($message1, $signature, $privkey1);
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
openssl_free_key($privkey1);
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
$signature2 = null;
$message2 = 'foobaz';
$privkey2 = openssl_pkey_get_private('file://path/to/private/key/privkey1.pem');
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
openssl_sign($message2, $signature2, $privkey2);
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
openssl_free_key($privkey2);
while ($msg = openssl_error_string()) {
echo $msg . "<br />\n";
}
echo base64_encode($signature) . '<br/>';
echo base64_encode($signature2) . '<br/>';
Outputs this:
uANYD6qKuvlcyK2svarB0ESPO7qLa75cEIhCmjkTF23cwveSE+Mxuhsl7JKjOEOPf7v8mCoTLmdlm/2RDD0Nabdpi+5Ez8Di8dFNpXtMVRByJvewOOGxTgYt/1XPIqe+dvLunkqtl8dHkRhtzuBHay1suco53Ybs7r41YKdqnkk=
uANYD6qKuvlcyK2svarB0ESPO7qLa75cEIhCmjkTF23cwveSE+Mxuhsl7JKjOEOPf7v8mCoTLmdlm/2RDD0Nabdpi+5Ez8Di8dFNpXtMVRByJvewOOGxTgYt/1XPIqe+dvLunkqtl8dHkRhtzuBHay1suco53Ybs7r41YKdqnkk=
Does anyone know what the reason of this problem?

CGI script not displaying results in browser

I am attempting to display some info from an infoblox device. When I run this code in a browser using an html post, the table that displays MAC address entries does not display the values from the api. When I run this code in the unix command-line, the variables show up appropriately. Any Advice?
#!/usr/bin/perl
use strict;
use Infoblox;
use CGI;
my $cgi = new CGI;
print
$cgi->header() .
$cgi->start_html( -title => 'Form Results') .
$cgi->h1('Form Results') . "\n";
my #params = $cgi->param();
my $username = $cgi->param('username');
print '<table border="1" cellspacing="0" cellpadding="0">' . "\n";
foreach my $parameter (sort #params) {
print "<tr><th>$parameter</th><td>" . $cgi->param($parameter) . "</td></tr>\n";
}
print "</table>\n";
print "<p>$username</p>";
print $cgi->end_html . "\n";
#Create a session to the Infoblox appliance
my $session = Infoblox::Session->new(
master => "server", #appliance host ip
username => "username", #appliance user login
password => "password" #appliance password
);
unless ($session) {
die("Construct session failed: ",
Infoblox::status_code() . ":" . Infoblox::status_detail());
}
print "Session created successfully\n<br>";
my #list = $session->get(
object => "Infoblox::DHCP::MAC",
filter => "macfilter",
);
my $nextobject = $list[0];
print <<EOF;
<br>
<table>
<tr>
<th>MAC</th>
<th>Description</th>
<th>UserID</th>
<th>Expiration</th>
</tr>
EOF
foreach my $test ( #list ) {
print "<tr>";
print "<td> $test->mac()</td>";
print "<td>" . scalar($test->comment()) . "</td>\n";
print "<td>" . scalar($test->username()) . "</td>\n";
print "<td>" . scalar(localtime(scalar($test->expiration_time()))) . "</td>\n";
print "</tr>";
}
exit (0);
I had incorrect permissions. The script was running as user nobody and would not display the items correctly on the web page.

Zend Search Lucene And Persian Language !

i have the following code in my zf project :
$index = Zend_Search_Lucene::open(APPLICATION_PATH . '/cache/search_index');
$doc = new Zend_Search_Lucene_Document();
$title = "سلام سینا xxx sad";
$doc->addField(Zend_Search_Lucene_Field::Text('title', $title));
$index->addDocument($doc);
$index->commit();
$index->optimize();
echo "Index contains " . $index->count() . " documents.\n\n";
$results = $index->find('xxx');
foreach ($results as $res) {
var_dump($res->title);
}
when var_dump performs output ->
string(39) "سینا جان xxx sad"
when i user utf_decode
string(25) "س�?ا�? س�?�?ا xxx sad"
how can i decode that correctly ! :(?
i already used the solution in this SOF quesion -> lucene encoding problem in zend framework
but not works and a notice error added about iconv !
plz help :)
Fixed by this code:
$index = Zend_Search_Lucene::open(APPLICATION_PATH . '/cache/search_index');
$doc = new Zend_Search_Lucene_Document();
$title = "سلام سینا xxx sad";
$doc->addField(Zend_Search_Lucene_Field::Text('title', $title,"UTF8"));
$index->addDocument($doc);
$index->commit();
$index->optimize();
echo "Index contains " . $index->count() . " documents.\n\n";
var_dump($index->getDocument(9));
echo "Search";
$results = $index->find('سینا');
foreach ($results as $res) {
var_dump($res->title);
}
die(1);