Notification icon hide number after click - php

I use October and I have made plugin with builder and I just add notification icon in menu.
I want to show "!" if there is a new item and hide "!" if user click that (it open bootstrap modal). And when I add new item it shows "!" again etc... I don't know exactly what is the best way to do this. Thank you if you can help me with this.
Menu:
<li>
<a data-toggle="modal" data-target="#itemModal"><i class="fa fa-bell"></i>
<span>! {{ Item }}</span></a>
</li>
Code:
function onStart()
{
$this['Item'] = Db::table('items')->count();
}

for this you need db table and you need to save last seen/click date.
so idea here is like you will add current date when user click !, you will show bootstrap modal and fire ajax request and set current date.
fire ajax request at that time and add new date for param.
for this you can use CMS default params table
<?php
use System\Models\Parameter;
....
// something like this in ajax request
Parameter::set('your_plugin_name::items.last_click_date', time() );
// this is when page reload
use System\Models\Parameter;
function onStart()
{
// now
$lastClickDate = Parameter::get('your_plugin_name::items.last_click_date');
if($lastClickDate == null) {
// first time it will just count items directly as user never click on icon
$count = Db::table('items')->count();
}
else {
// new this will call next time as user called that modal and we added click date
// so we compare if any record added after that date if it is then show `!`
$count = Db::table('items')->where('created_at', '>', $lastClickDate)->count();
}
$this['item_count'] = $count;
$this['new_items'] = $count > 0 ? true : false;
}
html
<li>
<a data-toggle="modal" data-target="#itemModal">
<i class="fa fa-bell"></i>
{% if new_items %}
<span>! {{ item_count }} </span>
{% endif %}
</a>
</li
please add comment if you face any problem.

Related

How can other users view a user's profile details in octobercms?

Displaying a user's information to the user is pretty much straight forward using their id or name in the url as parameter and displaying specific details using {{ user.name }}, {{ user.phone }} etc. But how can i display a specific user info to other users. When i click on www.example.com/profile/joe, it takes me to joe's profile. Trying it logged out or from a different account throws up an error.
This what i have done so far. my profile url has
url = "/:id"
and my php block
[session]
==
<?php function onStart(){
$profile = $this->param('id');
}
?>
==
this links up to the profile page
<button style="font-family: arial" class="btn btn-sm btn-light rounded--lg-pill shadow rounded border col-6">View Profile</button>
using {{ user.name }}, {{ user.phone }} etc shows all the logged in user's info. I am just at lost here how to make the profile of a user available to other users.
Edit:
Currently working if i use $userProfile = Auth::findUserById($profile)
connected to <a href = ''{{ id: users.id }}''>View Profile></a>
It still works if I change it to
$userProfile = Auth::findUserByLogin($profile)
connected to
<a href = ''{{ id: users.username }}''>View Profile></a>
or
<a href = ''{{ id: users.email }}''>View Profile></a>
but i would have loved it to work using a different field
<a href = ''{{ id: user.company_name }}''>View Profile></a> instead of id or login details
Suppose we are generating link : www.example.com/profile/joe
so id will be joe now we need to find that user's record and use it
to find it in code
[session]
==
<?php function onStart(){
$profile = $this->param('id');
$this['userProfile'] = Auth::findUserByLogin($profile);
// you can use other field if you need
// $this['userProfile'] = \RainLab\User\Models\User::where('username', $profile)->first();
}
?>
==
use it in template
{{ userProfile.name }}, {{ userProfile.phone }}
if any doubts please add comment.

laravel destroy just deletes last added stuff

I am a Laravel newbie. While writing and testing my code, I noticed that my destroy method isn't working right anymore and I cannot find the mistake I've made. So I hope you can help me out.
Whats my (software) target? I want to manage "projects". Every project has many reports. So I got a page with all created projects and I got a page with all reports listed in a table with buttons for "modify" and "delete". I finished all the CRUD stuff for projects and reports, when I recognized that, if I am hover over the delete-button of a report or project, the right ID of the chosen report or project is shown. If I am hitting the delete-button a dialog plopps up and a message is shown: "Do you really want to delete..." with "yes" and "no" buttons. So if I am pressing the "yes"-button Laravel is going to delete the last added database entry.
Even the projects as the reports too got their own controller. But both are using the same _messages.php. I think, that my mistake is in that file.
Excerpt from _messages.php:
#if(Session::has('sweet_alert.confirmDeleteReport'))
<div class="alert alert-warning" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div class="row">
<div class="col-xs-12">
<strong>Achtung!</strong> {{Session::get('sweet_alert.confirmDeleteReport')}}
</div>
</div>
<div class="row">
<div class="col-md-3 col-md-offset-8">
<a class="btn btn-danger" href="{{ route('reports.destroy', $report->id) }}" title="Löschen" data-token="{{csrf_token()}}" data-method="delete">Löschen</a>
<a class="btn btn-default" data-dismiss="alert">Abbrechen</a>
</div>
</div>
</div>
#endif
Excerpt from reportcontroller.php:
public function destroy(Report $report)
{
$report->delete();
Session::flash('sweet_alert.success','Der Bericht vom ' . $report->date . ' mit der Berichtsnummer ' . $report->reportNumber . ' wurde erfolgreich gelöscht.');
return redirect()->route('reports.index');
}
public function delete(Report $report) {
Session::flash('sweet_alert.confirmDeleteReport', 'Soll der Bericht vom ' . $report->date . ' mit der Berichtsnummer ' . $report->reportNumber . ' wirklich gelöscht werden? Dieser Vorgang kann nicht rückgängig gemacht werden.');
return redirect()->route('reports.index');
}
Might there be the fault within the session? I flushed the cache by executing php artisan config:cache but with no luck. Every idea is welcome.
Greetings
If I understand correctly you have a page which lists all reports. Each report has a delete button, and that button route maps to the delete() method you include above. That method flashes a msg to the session, and reloads your report index.
Now on your report index, all reports are listed again, probably inside something like #foreach ($reports as $report). At the bottom of the page, you have the extract you have shown. This uses $report->id, which on this page, after looping through all $reports, is just the last report in that collection. So all it will do is delete the last report.
There are a few solutions I can see:
1) Flash the id of the report you want to delete along with your message in the delete() method. I am not familiar with sweet_alert but can you flash an array, your msg and the id? Something like:
Session::flash('sweet_alert.confirmDeleteReport', [
'id' => $report->id,
'message' => 'Soll der Bericht ...'
]);
And then of course use that id in your alert:
<a class="btn btn-danger" href="{{ route('reports.destroy', Session::get('sweet_alert.confirmDeleteReport.id')) }}" ... </a>
Maybe the syntax is wrong, but I am assuming flashing an array is possible.
2) The way I usually do this kind of thing is use Javascript to populate the alert/modal/whatever with the required data when you click the delete button in the table. Eg your delete buttons (in the main table/list) could have a data-id attribute:
Delete
And some Javascript (pseudo code, implementation depends on how you have things set up):
$('a.delete').on('click', function(e) {
// Prevent the default behaviour of clicking a link
e.preventDefault();
// Find the id of the report clicked
var id = $(this).data('id');
// Update the href of the alert button so it will delete *that* report
$('div.alert a.danger').attr('href', 'reports/' + id);
// ... code to display the alert/modal
});
This way you avoid the page reload to get to your alert/modal. Note this makes your delete() method obsolete, it would not be used any more.
UPDATE
If you need to pass some variable from your view/controller to your external JS file, you can do something like this:
In the view
<script>
var url = "{{ route('reports.destroy', Session::get('sweet_alert.confirmDeleteReport.id')) }}";
</script>
In your external JS (must be loaded after the above inline script)
$('div.alert a.danger').attr('href', url);

In Codeigniter : How to get url if i clicked on the fetched data from database

I get this data through one controller in a list in my view files,and i also want to show in every page a different meta title. How it is Possible ?
View
<?php
$data = "";
foreach($items_data as $row){
$data .= '<li class="list-group-item">
<a href="'.base_url("hotels/ls/".$row->item_id).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
</li>';
}
echo $data;
?>
Suppose i got four items like
Books
Pens
Computers
All of my items are showing these urls which is pretty good.
www.xyz.com/1
www.xyz.com/2
www.xyz.com/3
But i want to show my item names in every url. along with different meta tag title name.
Now when i click on any listed items my url will show something like these
This is possible or not , Because above list are getting from database.
www.xyz.com/computers
www.xyz.com/Books
www.xyz.com/Pens
Here is my controller which is fetching data from database.
Controller
function index() {
$data['items_data'] = $this->Visa_mdl->get_items_name();
$data['title'] = "Hotels";
$this->load->view('include/header');
$this->load->view('hotels/visa',$data);
$this->load->view('include/footer');
}
This Controller is Linked to other Pages.
function ls($item_name = NULL) {
$data['title'] = "Company Lists";
$result = $this->Visa_mdl->get_items_company_list($item_name);
$result1 = $this->Visa_mdl->get_items_name_in_dash($item_name);
$data['items_data'] = $this->Visa_mdl->get_items_name();
// $data['items_company_data'] = $this->Visa_mdl->get_items_company_list($id);
$this->load->view('include/header',$data);
$this->load->view('hotels/dashboard',$data);
$this->load->view('include/footer');
}
Instead of this:
<a href="'.base_url("hotels/ls/".$row->item_id).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
try
<a href="'.base_url("hotels/ls/".$row->item_name).'">'.$row->item_name.'<span class="fa fa-angle-double-right pull-right" style=""></span>
and try again.
In your code you have passed $row->item_id which shows item id in url, instead of that pass $row->item_name then it will show item name in url.

if else statement from database record laravel 5.2

i want to create if else from database record.
for example : i've record "1" in "status" row.
"if status = "1"
echo Show Button
else
don't show button"
what code in controller and view?
Just put in the view your if
#foreach ($itens as $item)
#if ($item->status == 1)
<input type="button"/>
#endif
#endforeach
In your controller you don't need to care about it
You can use Laravel Blade Template Control Structures for this
i.e.
#if($status == 1)
...
<!-- HTML Code to generate button -->
...
#endif
Read more here

Get correct index with Splice()

I'm attempting to delete items from my AngularJS view. From the view, I'm passing the index & the ID. In the JS controller, I splice(index) from the AngularJS array/model. And I pass the ID to a $http.get to call a delete to my database. This all works.
....
Until I make a request to refresh my page. Every 5 seconds I make a request to update the Angular model and I concat any new data to it. But what I'm finding is it screws up my index values. I assumed it was reordering my index: and reading this StackOverflow thread helped confirm that.
What would happen is that I push delete for the 3rd item on the page, and it deletes that ID from the DB. But it splices the wrong item from the page until I refresh the page.
How do I make sure that I'm always passing the correct index?
View
<li ng-repeat="new in news | filter:query | orderBy:orderProp">
<div class="newsitem" id="newspost{{new.id}}">
<h4 ng-model="news.title">{{new.title}} </h4>
<p ng-model="news.text">{{new.text}}</p>
<a class="btn btn-info" ng-click="visible = true">View article</a>
<a ng-click="deleteNews(index,new.id)" class="btn btn-danger btn-mini">Delete</a>
</div>
</li>
controllers.js
$scope.deleteNews = function(index, id) {
$http.get('/ci/index.php/news/delete_news_ajax/'+id).success(function(){
$scope.news.splice(index, 1);
});
};
Instead of passing the index and id, pass new:
<a ng-click="deleteNews(new)" ...>
Then use indexOf in your controller function:
$scope.deleteNews = function(newsItem) {
$http.get('/ci/index.php/news/delete_news_ajax/' + newsItem.id)).success(function() {
$scope.news.splice($scope.news.indexOf(newsItem), 1);
});
}

Categories