Creating List Popups

In this example we will replace the standard link in a list with a popup.

Add to the controller head

// This property must be defined in the controller class
public $list_record_onclick = null;

public function __construct()
{
    parent::__construct();	

	// When a list item record is clicked, show the popup
	$this->list_record_onclick = "new PopupForm('index_onshow_manage_string_form', { ajaxFields: {string_id: '%s' } }); return false;";
}

Create a new method to render the popup form

protected function index_onshow_manage_string_form()
{
	try
	{
		$model_id = post('string_id', null);
		
		$this->reset_form_edit_session_key();

		$model = Cms_String::create();
		if ($model_id)
			$model = $model->find($model_id);

		$model->init_form_fields();
		
		$this->view_data['model'] = $model;
		$this->view_data['new_record_flag'] = !($model_id);
	} 
	catch (Exception $ex)
	{
		$this->handle_page_error($ex);
	}

	$this->display_partial('manage_string_form');
}

Create a partial to house the popup form content, named _manage_string_form.htm

<div>
	<h2>Update String</h2>
		<div class="form-400">
		<?=Phpr_Form::open_tag() ?>
			<? if (!isset($fatal_error)): ?>
				<div class="content">
					<? $this->form_render($model) ?>
					<?=form_hidden('string_id', $model->id)?>
				</div>
				<div class="footer">
					<?=admin_ajax_button('Save', 'index_onsave_string', array('class'=>'btn btn-success'), "
							loadIndicator: {injectInElement: true},
							onComplete: listReload,
							onSuccess: cancelPopup") ?>
					
					<?=admin_button('Cancel', array('onclick'=>"cancelPopup(); return false")) ?>

					<?=(!$new_record_flag) 
						? admin_ajax_button('Delete', 'index_ondelete_string', array('class'=>"btn btn-danger pull-right"), "
							confirm: 'Do you really want to delete this string?',
							onComplete: listReload,
							loadIndicator: {injectInElement: true},
							onSuccess: cancelPopup") 
						: '' ?>
				</div>
			<? else: ?>
				<div class="content">
					<div class="form-flash"><?=flash()?></div>
				</div>
				<div class="footer">
					<?=admin_button('Cancel', array('onclick'=>'return cancelPopup();"')) ?>
				</div>
			<? endif ?>
		</form>
	</div>
</div>

Create a new method to save the model after clicking Save on the popup

protected function index_onsave_string()
{
	try
	{
		$string_id = post('string_id');
		
		if (!$string_id)
			throw new Phpr_ApplicationException("Please select a string");
			
		$model = Cms_String::create()->find($string_id);
		if (!$model)
			throw new Phpr_ApplicationException("String not found");

		$model->save(post('Cms_String'));

		Phpr::$session->flash['success'] = sprintf('String %s updated successfully', h($model->code));
	}
	catch (Exception $ex)
	{
		Phpr::$response->ajax_report_exception($ex, true, true);
	}
}

Create a new method to delete the model after click Delete on the popup

protected function index_ondelete_string()
{
	try
	{
		$string_id = post('string_id');
		
		if (!$string_id)
			throw new Phpr_ApplicationException("Please select a string");
			
		$model = Cms_String::create()->find($string_id);
		if (!$model)
			throw new Phpr_ApplicationException("String not found");

		$model->delete();

		Phpr::$session->flash['success'] = sprintf('String %s has been deleted', h($model->code));
	}
	catch (Exception $ex)
	{
		Phpr::$response->ajax_report_exception($ex, true, true);
	}
}