Search the Community
Showing results for tags 'ChildCreator'.
-
Based on my Previous thread: https://processwire.com/talk/topic/7194-templatelist-inside-module-configuration/ here is an automatic child creater if you need on creation of a parent element a bunch of Child Elements. what also works: multiple diffrent childs on same parent caution: sadly what also works: first entry: parent_id = 47 child_id = 13 child_name = something child_numbers = 1 second entry: parent_id = 13 child_id = 47 ... ... this should afaik (not tested) kill your System by having a infinite Loop. because (first entry creates a child which call second entry which would then call first entry and so on). <?php /** * Created by PhpStorm. * User: Kielbasa * Date: 04.08.14 * Time: 10:09 */ class CustomChildCreater extends Process { public static function getModuleInfo() { return array( 'title' => 'CustomChildCreater', 'version' => 100, 'summary' => 'Child creator for Pages with Specific Parent', 'href' => '', 'singular' => true, 'autoload' => true, ); } public function ___execute() { $this->setFuel('processHeadline', 'Child Creater'); $table = $this->modules->get("MarkupAdminDataTable"); $table->setEncodeEntities(false); $table->headerRow(array('Edit','Parent Template', 'Child Template','Name of new Childs (childname+{n})', '{n} - Childs', 'Delete')); $result = $this->db->query("SELECT * FROM {$this->className} ORDER BY parent_template"); while($row = $result->fetch_assoc()) { // output in table rows with edit link and delete checkbox? $table->row(array( 'edit' => 'edit/?id='.$row['id'], $this->templates->get($row['parent_template'])->name , $this->templates->get($row['child_template'])->name, $row['child_name'], $row['child_numbers'], "<input type='checkbox' name='delete[]' value='$row[id]' />" )); } $button = $this->modules->get("InputfieldButton"); $button->type = 'submit'; $button->id = 'submit_delete'; $button->value = 'Remove selected sources'; $table->action(array('Add source' => 'add/')); // Is there clean way to add button to right side? return "<form action='./delete/' method='post'>" .$table->render() . $button->render() . "</form>"; } public function ___executeAdd() { $this->fuel->breadcrumbs->add(new Breadcrumb('../', 'Child Creater')); $this->setFuel('processHeadline', "new Instance"); $form = $this->modules->get("InputfieldForm"); $form->method = 'post'; $form->action = '../save/'; //--Parent Template $field = wire('modules')->get('InputfieldSelect'); $field->attr('name', 'parent_template'); $field->label = __('Parent Template'); foreach(wire('templates') as $template){ $field->addOption($template->id, $template->name); } $form->add($field); //--Child Template $field = wire('modules')->get('InputfieldSelect'); $field->attr('name', 'child_template'); $field->label = __('Child Template'); foreach(wire('templates') as $template){ $field->addOption($template->id, $template->name); } $form->add($field); //--Child Name $field = wire('modules')->get('InputfieldText'); $field->attr('name', 'child_name'); $field->label = __('Child Name'); $form->add($field); //--Children to Create $field = wire('modules')->get('InputfieldInteger'); $field->attr('name', 'child_number'); $field->label = __('Childs to Create'); $form->add($field); $field = $this->modules->get("InputfieldButton"); $field->type = 'submit'; $field->value = 'Create'; $form->add($field); return $form->render(); } public function ___executeSave() { $parent_id = (int) $this->input->post->parent_template; $child_id = (int) $this->input->post->child_template; $child_name = $this->input->post->child_name; $child_numbers = (int) $this->input->post->child_number; //no i am not allowing you to kill your system if($parent_id != $child_id){ $sql = "INSERT INTO {$this->className} SET parent_template = '$parent_id', child_template = '$child_id', child_name = '$child_name', child_numbers = '$child_numbers' ON DUPLICATE KEY UPDATE id = id;"; $this->db->query($sql); $this->message("Save Successful"); $this->session->redirect("../"); } $this->message("Parent and Child should not be the same!"); $this->session->redirect("../"); } public function ___executeEdit(){ if(!isset($_GET['id'])){ $this->session->redirect("../"); } $id = (int) $_GET['id']; $result = $this->db->query("SELECT * FROM {$this->className} WHERE id=$id"); $data = $result->fetch_array(); $form = $this->modules->get("InputfieldForm"); $form->method = 'post'; $form->action = '../update/'; $field = $this->modules->get("InputfieldHidden"); $field->name = 'id'; $field->value = $id; $form->add($field); //--Parent Template $field = wire('modules')->get('InputfieldSelect'); $field->attr('name', 'parent_template'); $field->label = __('Parent Template'); foreach(wire('templates') as $template){ $field->addOption($template->id, $template->name); } $field->value = $data['parent_template']; $form->add($field); //--Child Template $field = wire('modules')->get('InputfieldSelect'); $field->attr('name', 'child_template'); $field->label = __('Child Template'); foreach(wire('templates') as $template){ $field->addOption($template->id, $template->name); } $field->value = $data['child_template']; $form->add($field); //--Child Name $field = wire('modules')->get('InputfieldText'); $field->attr('name', 'child_name'); $field->label = __('Child Name'); $field->value = $data['child_name']; $form->add($field); //--Children to Create $field = wire('modules')->get('InputfieldInteger'); $field->attr('name', 'child_number'); $field->value = $data['child_numbers']; $field->label = __('Childs to Create'); $form->add($field); $field = $this->modules->get("InputfieldButton"); $field->type = 'submit'; $field->value = 'Save'; $form->add($field); return $form->render(); } public function ___executeUpdate(){ $this->fuel->breadcrumbs->add(new Breadcrumb('../', 'Data imports')); $id = $this->input->post->id; $parent_id = (int) $this->input->post->parent_template; $child_id = (int) $this->input->post->child_template; $child_name = $this->input->post->child_name; $child_numbers = (int) $this->input->post->child_number; //no i am not allowing you to kill your system if($parent_id != $child_id){ $this->db->query("UPDATE {$this->className} SET parent_template = '$parent_id', child_template = '$child_id', child_name = '$child_name', child_numbers = '$child_numbers' WHERE id=$id"); $this->message("Update Successful"); $this->session->redirect("../"); } $this->message("Parent and Child should not be the same!"); $this->session->redirect("../"); } public function ___executeDelete() { $count = 0; if(!is_array($this->input->post->delete) || empty($this->input->post->delete)) { $this->message("Nothing to delete"); $this->session->redirect("../"); // back to list } foreach($this->input->post->delete as $id) { $id = (int) $id; $this->db->query("DELETE FROM {$this->className} WHERE id=$id"); $count++; } $this->message("Deleted " . $count . " data sources"); $this->session->redirect("../"); // back to list } public function ___install() { parent::___install(); $p = new Page(); $p->template = $this->templates->get("admin"); $p->parent = $this->pages->get("template=admin, name=setup"); $p->title = 'Child Creater'; $p->name = 'child-creater'; $p->process = $this; $p->save(); $sql = <<< _END CREATE TABLE {$this->className} ( id int unsigned NOT NULL auto_increment, parent_template int unsigned NOT NULL, child_template int unsigned NOT NULL, child_name varchar(255) NOT NULL, child_numbers int unsigned NOT NULL, PRIMARY KEY(id) ) ENGINE = MYISAM; _END; $this->db->query($sql); } public function ___uninstall() { $p = $this->pages->get('template=admin, name=child-creater'); $p->delete(); $this->db->query("DROP TABLE {$this->className}"); } public function init() { parent::init(); // add a hook after the $pages->save, to issue a notice every time a page is saved $this->pages->addHookAfter('added', $this, 'createChildren'); } public function createChildren($event){ $page = $event->arguments[0]; $query = $this->db->query("SELECT * FROM {$this->className};"); //foreach child creater object foreach($query as $cco){ $t = $cco['parent_template']; $ct = $cco['child_template']; $cn = $cco['child_name']; $ci = (int) $cco['child_numbers']; $c = ''; //will be used as new Page if($t == $page->template->id){ if($ci > 0){ for($i=1;$i <= $ci;$i++){ $c = new Page(); $c->template = $ct; $c->parent = $page; // set the parent $c->name = $cn.$i; // give it a name used in the url for the page $c->title = $c->name; $c->gsv_tab_position = $i; $c->save(); } } } } } } Tell me your thoughts
-
- 2
-
- Custom
- ChildCreator
-
(and 5 more)
Tagged with: