We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30884
    • 142 Posts
    Hi there,

    I’ve managed to successfully implement UglyDogs Newspublisher altered to allow check box TVs but what I want to do is to have it read the existing TV and format it into a drop down menu with the options in there.

    I’ve got that far and it is able to populate the drop down menu and looks like it’s behaving normally. It’s when I hit the submit button that it breaks. It isn’t updating/creating the entry for that TV when I put it through as a drop down menu. I believe the PHP & MySQL is n’t putting the data into the TemplateVars table as I’d hoped.


    This is the submit TV check box code as it is on his untouched code.

     //tag area
                if($tvtags){
                    $rs = $modx->dbQuery("SELECT elements FROM ".$modx->getFullTableName('site_tmplvars')." WHERE name='".substr($tvtags, 2)."' LIMIT 1;");
                    if($row = $modx->fetchRow($rs, 'assoc')){
                        $default_tags = explode('||', $row['elements']);
                        if($default_tags){
                           $tag_area='';
    	                    foreach($default_tags as $tag){
    	$tag_area.= '<input  name="tags-'.$tag.'" id="tags-'.$tag.'" />'.$tag."<br />\n";
                       }
                   $formTpl = str_replace('[+tags+]',$tag_area,$formTpl);
    	                }
                    }
                }
    



    Now here’s what I’ve cobbled into his code. I’ve not altered the inject TV data routine (pasted below this) but I believe I’m probably trying to fit a square peg into a round hole...
       //tag area
                if($tvtags){
                    $rs = $modx->dbQuery("SELECT elements FROM ".$modx->getFullTableName('site_tmplvars')." WHERE name='".substr($tvtags, 2)."' LIMIT 1;");
                    if($row = $modx->fetchRow($rs, 'assoc')){
                        $default_tags = explode('||', $row['elements']);
                        if($default_tags){
                      	$tag_area='';
    			$tag_area='<select name="'.$tvtags.'">';
    	                    foreach($default_tags as $tag){
    			$tag_area.= '<option  name="tags-'.$tag.'" id="tags-'.$tag.'" />'.$tag."<br />\n";
                       }
    			$tag_area.="</select>";
    
                   $formTpl = str_replace('[+tags+]',$tag_area,$formTpl);
    	                }
                    }
    
                }


    Is there any documentation/pointers that can show how to hard code TV insertion into a snippet and any criteria/structure that they need to successfully happen? I’m needing to create a load of TVs for a lot more custom fields and I’m pretty sure that all the code I’m psuedoing just now is going to need some more hard coded TV management.

      if($tags_tvcontent){
                	$rs = $modx->dbQuery("SELECT id FROM ".$modx->getFullTableName('site_tmplvars')." WHERE name='".substr($tvtags, 2)."' LIMIT 1;");
    	            if($row = $modx->fetchRow($rs, 'assoc')){
    		            $tmplflds = array(
    		            	'tmplvarid' => $row['id'],
    		            	'contentid' => $lastInsertId,
    		            	'value' => mysql_escape_string($tags_tvcontent)
    		            );
    		            $modx->db->insert($tmplflds,$modx->getFullTableName('site_tmplvar_contentvalues'));
    		        }
    	        }


    If anyone knows where I can get some extended documentation on how to code for TVs from within a snippet I’d kiss their feet, or at the very least be indebted to them for helping me unleash the gates of creative coding to help get this site rockin.
    Cheers

    TXRX
      p i x e l p u s h e r e x t r o d i n a i r e
      A x i s A n i m a t i o n . c o m
      • 30884
      • 142 Posts
      Hi there,

      I’ve managed to resolve this problem. The fields in the form that were setting template variables for the checkbox type of TV were expecting an ’on’ attribute when being posted. They were also being treated as an array since there was the ability to choose more than one.

      I’ve altered the original newspublisher tag routines as below.

      This is the original insert routine.
                // tagging
                  $tags = array();
                  foreach($_POST as $n=>$v) {
                  	if( substr($n, 0, 5)=='tags-' && $v=='on' ){
                  		array_push($tags, substr($n, 5));
                  		unset($_POST[$n]);
                  	}
                  }
                  $tags_tvcontent = implode('||', $tags);


      Modified code:
                  // tagging
      //            $tags = array();
                  foreach($_POST as $n=>$v) {
                  	if( substr($n, 0, 5)=='tags-'){
      //            		array_push($tags, substr($n, 5));
                  		$tags=($_POST[$n]);
      					unset($_POST[$n]);
      
                  	}
                  }


      This is the original display checkbox routine:
                  //tag area
                  if($tvtags){
                      $rs = $modx->dbQuery("SELECT elements FROM ".$modx->getFullTableName('site_tmplvars')." WHERE name='".substr($tvtags, 2)."' LIMIT 1;");
                      if($row = $modx->fetchRow($rs, 'assoc')){
                          $default_tags = explode('||', $row['elements']);
                          if($default_tags){                
      			$tag_area='';
      	       foreach($default_tags as $tag){
                          	$tag_area.= '<input type="checkbox" name="tags-'.$tag.'" id="tags-'.$tag.'" />'.$tag."<br />\n";
      	                    }
                         $formTpl = str_replace('[+tags+]',$tag_area,$formTpl);
      	       }
                      }
                  }
      


      This is the drop down menu display routine:
      $tag_area='';
      $tag_area='<select name="tags-'.$tag.'" id="tags-'.$tag.'">';
      	       foreach($default_tags as $tag){
      	         $tag_area.= '<option name="tags-'.$tag.'">'.$tag;
      	                    }
      	                    $tag_area.= "</select>";
                         $formTpl = str_replace('[+tags+]',$tag_area,$formTpl);
      	       }
                      }
                  }
      


      I’ve also made the Checkbox TV itself into a drop down menu type with no widget settings either.

      I’m also experimenting with the setTV core code hack that someone posted here:
      http://modxcms.com/bugs/task/129
      and discussed here:
      http://modxcms.com/forums/index.php/topic,824.0.html

      I initially got this newspublisher hack from here:
      http://modxcms.com/forums/index.php/topic,4951.msg40296.html
      http://nanabit.net/modx/modx-newspublishertagging.html


      I can post the full updated newspublisher snippet here if anyone would rather use Drop downs over the ol’ check boxes.
      Cheers

      TXRX
        p i x e l p u s h e r e x t r o d i n a i r e
        A x i s A n i m a t i o n . c o m
        • 30884
        • 142 Posts
        Now how the heck do I get NewsManager to work with editting the Template variables...

        wink

        Ah here we go!

        http://modxcms.com/forums/index.php/topic,13270.0.html

        edit: I’ve added all the links to everything I found useful in getting this to work. I’ve not had time to actually compile a how to nor document how I got the newspublisher, newseditor, newsmanager snippets all working with custom template variable management. If someone does a search, I hope this list/topic saves a bit of time for others.
          p i x e l p u s h e r e x t r o d i n a i r e
          A x i s A n i m a t i o n . c o m