I'm using default delete of Yii. And I just want soft delete a row, it's mean I keep that row and update column deleted from 0 to 1.
That my code in Controller:
public function actionDelete($id)
{
$model = $this->loadModel($id);
if (Posts::model()->countByAttributes(['category_id' => $model->id]) == 0) {
$model->deleted = 1;
$model->update();
}
if (!isset($_GET['ajax'])) {
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
}
Now I just want if Post have any category, it will show popup cannot delete this Post.
Any solutions about this?
Ugh... usualy you would add errors in the validator. Honestly that is the best advice... well the Yii way. You could just check if $_POST is set, mass assign $_POST to $model->attributes and then let model worry about validation. What I would do next is write a custom but simple validation function in the model that would check if there are categories for said model and if yes return true, otherwise false. Then you would just do:
if($model->validate())
{
$model->delete = 1;
if($model->update)
{
// do whatever you wanna do redirect/render...
}
else {
Yii::app()->user->setFlash('error', "Unable to delete post");
// render or redirect... flash is more for redirect but it'll do the trick
}
}
This would solve the backend of it. In the view file you need to display this flash. In the view that you render if it's not deleted add this piece of code:
<?php if(Yii::app()->user->hasFlash('error')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('error'); ?>
</div>
<?php endif; ?>
I would do it this way... you can do whatever you want to do
Related
I am working on a page that can update the sales agent on a specific order.
I made the list of options and a dropdown list is created.
Heres in the controller:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Heres the view:
echo $this->Form->create("Order");
echo $this->Form->input("OrderID");
echo $this->Form->input("UserID");
echo $this->Form->submit("Submit");
echo $this->Form->end();
When I submit the data, it appears that the data is saved, (the flash message is set).
However, when I then preset the fields with data that is already in the database, all the sudden it doesn't even post. (I put a debug after the reques->is("post) condition which doesnt show up after I submit).
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is("post")) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
if (!$this->request->data) {
$this->request->data = $order;
}
The input fields are correctly pre filled, but now the form doesn't even post.
Does anybody know whats wrong?
Thanks!
Update your controller code to:
$order = $this->Order->read(null,$id);
$this->set("order",$order);
if ($this->request->is('post') || $this->request->is('put')) {
if($this->Order->save($this->request->data)) {
$this->Session->setFlash("Sales Agent Updated");
}
}
Now put a debug after the reques->is('post') condition which will show what you need to show.
Im using laravel 4.0 im tyring to display a layout only if a variable ==0 (just in case a user tries to navigate to the url instead of clicking through) (i know I can redirect instead of extending but this is undesirable for now)
I am trying to get the layout to only extend when the user navigates to the page manually, noajax is set to true if their is no ajax request being sent when it goes to the function, so if the user where to navigate to the url manually it will still display the page but extend the layout.
#if ($noajax==1)
#extends('layouts.master')
#endif
#section('content')
//controller
public function test($id,$model)
{
if (Request::ajax())
{
//$foreign_key and $model must be <> null
if ($id == null || $model == null) {
$this->render('../Errors/missing_arg', 'error');
return;
}
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$noajax=0;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
else{
if($model=="ArtObj")
{
$partable = "art_objects";
$path='img/art-objects/';
}
$parid=$id;
$mediaimgs = Media::where('parent_id' , $id )->where('parent_table', $partable)->paginate(15);
$response = Response::Json($mediaimgs);
$noajax = 1;
return View::make('/Admin/manageimage/manage_image',compact('parid','mediaimgs','model','path','noajax'));
}
}
In this case you should use 2 views in controller.
In controller you should use:
if ($noajax) {
return View::make('noajax');
}
else {
return View::make('ajax');
}
In noajax view you can extend from any other view and if noajax and ajax have common code, you should put it in separate file and use #include in those both views to include common part of code.
I am trying to send the id data from a button back to one of my controllers on button click. Depending on the id sent, I want the controller function to redirect the user to different views.
Button:
<a href="/oauth/facebook" id="{{$artist->id}}" class="sign">
/oauth/facebook route:
Route::get('oauth/{provider}', 'Oauth2Controller#action_session')->before('guest');
Function action_session in Oauth2Controller:
public function action_session($provider) {
$id=Input::get('id');
if($id > 5) {
return Redirect::to('/fans');
}
else {
return Redirect::to('/artists');
}
}
I tried using ajax but it seems that the oauth/facebook route is called on button click first, before the ajax request can go through (so the $id field is blank when the controller runs). Is there any way to do this? Thank you.
You won't find the id in the input using this approach, but you could extend the route with an optional (unless you want it for all providers) parameter like this:
Route
Route::get('oauth/{provider}/{id?}', 'Oauth2Controller#action_session')->before('guest');
Controller
public function action_session($provider, $id = null) {
if ($id > 5) {
return Redirect::to('fans');
} else {
return Redirect::to('artists');
}
}
Button
...
I have set category page my default home page.But now i want to show CMS(Home page) for not logged in user while for logged-in i want to show category page.Means how i can set logic that can load both cms pages. How can i do that ? Thanks for any help in advance
<?php if(!Mage::getSingleton('customer/session')->isLoggedIn()): ?>
load->category page
<?php else: ?>//If user is NOT logged in
Load home page default one
<?php endif; ?>
IF you want only to check condition on home page .You can do like this .Change little bit code for CMS controller like this :
class Mage_Cms_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction($coreRoute = null)
{
if(Mage::getSingleton('customer/session')->isLoggedIn())
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('catalog/category/view/id/3'));
}else{
$pageId = Mage::getStoreConfig(Mage_Cms_Helper_Page::XML_PATH_HOME_PAGE);
if (!Mage::helper('cms/page')->renderPage($this, $pageId)) {
$this->_forward('defaultIndex');
}
}
}
Try and inform me about results.Hope this will solve your issue
This might work in your case :
// condition depending on login status of user
if(!$this->helper('customer')->isLoggedIn())
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl());
die();
}
else
{
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('YOUR_CATEGORY_URL'));
die();
}
IF above doesn't work, try this :
$sessionCustomer = Mage::getSingleton("customer/session");
if($sessionCustomer->isLoggedIn()) {
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('YOUR_CATEGORY_URL'));
} else {
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl());
}
I am trying to submit a EDIT form which edits Users Academics Details,
These Details have unique id in DB and my Code in Short Looks like below :
class edit extends ci_controller
{
function user_academics($id = NULL)
{
if(isset($id) == FALSE) //if link is ./edit/user_academics
{
$id = NULL;
$link = site_url('profile');
show_error("Invalid Page Request! <a href='$link' Go to Profile </a>");
}
$user_id = $this->session->userdata('user_id');
$data['fill'] = $this->edit_model->get_user_academics($id);
if($user_id != $data['fill']['user_id']) // check if logged in user is accessing his record or others
{
$link = site_url('profile');
show_error("This is an Invalid Request ! <a href='$link'>Go to Profile </a>");
}
else // actual work starts here
{
$this->session->set_flashdata('ua_id',$id); // update_academics will get this data
$this->load->view('edit/edit_3_view',$data);
}
}
function update_academics()
{
$ua_id = $this->session->flashdata('ua_id'); // flash data used here .
if( !$ua_id )
{
show_error('Sorry, This request is not valid!');
}
$academics = array(
// All post values
);
$this->edit_model->update_user_academics($academics,$ua_id);
//print_r($academics);
redirect('profile');
}
}
Now the problem is
- If I open two different records to edit, then It will set only one Session Flash value.
- And No matter what I edit , the existing values of the last flash value gets updated.
Please Suggest me another way or Correct me if I am wrong in above code . Thanks
save that flashdata in array, like:
$myArr = array('value 1', 'value 1');
//set it
$this->session->set_flashdata('some_name', $myArr);
And in view:
$dataArrs = $this->session->flashdata('some_name');
//loop thru $dataArrs to show the flashdata
Flash data is simply like variable which is available only in next request, you can bypass this behavior by using two different keys with record id in it, so that when you use flash data for showing message you can access key with particular record id.