Jump to content

custom inputfieldselect


Recommended Posts

I created (or trying to) an inpufieldselect that shows me all files stored in a server folder and save the value as text.

It works good but i have two problems.

First: I need to execute a function called "sendVideo" after the page is saved based on my select value. I tried to add an hookafter save but the code is executed everytime a page is saved and not only the current page.

public function init() {
$this->pages->addHookAfter('save', $this, 'sendVideo'); 
}

Second: I need to execute my code only if i change the value of my select and do nothing if not .

This is my entire code

<?php

class FieldtypeSelect extends Fieldtype {

    public static $defaultOptionValues = array ();

    public static function getModuleInfo() {
  return array(
  'title' => 'Select temporary videos',
  'version' => 100,
  'summary' => 'This Fieldtype get files stored in /toencode/ folder',
  'singular' => true, 
  'autoload' => true, 
  );
    }

   public function init() {
	$this->pages->addHookAfter('save', $this, 'sendVideo'); 
   }

   public function sendVideo($event) {
	$page = $event->arguments[0]; 
	$vfile = "http://".$_SERVER['HTTP_HOST']."/videodaconvertire/".$page->videotoconvert;

	$filename = $page->id;

	function sendRequestv($xml)  
	{  
	    $ch = curl_init();  
	    curl_setopt($ch, CURLOPT_URL, "http://manage.encoding.com/");  
	    curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=" . urlencode($xml));  
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
	    curl_setopt($ch, CURLOPT_HEADER, 0);  
	    return curl_exec($ch);  
	}   
	  

	// Begin processing User's POST  
	if(!empty($vfile))  
	{  
	    // Preparing XML request  
	    
	    // Main fields  
	    $req = new SimpleXMLElement('<?xml version="1.0"?><query></query>');  
	    $req->addChild('userid', 'xxxxxx');  
	    $req->addChild('userkey', 'xxxxxxxxxxxxxxxxxxxx');  
	    $req->addChild('action', 'AddMedia');  
	    $req->addChild('source', $vfile); 
	    $req->addChild('notify', 'http://www.librolo.com/addmedia/');  		
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'flv');
	    $formatNode->addChild('size', '650x366');
	    $formatNode->addChild('bitrate', '512k');
	    $formatNode->addChild('destination','ftp://xxx:xxx@librolo.com/public_html/video/'.$filename.'.flv');
	    
	    $logoNode = $formatNode->addChild('logo');  
	    $logoNode->addChild('logo_source', 'http://www.librolo.com/watermark.png');
	    $logoNode->addChild('logo_x', '4');
	    $logoNode->addChild('logo_y', '4');
	    $logoNode->addChild('logo_mode', '1');
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'thumbnail');
	    $formatNode->addChild('time', '10%');
	    $formatNode->addChild('width', '650');
	    $formatNode->addChild('destination', 'ftp://xxx:xxx@librolo.com/public_html/thumbs/'.$filename.'_1.jpg');
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'thumbnail');
	    $formatNode->addChild('time', '30%');
	    $formatNode->addChild('width', '650');
	    $formatNode->addChild('destination', 'ftp://xxx:xxx@librolo.com/public_html/thumbs/'.$filename.'_2.jpg');
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'thumbnail');
	    $formatNode->addChild('time', '50%');
	    $formatNode->addChild('width', '650');
	    $formatNode->addChild('destination', 'ftp://xxx:xxx@librolo.com/public_html/thumbs/'.$filename.'_3.jpg');
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'thumbnail');
	    $formatNode->addChild('time', '70%');
	    $formatNode->addChild('width', '650');
	    $formatNode->addChild('destination', 'ftp://xxx:xxx@librolo.com/public_html/thumbs/'.$filename.'_4.jpg');
	    
	    $formatNode = $req->addChild('format');  
	    $formatNode->addChild('output', 'thumbnail');
	    $formatNode->addChild('time', '90%');
	    $formatNode->addChild('width', '650');
	    $formatNode->addChild('destination', 'ftp://xxx:xxx@librolo.com/public_html/thumbs/'.$filename.'_5.jpg');
	     
	    // Sending API request  
	    $res = sendRequestv($req->asXML());  
	    
	    try  
	    {  
	        // Creating new object from response XML  
	        $response = new SimpleXMLElement($res);  
	        
	        // If there are any errors, set error message  
	        if(isset($response->errors[0]->error[0])) {  
	            $error = $response->errors[0]->error[0] . '';  
	        }  
	        else  
	        if ($response->message[0]) {  
	            // If message received, set OK message  
	           $message =$response->message[0];
	        }  
	    }  
	    catch(Exception $e)  
	    {  
	        // If wrong XML response received  
	        $error = $e->getMessage();  
	    }  
	    
	    // Displaying error if any  
	    if (!empty($error)) {  
	        $this->message(htmlspecialchars($error));  
	    }  
	    
	    // Displaying message  
	    if (!empty($message)) {  
	        //$this->message($message);
	    }  
	} 
}
    
    
    public function getInputfield(Page $page, Field $field) {

	 function read_folder_directory($dir = "root_dir/dir") { 
        $listDir = array(); 
        if($handler = opendir($dir)) { 
            while (($sub = readdir($handler)) !== FALSE) { 
                if ($sub != "." && $sub != ".." && $sub != "Thumb.db" && $sub != "Thumbs.db") { 
                    if(is_file($dir."/".$sub)) { 
                        $listDir[] = $sub; 
                    }elseif(is_dir($dir."/".$sub)){ 
                        $listDir[$sub] = ReadFolderDirectory($dir."/".$sub); 
                    } 
                } 
            } 
            closedir($handler); 
        } 
        return $listDir; 
    } 

  $inputfield = $this->modules->get('InputfieldSelect');
  $inputfield->addOption(''); // blank or unselected option
  
  $files = read_folder_directory ($_SERVER["DOCUMENT_ROOT"]."/videodaconvertire/"); 

	if ($files) 
	{ 
	     foreach ($files as $file) 
	     { 
	           $inputfield->addOption($file,$file);
	     } 
	} 
  
  return $inputfield;
    }

    public function getDatabaseSchema(Field $field) {
  $schema = parent::getDatabaseSchema($field);
  $schema['data'] = 'text NOT NULL';
  $schema['keys']['data_exact'] = 'KEY `data_exact` (`data`(255))';
  $schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';

  return $schema;
    }

    public function sanitizeValue(Page $page, Field $field, $value) {
  return $value;
    }

    public function ___getConfigInputfields(Field $field) {
  $inputfields = NULL;
  return $inputfields;
    }
}

This is made for adding a video to a page using encoding.com apis.

Basically i upload some videos in a folder, than i create a page and choose a video for encoding, after the page is saved i use encoding.com apis to send request to their server  via xml and after the video is encoded and thumbnails created with a callback function that call an hidden pw page i editing the page, add the thumbnails and save it again. :)

Link to comment
Share on other sites

If I'm understanding correctly, I don't think that you need that 'save' hook at all. Instead, you can just implement this function which is already part of the Fieldtype interface, and PW will call it only on the $page and $field that you expect:

<?php

public function ___savePageField(Page $page, Field $field) {

    if($page->isChanged($field->name)) {
        // your sendVideo code here - you should probably put it in a new function and call that function here
    }

    // since you aren't handling PW's DB save, let it do the rest: 
    return parent::___savePageField($page, $field); 
}

See this file which provides the full interface and functions that you can override in any Fieldtype:

/wire/core/Fieldtype.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...