2

I have created a PDF generating tool that creates certificates for members, creates a sub-folder within a one of my file upload directories, then places the file into the new directory. I am trying to figure out how I can run this script, then tell Assets to Update Indexes, via PHP probably, then the channel:form I have can see the new file/subfolder I have created.

In the Developer docs, there are hooks for assets_create_folder and assets_upload_file. These hooks look like they work in the opposite direction to what I want.

I want to tell Assets to index new folders and files that I have generated.

Is there any way to do this using PHP in a template without building an extension?

Thank you!

2 Answers 2

2

I got an email back from Andris at P&T with the following:

Hey Rowan.

Indexing generally refers to a bunch of files - seems to me that you'd be doing that one file at a time. You should look into Assets_lib->register_ee_file($filedir_id, $path);

Then if you have a Filedir with ID of, say, 8, that has a PDF file at "some/other/folder/file.pdf" that you want Assets to know about, just call ee()->assets_lib->register_ee_file(8, "some/other/file.pdf");

Also, make sure to load the lib beforehand.

$this->EE->load->add_package_path(PATH_THIRD.'assets/');
$this->EE->load->library('assets_lib'); 

I followed the instructions Andris gave me and with minimal effort, was able to register the files as needed.

So laser focused, i'm really happy with this solution!

In case anyone else is curious, here is some of my code for the PDF generation, imagick pdf to jpg generation, then Assets file registration.

<?php

$this->EE->load->add_package_path(PATH_THIRD.'assets/');
$this->EE->load->library('assets_lib');

// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
require './site_assets/dompdf/vendor/autoload.php';

// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', false);

// include DOMPDF's default configuration
require_once './site_assets/dompdf/dompdf_config.inc.php';  


?>

... generate my $html code i want to turn into PDF ...

Handle the DOM2PDF etc.

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->set_paper('letter','landscape');

$dompdf->render();
$pdf = $dompdf->output();

$dir = "/assets/uploads/trainee_attachments/";

$path_to_uploads = NSM_BASEPATH.$dir.$member_entry_id."/";
$url_to_uploads = "{path='".$dir."'}/".$member_entry_id."/";

if(!file_exists($path_to_uploads)){
    mkdir($path_to_uploads,0777,true);
    echo "created directory";
}
$cert_filename = $url_title."_".$format."_certificate_".$member_entry_id;
$cert_url_full = $path_to_uploads.$cert_filename;


if (file_put_contents($cert_url_full.".pdf",$pdf)){
    echo "<p>PDF Generated!</p>";
}
echo ("<a href='".$url_to_uploads.$cert_filename.".pdf'>view pdf</a><br/>");
echo ("PDF URL: ".$url_to_uploads.$cert_filename.".pdf");


// display jpg of pdf
$imagick = new Imagick(); 
$imagick->setResolution(300,300);
$imagick->readImage($cert_url_full.".pdf"); 
$imagick->writeImages($path_to_uploads.$cert_filename.'.jpg', false); 

ee()->assets_lib->register_ee_file(11, $member_entry_id."/".$cert_filename.".jpg");
ee()->assets_lib->register_ee_file(11, $member_entry_id."/".$cert_filename.".pdf");

echo ("<br/>JPG URL: ".$url_to_uploads.$cert_filename.'.jpg');
echo ("<br/><a href='".$url_to_uploads.$cert_filename.".jpg' class='fancybox'><img src='".$url_to_uploads.$cert_filename.".jpg' class='img-responsive col-md-3'/></a>");
?>
1

I can help you figure out the direction you should take, but this one seems obscure and like quite a process, and I'm pretty busy right now. First, I would trace back from the ajax hit that happens when you call Assets to index a new folder from that Control Panel page. Use your browser developer tools to monitor XHR processes (this is usually under the "Network" tab of your dev tools, depends on what browser you use), and index some random folder.

When you do that, you should be able to get the ACTion ID used for indexing folders. Then, look in your exp_actions database table. Match up that ACTion ID, and look at what method it calls in the Assets module.

SELECT method FROM exp_actions
    WHERE action_id = %% your discovered action id number here in double quotes (no % signs) %%;

From there, you can look up that method in the Assets web-root/system/expressionengine/third_party/assets/mod.assets.php file. The key from there is to see if you can import the Assets module class into your own add-on (or however you've implemented your PDF generation). Then instantiate the module class, and just call the appropriate methods to index a folder.

If instantiating the Assets module class doesn't work, you at least know what code to copy to reverse engineer how Assets indexes folders.

1
  • Thanks @jrothafer for the response. I didn't have to dig too much thanks to P&T's official response. I do appreciate your answer though! Oct 19, 2015 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.