Jump to content

Recommended Posts

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 :)

  • Like 2

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
    • By louisstephens
      So I reread my first draft, and it made absolutely no sense (I deleted it to hopefully better explain myself).  I am trying to make a system (that to me is a bit complicated) utilizing jquery and processwire together. My whole goal is to put a url like https://domain.com/launch?first_name=jim&occupation=builder in a script tag on another site(just a localhost .php page) to then pull out the data for that person and append to divs etc. Basically, the initial script tag would point to "launch" which has a content-type of "application/javascript". Using jquery, I would pull out the persons name and occupation and then make a specific ajax get request to "domain.com/api" (in json format) for a look up of the person. Essentially then I could pull that particular person's information from the json data, and do with it how I please in the "launch" page.  In processwire, I have a page structure like:
      People -Jim Bob (template: person ) --Occupations (template: basic-page) ---Builder (template: occupation) ---Greeter (template: occupation) It is really just a bunch of people with their occupations and a few fields to the occupation template. With the "api" (template: api) url, I was hoping to return all the data (of people) in json format like:
      Example Format:
      { "id": 1, "title": "Jim Bob", "occupations": { "builder": { "id": 44, "title": "Builder", "years_worked": 1, "etc": "ect", }, "Greeter": { "id": 44, "title": "Greeter", "years_worked": 1, "etc": "ect", }, } } Where I get lost is really outputting the page names and nesting in the occupations into json. I have used Pages2JSON before, but I was a bit lost on how to implement what i was thinking.
       
      I have access to all the local host files, but I was hoping to kind of build out a "system" where I could place the script tag/parameters in any project, and be able to interact with the data without doing an ajax call on the actual site. In a way, this would keep processwire handling all the data and requests, and my other "projects" just with a simple script tag. This might all be way too much/over complicated, but I couldn't quite wrap my head around how to achieve it. 
    • By sambadave
      Hi everyone, here's the problem I'm trying to solve.
      I have a config area in my PW admin that is locked down for admin use. I use pages to store a bunch of settings that I'll use for my clients website. It's mostly used for visual things like colours and theming. This list could be tiny or large, depending on the sites requirements, but its great because I can store any information I want to. So the page tree could look something like this:
      ADMIN SETUP
      Home Config Aesthetics Colours Red Field: Custom Label - "Red" Field: Class name - "theme--red' Green Field: Custom Label - "Green" Field: Class name - "theme--green' Blue Field: Custom Label - "Blue" Field: Class name - "theme--blue' etc... Sizes Small Field: Custom Label - "Small" Field: Class name - "sm' Medium Field: Custom Label - "Medium" Field: Class name - "md Large Field: Custom Label - "Medium" Field: Class name - "lg" etc... Icons Target Field: Custom Label - "Hands shaking Icon" Field: Icon SVG - "[svg code]' Target Field: Custom Label - "Target icon" Field: Icon SVG - "[svg code]' Success Field: Custom Label - "Happy face icon" Field: Icon SVG - "[svg code]' etc... HOW I USE THIS
      I'm then able to set up page reference fields for colour, size and icons. I'll use these fields on particular pages so that my clients can select a particular colour, size or icon, or anything really. Currently, with the page reference field I can create a custom label for the options. So for something like colour I can label the field "Theme" and present a list of colours like "Red, "Green" and "Blue" using the custom page label label format of the colours page.
      This of course means that I can use this to do some lovely presentation on the front end of the site. With a colour selected I'll then be able to use the page reference to get the class name for that colour theme so that I can update the page's look and feel. So in my markup I'd end up with "theme--red" or "theme--green" etc.
      THE PROBLEM
      I have this all working which is great and it's really flexible for the client which they love. However, I'd love to be able to make things more visual for the client.
      Is there a way to be able to output more than just text in the page reference field? I might have a bunch of different blue colours, so instead of a list like so:
      Navy Blue Deep Blue Bright Blue Sea Blue ... it would be great to be able to output actual colour swatches, which is a lot mor visual for the client.
      Taking the icon selector, I would ideally like to show the actual svg that I've stored against that icon as a selectable image instead of seeing text options like:
      Hands shaking icon Target icon Happy face icon Just a couple of scenarios here, but as you can see there could be any number of reasons to display a more visual method of selection.
      ANY SOLUTIONS?
      I've looked at modules like FieldtypeColorPicker which could help in solving the colour issue, but it doesn't allow me to select a colour and then use a particular class name assigned to it the way I describe above.
      Considering the other use cases I mention above, does anyone know if anything exists already that would help me to create custom presentaion for page reference lists, or if there's anything planned?
      Thank you in advance for anyone who's read this far and has any words of wisdom!
      Dave
       
    • By gebeer
      Hello,
      I have a user page template with many fields that are organised in tabs. Tabs do not work on the profile edit screen. So I had to find a way how to let users edit their page with tabs in place.
      The way I solved this is having users edit their user page in the backend instead of their profile page. So basically they are on a page edit screen and not on their profile edit screen which is a different process. The drawback of this method is that the users edit their profile on a URL like .../youradminurl/access/users/edit/?id=1377. So I needed to make sure that users cannot edit other users' profiles by just switching out the id. I did this through hooks that redirect them to their own profile.
      This is far from a perfect solution. Ideally I would like to mask the page edit url to something like /myprofile but haven't found a way on how to do this, yet.
      How would you go about this?
      redirect rules in .htaccess hooking into the profile edit process? Either way I couldn't figure out how to accomplish it. Any pointers towards a solution would be very much appreciated.
    • By Thomas Diroll
      Hi guys I'm relatively new to PW and just finished developing a page for a client. I was able to include all necessary functionality using the core fieldtypes but now I it seems that I need to extend them with a custom one. What I need is a simple button, that copies the absolute url (frontend not PW-backend) of the page which is currently edited to the clipboard. As this feature is only needed inside a specific template, I tend to use a custom fieldtype which provides this feature. I've been looking inside the core modules code (eg. FieldtypeCheckbox.module) but I don't really get the structure of it and how its rendered to the admin page. I also didn't find a lot of tutorials covering custom fieldtypes.
      Maybe some of you could give me some tips on how to write a basic custom fieldtype that renders a button which copies the value of
      page->httpUrl() to the clipboard using JS. Thanks!
×
×
  • Create New...