<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

class jcr_link_custom
{
    
/**
     * Initialise.
     */
     
function __construct()
    {
        
register_callback(array(__CLASS__'lifecycle'), 'plugin_lifecycle.jcr_link_custom');
        
register_callback(array(__CLASS__'ui'), 'link_ui''extend_detail_form');
        
register_callback(array(__CLASS__'save'), 'link''link_save');
    }

  
/**
   * Add and remove custom field from txp_link 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_link',
                
'ADD COLUMN jcr_link_custom VARCHAR(255) NULL AFTER author'
              
);
              break;
          case 
'deleted':
              
safe_alter(
                
'txp_link',
                
'DROP COLUMN jcr_link_custom'
              
);
              break;
      }
      return;
  }

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

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

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

if (
txpinterface === 'admin') {

    new 
jcr_link_custom;

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

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

}

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

    
function jcr_link_custom($atts)
    {
        global 
$thislink;

        
assert_link();

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

        return (
$escape == 'html')
            ? 
txpspecialchars($thislink['jcr_link_custom'])
            : 
$thislink['jcr_link_custom'];
    }