We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: brettdusek at Jun 14, 2009, 07:06 PM

    I will try not to be harsh, as I am generally just very discouraged. The main problem I have is that people providing new functionality and content are severely holding back the potential of MODx by not being (to the minimum) granular with their installation instructions. I have spent more time looking for alternative installation instructions for a number of great functionality such as yours, than actually working with the functionality and modifying it to our needs.

    So this is what I would suggest to you and any others who would read this. Please, provide step-by-step instructions to install your functionality so that there is little chance someone could do it wrong.

    This is a never-ending problem for almost all open-source software and I’ve lost count of the times I’ve been frustrated enough to strangle someone. Your points are legitimate and the advice is good.

    That said, getting angry and/or ranting doesn’t help that much. The fact is that writing good documentation is a skill that not everyone has and ranting at people who are providing you with absolutely free software produced with great effort in their spare time (some not writing in their native language) is counterproductive at best. Many excellent programmers lack the ability to imagine where people can go wrong and write instructions that will head them off. It takes years of giving instructions to people who often screw up to get good at this. Being an elementary-school teacher would be great training. wink

    We try our best to keep the tone positive around here because we’re all volunteers trying to make MODx not only good, but a pleasure to use. When I create add-ons for MODx, I try to make the docs bulletproof and I always add a line about letting me know if you think of a way to improve them.

    It would be great if you write improved docs for MODx add-ons and make them available to others. You might consider putting them on the MODx Wiki where they’ll be easier to find (or at least a link to them).

    Thanks for contributing your opinions. Welcome to MODx -- We look forward to your future output? smiley
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 5340
      • 1,624 Posts
      Hi guys,

      Here’s the new version for automaticTpl.
      I updated the installation instructions based on brettdusek comments.
      I also added a "Testing" and "Advanced Usage" section.

      Please review the code bellow and let me know if you find any problems with it.

      Thx

       /* automaticTpl - Documents are automatically created with the 
       * template specified in the parent document. You can specify 
       * the default template for children, grand children, grand grand 
       * children documents and more.
       *
       * Childrens will inherit the template specified in the automaticTpl 
       * TV of the parent. If no valid Template ID is found Inherit 
       * Template Plugin will kick in(if installed)
       *
       * Installation:
       *
       * 1) Create a new Template Variable with the following information:
       *   a) Variable Name: automaticTpl
       *   b) Caption: Eg: Comma separated list of template ids
       *   c) Description: Eg: Children will be created using the 
       *      specified template IDs. Each template Id represents a level
       *   d) Input Type: Text
       *   e) Input Option Values: Empty
       *   f) Default Value: Empty
       *   g) Widget: Don't change, leave blank.
       *   h) Sort Order: Don't change unless desired.
       *   i) Template Access: Select which templates should use 
       *      this TV. The children of the documents using this template 
       *      will be created with the template specified in step 1.b.
       *  j) Access Permissions: It's up to you what users will have 
       *     access to change this TV
       *
       * 3) Take note of the id of the automaticTpl Template Variable 
       *    from either the Template Variable Resource List or from 
       *    your database table modx_site_tmplvars.
       *
       * 4) Create a new plugin with the following information:
       *  a) Plugin Name: automaticTpl
       *  b) Description: Sets the default template when a children is created.
       *  c) Plugin Disabled: Leave Unchecked
       *  d) Plugin code (php): Copy & paste the code from the 
       *     automaticTpl.php file that you downloaded.
       *
       * 5) Go into the System Events tab and find OnDocFormPrerender 
       *    and check it on.
       * 6) Go into the Configuration tab and paste the following 
       *    code: &automaticTplTvID=automaticTpl TV ID;text;
       * 7) Click anywhere outside the textbox, this will dynamically 
       *    generate a new parameter field.
       * 8) Input the ID of the Template Variable you took note of before.
       * 9) Save the plugin
       * 10) Go back to the main Plugins Resource List page and click 
       *     on 'Edit Plugin Execution Order by Event'. Ensure that 
       *     'automaticTpl' is sorted  BELOW 'Inherit Parent Template'. 
       *     If not, change it to be so.
       *  
       * Testing:
       * 1) Edit a document that has a template with 
       *    the TV 'automaticTpl' asociated to it
       * 2) Inside automaticTpl TV insert the id of the template you 
       *    want you children to be created by default Eg: 4
       * 3) If you need to assign the default template for children 
       *    on more levels put a comma separated list Eg: 4,6,29
       * 4) Save the document
       * 5) Create a children for the above document. The "Uses template" 
       *    field should be populated by default with the name of the 
       *    template Id you entered. In our example: 4
       * 6) Save your document. If you entered a comma separated list 
       *    GO TO 7. Otherwise you're done
       * 7) Create a children in for the children document created at step 5.
       * 8) Create a children for the children document created at 5. 
       *    The "Uses template" field should be populated by default 
       *    with the name of the second template Id you entered. 
       *    In our example: 6
       * 9) And so on if you entered a longer list of IDs
       *
       *
       * Advanced Usage:
       * Refer to the example above.
       * If the template id 6 has an automaticTpl TV with the value of 
       * 30 the children of the document using this template will be 
       * created using the template id 30 not 29.
       * The plugin goes up the tree until it finds the first not 
       * empty automaticTpl. This way you can have more flexibility for 
       * multiple levels of documents
       *
       *
       * Version 0.5
       * 0.5 - updated install instruction(thx to brettdusek)
       * 0.4 - removed php tags and spell check
       * 0.3 - completely changed the logic. You can use the automaticTpl 
       *       TV for any container
       *       Previous version was limited to a certain container 
       *       structure. Now other containers with different structures 
       *       can be placed anywhere in the parent container
       *       The used template will be taken from the first parent that 
       *       has a value for the automaticTpl TV. If no automaticTpl TV 
       *       is found then the parent template is used
       * 0.1 - first version
       *
       */
      global $content;
      $e = &$modx->Event;
      
      switch($e->name) {
        case 'OnDocFormPrerender':
          if(($_REQUEST['pid'] > 0) && ($id == 0)) {
      			//init parent
      			$parent = $_REQUEST['pid'];
      			//init level
      			$level = 0;
      			//init the TV value
      			$tplVal = ""; 
      			
      			//find the first parent that has an automaticTpl TV and on which level
      			while($parent){
      				//increase level
      				$level++;
      				
      				//check if the parent has the automaticTpl TV
      				$sql = "SELECT * FROM ".$modx->db->config['table_prefix']."site_tmplvar_contentvalues WHERE tmplvarid = " . $automaticTplTvID . " AND contentid = " . $parent;
      				$rs= $modx->dbQuery($sql);
      				$row = $modx->fetchRow($rs);
      				
      				if($row && $row['value'] != ''){
      					//stop while loop
      					$parent = 0;
      					//save TV value
      					$tplVal = $row['value'];
      				}else{
      					//get next parent
      					$sql = "SELECT parent FROM ".$modx->db->config['table_prefix']."site_content WHERE id = " . $parent;
      					$rs= $modx->dbQuery($sql);
      					$row = $modx->fetchRow($rs);
      					
      					$parent = $row['parent'];
      				}
      			}//end While
      			
      			if($tplVal){
      				//set template to the automaticTpl value
      				$aTplVals = explode(",",$tplVal);
      				if(isset($aTplVals[$level-1])){
      					$content['template'] = intval($aTplVals[$level-1]);
      				}else{
      					//do nothing - Inherit Template Plugin will kick in
      				}
      			}else{
      				//do nothing - Inherit Template Plugin will kick in
      			}
      		}
      	break;
      
        default:
          return;
          break;
      }