We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16289
    • 121 Posts
    Вот накидал плагин, который позволяет к каждому документу приаттачивать любое количество файлов и вклеивать код с ссылками в виде списка в конец контента страницы.
    <ul class="attached">
    	<li>...</li>
    	<li>...</li>
    	...	
    </ul>
    


    Специально для людей-менеджеров, что не умеют работать с встроенным редактором и создавать ссылки.

    
    $e = &$modx->Event;
    
    switch($e->name) 
    {
      case 'OnBeforeDocFormSave': 
       
        global $content;
    	if($_POST['attach_file'] && $_POST['attach_file'][0]<>''){
    		$attached_files='<ul class="attached">';		
    		$attach_file=$_POST['attach_file'];
    		$attach_file_name=$_POST['attach_file_name'];	   
    		
    		  foreach ($attach_file as $key=>$value)
    		   {
    			$attached_files .= "<li><a href=".$value.">".$attach_file_name[$key]."</a></li>";
    		   }
    		
    		$attached_files.='</ul>';
    		$_POST['ta'].=$attached_files; 
    	}
      break;
    
      case 'OnDocFormRender':
        global $content;
    
       
    	$output =  '<script type="text/javascript">var $j = jQuery.noConflict(); $j(document).ready(function($)
    					{
    					 attach="<div class=\"sectionHeader\">Присоединить файл</div>";					 
    					 attach +="<div class=\"sectionBody tmplvars\">";
    					 attach_field ="<p>";
    					 attach_field +="<label for=\"attach_file_browser\">Путь файла</label> ";
    					 attach_field +="<input id=\"attach_file\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file[0]\"/>";
    					 attach_field +="<input id=\"attach_file_browser\" type=\"button\" onclick=\"BrowseFileServer(\'attach_file[0]\')\" value=\"Выбрать\"/>";
    					 attach_field +=" <label for=\"attach_file_name\">Название файла</label> ";
    					 attach_field +="<input id=\"attach_file_name\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file_name[0]\"/>";
    					 attach_field +=" <a href=\"#\" id=\"add_attach_field\">Добавить поле</a> ";
    					 attach_field +="</p>";
    					 attach_end ="</div>";
    					
    					  jQuery(".sectionBody .sectionBody:eq(0)").after(attach+attach_field+attach_end);
    					  
    					  
    					  jQuery("#add_attach_field").live("click",function(e){
    																		
    							
    								number=0;
    								a=jQuery(this).parent().parent();
    								a.children().each(function(){number++;});	
    								attach_field ="<p>";
    								attach_field +="<label for=\"attach_file_browser["+number+"]\">Путь файла</label> ";
    								attach_field +="<input id=\"attach_file\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file["+number+"]\"/>";
    								attach_field +="<input id=\"attach_file_browser["+number+"]\" type=\"button\" onclick=\"BrowseFileServer(\'attach_file["+number+"]\')\" value=\"Выбрать\"/>";
    								attach_field +=" <label for=\"attach_file_name["+number+"]\">Название файла</label> ";
    								attach_field +="<input id=\"attach_file_name["+number+"]\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file_name["+number+"]\"/>"
    								attach_field +=" <a href=\"#\" id=\"add_attach_field\">Добавить поле</a> ";						
    								attach_field +="</p>";
    								
    								jQuery(this).parent().parent().append(attach_field);
    								
    								return false;});
    					            
    																										 
    					});</script>';
    	
    	
        $e->output($output);
      break;
    }
    
    


    Только как при редактировании контента с уже вставленным списком удалить элемент
    <ul class="attached">
    и сгенерировать поля с уже вставленными файлами.
    При любой манипуляции с контентом в событии OnDocFormRender пропадает визуальный редактор.
      How could number of mysql queries be more than number of tables in DB???
      • 17921
      • 101 Posts
      А можно ли этот плагин использовать, чтобы прикрепить файлы к документу, создаваемому через eForm, когда к eFormOnBeforeMailSent подключается сниппет, который это дело генерит в документ? smiley

      Как???
        samurai smiley
        • 16289
        • 121 Posts
        Можно, но только придется подключать еще FCK Editor и UploadBrowser (без них не закачаешь файлы).
        Затем через сниппет вставлять JQuery код, который создает поля.

        Пока что написал для Backend

        $e = &$modx->Event;
        
        switch($e->name) 
        {
          case 'OnBeforeDocFormSave': 
           
            global $content;	
        	
        	function fileSizeText($fileSize) {
        		if ($fileSize == 0) $returnVal = '0 bytes';
        		else if ($fileSize > 1024*1024*1024) $returnVal = (ceil($fileSize/(1024*1024*1024)*100)/100) . ' GB';
        		else if ($fileSize > 1024*1024) $returnVal = (ceil($fileSize/(1024*1024)*100)/100) . ' MB';
        		else if ($fileSize > 1024) $returnVal = (ceil($fileSize/1024*100)/100) . ' kB';
        		else $returnVal = $fileSize . ' B';
        		return $returnVal;
        	}
        
        	
        	if($_POST['attach_file'] && $_POST['attach_file'][0]<>''){
        		$attached_files='<ul class="attached">';		
        		$attach_file=$_POST['attach_file'];
        		$attach_file_name=$_POST['attach_file_name'];	   
        		
        		  foreach ($attach_file as $key=>$value)
        		   {
        			if (!empty($value))
        			{
        			if ($attach_file_name[$key]=='') $attach_file_name[$key]=preg_replace("'.*?/'", '', $value);
        			$attached_files .= '<li id="attached_file"><a href="'.$value.'">'.$attach_file_name[$key].'</a><span>('.fileSizeText(filesize($modx->config['base_path'].$value)).')</span></li>';
        			}
        		   }
        		
        		$attached_files.='</ul>';
        		$_POST['ta'].=$attached_files; 
        		$content.=$attached_files;
        	}
          break;
          
           case 'OnDocFormPrerender': 
           
            global $content;
        
        	/*echo "<pre>";
        	print_r ($content);
        	echo "</pre>";*/
        	//$content['content'].="dsf";
        
            
            preg_match_all("/<li id=\"attached_file\">.*?(href[= = ])(.*?)(>)(.*?)(<\/a>+)/sim",$content['content'],$attached);
        	
        	$content['content']= preg_replace("'<ul class=\"attached\">.*?<\/ul>'im", '', $content['content']);
        			
        	$attach_file=$attached[2];
        	$attach_file_name=$attached[4];
           
        	$output =  '<script type="text/javascript">var $j = jQuery.noConflict(); $j(document).ready(function($)
        					{
        					 jQuery("ul.attached").remove();
        					 attach="<div class=\"sectionHeader\">Присоединить файл</div>";					 
        					 attach +="<div class=\"sectionBody tmplvars\">";
        					 attach_field ="";';
        					
        					 for ($i=0; $i<=sizeof($attach_file);$i++)
        					 {
        					 $output .=  'attach_field +="<p rel=\"'.$i.'\">";
        					 attach_field +="<label for=\"attach_file_browser['.$i.']\">Путь файла</label> ";
        					 attach_field +="<input id=\"attach_file['.$i.']\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"'.str_replace('"','',$attach_file[$i]).'\" name=\"attach_file['.$i.']\" />";
        					 attach_field +="<input id=\"attach_file_browser['.$i.']\" type=\"button\" onclick=\"BrowseFileServer(\'attach_file['.$i.']\')\" value=\"Выбрать\"/>";
        					 attach_field +=" <label for=\"attach_file_name['.$i.']\">Название файла</label> ";
        					 attach_field +="<input id=\"attach_file_name['.$i.']\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"'.htmlspecialchars($attach_file_name[$i]).'\" name=\"attach_file_name['.$i.']\"/>";
        					 attach_field +=" <a href=\"#\" id=\"add_attach_field\">Добавить поле</a> ";
        					 attach_field +=" <a href=\"#\" id=\"delete_attach_field\">Удалить поле</a> ";
        					 attach_field +="</p>";';
        					 }
        					
        					 $output .=  'attach_end ="</div>";
        					
        					  jQuery(".sectionBody .sectionBody:eq(0)").after(attach+attach_field+attach_end);
        					  
        					  
        					  jQuery("#add_attach_field").live("click",function(e){
        																		
        							
        								number=0;
        								a=jQuery(this).parent().parent();
        								var number= parseInt(a.children("p:last").attr("rel"))+1;								
        								attach_field ="<p rel=\""+number+"\">" ;
        								attach_field +="<label for=\"attach_file_browser["+number+"]\">Путь файла</label> ";
        								attach_field +="<input id=\"attach_file\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file["+number+"]\"/>";
        								attach_field +="<input id=\"attach_file_browser["+number+"]\" type=\"button\" onclick=\"BrowseFileServer(\'attach_file["+number+"]\')\" value=\"Выбрать\"/>";
        								attach_field +=" <label for=\"attach_file_name["+number+"]\">Название файла</label> ";
        								attach_field +="<input id=\"attach_file_name["+number+"]\" type=\"text\" onchange=\"documentDirty=true;\" style=\"width: 200px;\" value=\"\" name=\"attach_file_name["+number+"]\"/>"
        								attach_field +=" <a href=\"#\" id=\"add_attach_field\">Добавить поле</a> ";	
        								attach_field +=" <a href=\"#\" id=\"delete_attach_field\">Удалить поле</a> ";
        								attach_field +="</p>";
        								
        								jQuery(this).parent().parent().append(attach_field);
        								
        								return false;});
        					   
        					    jQuery("#delete_attach_field").live("click",function(e){
        																		
        											
        								jQuery(this).parent().remove();
        								
        								return false;});
        					            
        																										 
        					});</script>';  
        	
            $e->output($output);
          break;
        }
        


        В Событиях отметить OnBeforeDocFormSave и OnDocFormPrerender
          How could number of mysql queries be more than number of tables in DB???