Jump to content

Create & publish new page with specific filed data


Vayu Robins
 Share

Recommended Posts

Hi.

I am trying to create a button on specific admin pages, that when pressing this button, it creates and publishes a new page with some of the input data from the current page.

I have been able to add a button to the admin pages with this code in the admin.php template file:

$page->addHook('ProcessPageEdit::buildForm', null, 'addButton');
function addButton(HookEvent $event) {
	$page = $event->arguments(0);
	$href = wire()->config->urls->admin.'page/add/?parent_id=1089&credit='.$page->credit->value.'&partner='.$page->partner->value;
	$field = wire()->modules->get('InputfieldButton');
	$field->attr('id+name', 'create_new_log');
	$field->attr('class', $field->class);
	$field->attr('value', 'Create new log');
	$field->attr('href',$href);
	$event->return = $event->return->append($field);
}
require($config->paths->adminTemplates . 'controller.php');

I have attempted to add some url parameters, but this method does nothing good. Is it possible to do what I am trying to do?

Link to comment
Share on other sites

Thanks again! I used and modified your code to fit my needs and it works nicely. Here is the code I ended with:

/ In Admin area, create Refundering btn on cases.
$page->addHookAfter( 'ProcessPageEdit::buildForm', null, 'addButtons' );

/**
 *	Add a button besides "Save", which lets the user accept the application
 *
 */
function addButtons($event) {
	$page = $event->object->getPage();

	if( $page->template == "credit-log" && $page->id && !$page->isTrash() && !$page->is( Page::statusUnpublished ) && $page->credit_action->id === 1 ){

		$refunded_page = wire('pages')->find("template=credit-log, product=$page->product, partner=$page->partner, credit_action=2");
		
		if( $refunded_page->count() == 0 ) {
			$form = $event->return;
			$refund = wire()->modules->get('InputfieldSubmit');
			$refund->attr('id+name', 'create_refund');
			$refund->class .= ' ui-priority-secondary';
			$refund->attr('value', wire()->_('Refunder'));
			$event->return = $event->return->insertBefore( $refund, $form->get("id") );
		}else{
			// echo out a message that this case has been refunded.
			$form = $event->return;
			$refund = wire()->modules->get('InputfieldMarkup');
			$refund->attr('id+name', 'create_refund');
			$refund->attr('value', wire()->_('This case is now refunded!'));
			$event->return = $event->return->insertBefore( $refund, $form->get("id") );
		}
	}

	// When submitted save new page and update user.
	if( wire()->input->post->create_refund ){

		if(!$page->id){
			wire()->error("Invalid case to be refunded."); 
			return;
		}

		// Create credit log
		$credit_page = new Page(); // create new page object
		$credit_page->of( false );
		$credit_page->template = 'credit-log'; // set template
		$credit_page->parent = $page->parent; // set the parent 
		$credit_page->title = date( 'd-m-Y-H-i-s' );
		$credit_page->credit = intval( $page->credit );
		$credit_page->partner = $page->partner;
		$credit_page->product = $page->product;
		$credit_page->credit_date = date( 'd-m-Y H:i:s' );
		$credit_page->credit_action = intval( 2 );
		$credit_page->save();

		// Subtract case credit from user credit.
		$partner = $page->partner;
		$partner->of( false );
		$partner->credit = intval( $partner->credit + $page->credit );
		$partner->save();
	}
}
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...