We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4753
    • 31 Posts
    Is it possible to create a TV to select a document from the treeview? -> to create a internal link. Would be much more nice in combination with the default URL TV.

    So: What do You say?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      That might be possible; it’s a javascript function; if you look in manager/actions/mutate_content.dynamic.php you’ll see where I borrowed from the parent selection and added it to the weblink field for weblinks...
      <img name="llock" src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/tree/folder.gif" width="18" height="18" onclick="enableLinkSelection(!allowLinkSelection);" style="cursor:pointer;" />

      and the javascript functions to implement this
      var allowLinkSelection = false;
      
      function enableLinkSelection(b){
        parent.tree.ca = "link"; // set the value for the Tree frame to decide what to do when clicked
          var closed = "media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/tree/folder.gif";
          var opened = "media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/tree/folderopen.gif";
          if(b) {
              document.images["llock"].src = opened;
              allowLinkSelection = true;
          }
          else {
              document.images["llock"].src = closed;
              allowLinkSelection = false;
          }
      }
      
      function setLink(lId) { // gets called from the Tree frame when a document is clicked
          if (!allowLinkSelection) {
              window.location.href="index.php?a=3&id="+lId; // open document for view as normal
              return;
          }
          else {  
                  documentDirty=true;
                  document.mutate.ta.value=lId; // put the document ID in the field named ta of the form named mutate
          }
      }
      

      Here’s the code in manager/frames/tree.php that does this
              if(ca=="link") {
                  try {
                      parent.main.setLink(id);
                  } catch(oException) {
                      alert('<?php echo $_lang['unable_set_link']; ?>');
                  }
              }
      
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 4753
        • 31 Posts
        Here is what I’ve done by now. The Problem now is, to get the Document ID in the <input>-Element

        file: tmplvars.inc.php
        case "url": // handles url input fields
        $urls= array(''=>'--', 'http://'=>'http://', 'https://'=>'https://', 'ftp://'=>'ftp://', 'mailto:'=>'mailto:');
        $field_html = '<script type="text/javascript">
        function setTVLink(lId) { // gets called from the Tree frame when a document is clicked
        	if (!allowLinkSelection) {
        		window.location.href="index.php?a=3&id="+lId; // open document for view as normal
        		return;
        	}
        	else {  
        		documentDirty=true;
        		document.mutate.tv'.$field_name.'.value=lId; // put the document ID in the field named ta of the form named mutate
        	}
        }				
        </script>';
        
        $field_html .='<table border="0" cellspacing="0" cellpadding="0"><tr><td><select id="tv'.$field_name.'_prefix" name="tv'.$field_name.'_prefix" onchange="documentDirty=true; setVariableModified(\''.$field_name.'\');">';
        foreach($urls as $k => $v){
        	if(strpos($field_value,$v)===false) $field_html.='<option value="'.$v.'">'.$k.'</option>';
        	else{
        		$field_value = str_replace($v,"",$field_value);
        		$field_html.='<option value="$v" selected="selected">'.$k.'</option>';
        	}
        }
        $field_html .='</select></td><td>';
        $field_html .=  '<input type="text" id="tv'.$field_name.'" name="tv'.$field_name.'" value="'.htmlspecialchars($field_value).'" width="100" '.$field_style.' onchange="documentDirty=true;setVariableModified(\''.$field_name.'\');" /> <img name="llock" src="media/style/'. ($manager_theme ? "$manager_theme/" : "") .'images/tree/folder.gif" width="18" height="18" onclick="enableLinkSelection(!allowLinkSelection);" style="cursor:pointer;" /> </td></tr></table>';
        break;
        


        file: tree.php
        if(ca=="link") {
            parent.main.setTVLink(id);
            try {
                parent.main.setLink(id);
            } catch(oException) {
                alert('<?php echo $_lang['unable_set_link']; ?>');
            }
        }
        


        Now i can select the Document but i can’t save the Value... its seems because it’s numeric...
          • 4753
          • 31 Posts
          Next Problem solved!

          File: save_content.processor.php
          $tmplvar = $_POST["tv".$row['name']];
          if($_POST["tv".$row['name'].'_prefix']!='--') {
          	$tmplvar = str_replace(array("ftp://","http://"),"",$tmplvar);
          	$tmplvar = $_POST["tv".$row['name'].'_prefix'].$tmplvar;            	
          }
          elseif (is_numeric($_POST["tv".$row['name']]))
          {
          	$tmplvar = (string)$_POST["tv".$row['name']];
          }
          


          Still the problem to get a working URL as output...