Created
February 12, 2014 19:15
-
-
Save manviny/8962522 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ProcessWire 'Hello world' demonstration module | |
* | |
* Demonstrates the Module interface and how to add hooks. | |
* | |
* ProcessWire 2.x | |
* Copyright (C) 2010 by Ryan Cramer | |
* Licensed under GNU/GPL v2, see LICENSE.TXT | |
* | |
* http://www.processwire.com | |
* http://www.ryancramer.com | |
* | |
* http://processwire.com/talk/topic/4887-additional-buttons-on-page-edit/ | |
*/ | |
class PageToJson extends WireData implements Module { | |
/** | |
* getModuleInfo is a module required by all modules to tell ProcessWire about them | |
* | |
* @return array | |
* | |
*/ | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Page to Json', | |
'version' => 101, | |
'summary' => 'Converts this page to json.', | |
'href' => 'http://www.processwire.com', | |
'singular' => true, | |
'autoload' => true, | |
); | |
} | |
public function init() { | |
$this->addHook('ProcessPageEdit::buildForm', $this, 'addButton'); | |
} | |
public function addButton(HookEvent $event) { | |
// only superuser is allowed to convert to json | |
if(!wire("user")->hasRole("superuser")) return; | |
$array = array(); | |
// fields to be avoided | |
$avoid = array("FieldtypeFieldsetOpen", "FieldtypeFieldsetClose"); | |
// get actual page | |
$page = wire(pages)->get($this->input->get->id); | |
// loop through all fields | |
foreach($page->fields as $field) { | |
if (!in_array($field->type, $avoid)) { | |
$array[$field->name] = htmlentities($page->get($field->name)); | |
} | |
} | |
// convert to json | |
echo json_encode(array($page->name =>$array)); | |
// CREATE BUTTON | |
$href = $this->config->urls->admin.'page/edit/?id='.$this->input->get->id.'&e=1'; | |
$field = $this->modules->get('InputfieldButton'); | |
$field->attr('id+name', 'email_customer'); | |
$field->attr('class', $field->class); | |
$field->attr('value', 'page to json'); | |
$field->attr('href',$href); | |
$event->return = $event->return->append($field); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment