Jump to content

Image save issue in custom page edit process module


simon
 Share

Recommended Posts

Hi guys,

I created a custom ProcessModule to manage children of an admin page.
The children are always in hidden state and have a template assigned.

I can render the page edit form by template and use ajax upload for images.
After submitting the form I can see that all images are stored in the assets folder but they aren't saved in the database.
All the other inputfields (text, textarea, checkbox...) were saved correctly.

Could you please help me?

Here is the code is use for the form:

public function ___executeEdit() {

	$page = $this->pages->get($this->input->get->id);

	if($this->config->ajax && (isset($_SERVER['HTTP_X_FIELDNAME']) || count($_POST))) {
		$this->ajaxSave($page);
		return '';
	}

	$this->headline(__('Edit'));
	$this->breadcrumb('../', $this->page->title); 

	$form = $this->modules->get("InputfieldForm");
	
	$form->attr('id', 'PageIDIndicator');
	$form->attr('action', './?id='.$page->id);
	$form->attr('method', 'post'); 
	$form->attr('enctype', 'multipart/form-data'); 
	$form->attr('autocomplete', 'off');
	$form->attr('data-uploading', $this->_('Are you sure? An upload is currently in progress and it may be lost if you proceed.'));

	// fields
	$inputfields = $page->getInputfields();
	$form->append($inputfields);		

	// save
	$field = $this->modules->get("InputfieldButton");
	$field->type = 'submit';
	$field->name = 'submit_save';
	$field->value = $this->_("Save");
	$form->add($field);

	// delete
	$field = $this->modules->get("InputfieldButton");
	$field->type = 'submit';
	$field->name = 'submit_delete';
	$field->value = $this->_("Delete");
	$form->add($field);


	if($this->input->post->submit_save) {
		
		$form->processInput($this->input->post);
		$errors = $form->getErrors();

		if(count($errors)) {
			
			// show errors
			return $form->render();

		}
		else {
			
			// save values
			foreach($page->fields as $field) {
				$fieldName = $field->name;
				$page->$fieldName = $this->input->post->$fieldName;
			}

			$page->save();
			$this->session->redirect($this->page->url);

		}
	
	}
	else if($this->input->post->submit_delete) {
		
		$page->delete();
		$this->session->redirect($this->page->url);
		return true;

	}
	else {
		return $form->render();
	}
}

 

Link to comment
Share on other sites

Thanks kixe, unfortunately there is no ajax upload in the example.

As I mentioned before the ajax upload is already working. The files are stored successfully in the page related assets folder.
But how do I receive the filename(s) of the uploaded images after submitting?
 

 

Link to comment
Share on other sites

The ajaxsave method is copied from ProcessPageEdit.module
 

/**
	 * Save only the fields posted via ajax
	 *
	 * - Field name must be included in server header HTTP_X_FIELDNAME or directly in the POST vars.
	 * - Note that fields that would be not present in POST vars (like a checkbox) are only supported
	 *   by the HTTP_X_FIELDNAME version.
	 * - Works for custom fields only at present.
	 *
	 * @param Page $page
	 * @throws WireException
	 *
	 */
	protected function ___ajaxSave(Page $page) {

		if($this->config->demo) throw new WireException("Ajax save is disabled in demo mode");
		if($page->hasStatus(Page::statusLocked)) throw new WireException($this->noticeLocked);
		if(!$this->ajaxEditable($page)) throw new WirePermissionException($this->noticeNoAccess);
		$this->session->CSRF->validate(); // throws exception when invalid

		$form = $this->wire(new InputfieldWrapper());
		$form->useDependencies = false;
		$keys = array();

		if(isset($_SERVER['HTTP_X_FIELDNAME'])) {
			$keys[] = $this->sanitizer->fieldName($_SERVER['HTTP_X_FIELDNAME']);

		} else {
			foreach($this->input->post as $key => $unused) {
				if($key == 'id') continue;
				$keys[] = $this->sanitizer->fieldName($key);
			}
		}

		foreach($keys as $key) {

			if(!$field = $page->template->fieldgroup->getFieldContext($key)) continue;
			if(!$this->ajaxEditable($page, $key)) continue;
			if(!$inputfield = $field->getInputfield($page)) continue;

			$inputfield->showIf = ''; // cancel showIf dependencies since other fields may not be present
			$inputfield->name = $key;
			$inputfield->value = $page->get($key);
			$form->add($inputfield);
		}

		$form->processInput($this->input->post);
		$page->setTrackChanges(true);
		$numFields = 0;
		$lastFieldName = null;
		$languages = $this->wire('languages');

		foreach($form->children() as $inputfield) {
			$name = $inputfield->name;
			if($languages && $inputfield->getSetting('useLanguages')) {
				$v = $page->get($name);
				if(is_object($v)) {
					$v->setFromInputfield($inputfield);
					$page->set($name, $v);
					$page->trackChange($name);
				} else {
					$page->set($name, $inputfield->value);
				}
			} else {
				$page->set($name, $inputfield->value);
			}
			$numFields++;
			$lastFieldName = $inputfield->name;
		}

		if($page->isChanged()) {
			if($numFields === 1) {
				$page->save((string)$lastFieldName);
				$this->message("AJAX Saved page '{$page->id}' field '$lastFieldName'");
			} else {
				$page->save();
				$this->message("AJAX Saved page '{$page->id}' multiple fields");
			}
		} else {
			$this->message("AJAX Page not saved (no changes)");
		}
	}

 

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...