<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

class jcr_file_custom
{
    
/**
     * Initialise.
     */
     
function __construct()
    {
        
// Hook into the system's callbacks.
        
register_callback(array(__CLASS__'lifecycle'), 'plugin_lifecycle.jcr_file_custom');
        
register_callback(array(__CLASS__'ui'), 'file_ui''extend_detail_form');
        
register_callback(array(__CLASS__'save'), 'file''file_save');
    }

    
/**
   * Add and remove custom field from txp_file table.
   *
   * @param $event string
   * @param $step string  The lifecycle phase of this plugin
   */
  
public static function lifecycle($event$step)
  {
      switch (
$step) {
          case 
'enabled':
              break;
          case 
'disabled':
              break;
          case 
'installed':
              
safe_alter(
                
'txp_file',
                
'ADD COLUMN jcr_file_custom VARCHAR(255) NULL AFTER author'
              
);
              break;
          case 
'deleted':
              
safe_alter(
                
'txp_file',
                
'DROP COLUMN jcr_file_custom'
              
);
              break;
      }
      return;
  }

    
/**
     * Paint additional fields for file custom field
     *
     * @param $event string
     * @param $step string
     * @param $dummy string
     * @param $rs array The current file's data
     * @return string
     */
    
public static function ui($event$step$dummy$rs)
    {
        
extract(lAtts(array(
            
'jcr_file_custom' => ''
        
), $rs0));

        return
            
inputLabel('jcr_file_custom'fInput('text''jcr_file_custom'$jcr_file_custom''''''INPUT_REGULAR'''jcr_file_custom'), 'jcr_file_custom').n;
    }

    
/**
     * Save additional file custom fields
     *
     * @param $event string
     * @param $step string
     */
    
public static function save($event$step)
    {
        
extract(doSlash(psa(array('jcr_file_custom''id'))));
        
$id assert_int($id);
        
safe_update('txp_file',
          
"jcr_file_custom = '$jcr_file_custom'",
            
"id = $id"
        
);
    }
}

if (
txpinterface === 'admin') {

    new 
jcr_file_custom;

} elseif (
txpinterface === 'public') {

    if (
class_exists('\Textpattern\Tag\Registry')) {
        
Txp::get('\Textpattern\Tag\Registry')
            ->
register('jcr_file_custom');
    }

}

  
/**
   * Public tag: Output custom file field
   * @param  string $atts[escape] Convert special characters to HTML entities.
   * @return string custom field output
   * <code>
   *        <txp:jcr_file_custom escape="html" />
   * </code>
   */

    
function jcr_file_custom($atts)
    {
        global 
$thisfile;

        
assert_file();

        
extract(lAtts(array(
            
'escape' => 'html',
        ), 
$atts));

        return (
$escape == 'html')
            ? 
txpspecialchars($thisfile['jcr_file_custom'])
            : 
$thisfile['jcr_file_custom'];
    }