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

    Can ManagerManager move Template Variables based on their Category? I have quite a few tv’s now and it would be easier just to move or hide TV’s based on their category.

    As an example I have Code-category which doesn’t have to be visible to anyone else than admin group. Then I have Basic info category, which I would like to move to a different tab. But the category contents (TV’s) might change and I would like the tab to update according to it.

    I know there is category_to_tabs plugin which basically does this, but not quite as elegantly as MM.
      The sun always shines for tough guys.
      • 10913
      • 66 Posts
      If someone is wondering this, I made a hack. Based on move_FieldsToTab

      Usage is simple. Create a rule mm_moveCategoryToTab and insert category ID (not name!)(required),new tad (required), roles (optional) and templates (optional).

      The code gets tv:s in a category and moves them into new tab just like moveFieldsToTab does. Simple.

      To get this to work you need to modify from the mm.inc.php section
      	// check if there are any name clashes between TVs and default field names? If there is, preserve the default field
      	if (!isset($mm_fields[ $n ])) { 
      		$mm_fields[ $n ] = array('fieldtype'=>$t, 'fieldname'=>'tv'.$thisTv['id'].$fieldname_suffix, 'dbname'=>'', 'tv'=>true]);
      	}
      	
      	$mm_fields[ 'tv'.$n ] = array('fieldtype'=>$t, 'fieldname'=>'tv'.$thisTv['id'].$fieldname_suffix, 'dbname'=>'', 'tv'=>true);
      }
      

      to
      	// check if there are any name clashes between TVs and default field names? If there is, preserve the default field
      	if (!isset($mm_fields[ $n ])) { 
      		$mm_fields[ $n ] = array('fieldtype'=>$t, 'fieldname'=>'tv'.$thisTv['id'].$fieldname_suffix, 'dbname'=>'', 'tv'=>true, 'category'=>$thisTv['category']);
      	}
      	
      	$mm_fields[ 'tv'.$n ] = array('fieldtype'=>$t, 'fieldname'=>'tv'.$thisTv['id'].$fieldname_suffix, 'dbname'=>'', 'tv'=>true, 'category'=>$thisTv['category']);
      }
      

      to get the needed information about categories.

      The code itself
      //---------------------------------------------------------------------------------
      // mm_moveCatToTab
      // Move all fields belonging to a category to a different tab
      //--------------------------------------------------------------------------------- 
      function mm_moveCategoryToTab($cat, $newtab, $roles='', $templates='') {
      
      	global $modx, $mm_fields;
      	$e = &$modx->Event;
      	
      	$fields = '';
      	
      	foreach ($mm_fields	as $parent => $child) {
      		if ($child['category'] == $cat) {
      			$fields .= $parent.',';
      		}
      						
      	}
      
      	$fields = trim($fields,",");
      
      	// if we've been supplied with a string, convert it into an array 
      	$fields = makeArray($fields);
      
      	// if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
      	if (useThisRule($roles, $templates)) {
      	
      	$output = " // ----------- Move category to tab -------------- \n";
      	
      		// If it's one of the default tabs, we need to get the capitalisation right
      		switch ($newtab) {
      			case 'general':
      			case 'settings':
      			case 'access':
      			case 'meta': // version 1.0.0 only, removed in 1.0.1
      				$newtab = ucfirst($newtab);
      			break;
      		}
      		
      		// Make sure the new tab exists in the DOM
      		$output .= "if ( \$j('#tab".$newtab."').length > 0) { \n";
      		$output .= 'var ruleHtml = \'<tr style="height: 10px"><td colspan="2"><div class="split"></div></td></tr>\'; ';
      			
      		// Go through each field that has been supplied
      		foreach ($fields as $field) {
      
      			// What type is this field?
      			if (isset($mm_fields[$field])) {
      				$fieldtype = $mm_fields[$field]['fieldtype'];
      				$fieldname = $mm_fields[$field]['fieldname'];
      				$output .= '
      					var toMove = $j("'.$fieldtype.'[name='.$fieldname.']").parents("tr"); // Identify the table row to move
      				toMove.next("tr").find("td[colspan=2]").parents("tr").remove(); // Get rid of line after, if there is one
      				var movedTV = toMove.appendTo("#tab'.$newtab.'>table:first"); // Move the table row
      				movedTV.after(ruleHtml); // Insert a rule after 
      				movedTV.find("td[width]").attr("width","");  // Remove widths from label column
      				$j("[name^='.$fieldname.']:first").parents("td").removeAttr( "style" );  // This prevents an IE6/7 bug where the moved field would not be visible until you switched tabs
      				';
      			}	
      
      		} // end foreach
      		
      		$output .= "}";
      		$e->output($output . "\n");
      		
      	}	// end if
      } // end function
      


      I created a new file in the functions directory named categories.inc.php and added the above code to there.

      Notes:
      This suits my usage perfectly the way it is right now. YMMV. I use this with custom filter function (filters TV:s based on a value given in another TV).

      edit: Trimmed the base code a bit
        The sun always shines for tough guys.
        • 27330
        • 884 Posts
        this is a great idea. i will try it to see if works for me. thanks for sharing.
          • 10913
          • 66 Posts
          One problem found. This doesn’t respect the TV order. I’ll have to check this out.
            The sun always shines for tough guys.
            • 10913
            • 66 Posts
            So I made a change to the mm.inc.php query.

            Changed this
            $all_tvs = $modx->db->makeArray( $modx->db->select("name,type,id,category", $modx->db->config['table_prefix']."site_tmplvars", '', 'name ASC') );
            

            to this
            $sql = "SELECT t.name, t.type, t.id, t.category, n.rank FROM ".$modx->db->config['table_prefix']."site_tmplvars t
            	JOIN ".$modx->db->config['table_prefix']."site_tmplvar_templates n ON t.id = n.tmplvarid
            	WHERE n.templateid = '".$mm_current_page['template']."' 
            	ORDER BY rank,name ASC";
            
            $all_tvs = $modx->db->makeArray( $modx->db->query($sql) );
            

            and it fixed the listing order to follow the modified rank.
              The sun always shines for tough guys.
              • 20413
              • 2,877 Posts
              You rock!! Thanks for sharing!! cool
              Works nice on my end! smiley
                @hawproductions | http://mrhaw.com/

                Infograph: MODX Advanced Install in 7 steps:
                http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                • 30485
                • 7 Posts
                @Glen Tks for sharing

                I used this change in managermanager and use this code:

                mm_createTab(’TAREFAS’, ’todo’);
                mm_moveCategoryToTab(’13’, ’todo’);


                Now I pretend to creat a new TAB inside this TAB ’TAREFAS’ (to do) to divide the TVs for 2 tabs.... to do for ’admin’ and for ’clients’

                any ideas? tks.