A short missive on using the file path constants that SyntaxCMS. Relative paths can make your applicatoin more fragile than it needs to be (see dbasis), so don't do the following:
include realpath('./../../stx/lib/documentInfo/documentInfo.php');
The correct way is (note the parens around include as well, that's our coding standard)
There's a corresponding SITE_LIB_PATH for private/lib, which is for project specific includes. You can see what global path constants Syntax sets up in stx/init/init.urls.php. Also, realpath() incurs a stat call, iirc, which degrades performance.
Next:
$doc = new documentInfo();
$doc->set_file(realpath(SITE_PATH.'/..') . $Record->getFile());
There's a constant for the files path - PXDB_FILESTORE_PATH, so the above should be the following. The basename call is extra, because int his case Syntax is storing the filename as "/files/205_file_myfile.pdf" due to a pxdb_prefs setting.
$doc = new documentInfo();
$doc->set_file(PXDB_FILE_STORE . '/' . basename($Record->getFile()));
First, note above that the name $doc is a bit misleading, a better name would be $doc_info. Instead of embedding this functionality in a specific template (the detail template in this case), fetching the file information should be encapsulated as a method of the Document object by adding the following to lib/Document.class.php file. A review of the documentInfo API turns up a method that returns a document Info object for a file in a SyntaxCMS instance, named stxFactory and takes the file value as a parameter. So we add the following method to our Document class definition:
/**
Returns a documentInfo object for the File field.
@access public
@return object
/
function getFileInfo()
{
// only need to instantiate this once per object
if (!isset($this->_docInfo))
{
$this->_file_info = documentInfo::stxFactory($this->getFile());
}
return $this->_file_info;
}
Why is the extra effort worth it, if the starting code works? Imagine that a week from now, you're asked to display the file, and its size and type information, wherever it is listed. Are you going to copy and paste that snippet to multiple templates? What if you discover a bug in the code you cut and paste - how can you be sure you hunt down and correct all the copies? With good encapsulation your display template(s) are getting the values they need to display from a single source.