Tuesday 30 August 2016

How to Get / Upload / Delete / Update file in using SFTP / FTP in PHP, Codeigniter Library

PHP has ssh2 stream wrappers (disabled by default), so you can use sftp connections with any function that supports stream wrappers by using ssh2.sftp:// for protocol

Here is a small code on how to read the folder and download all files:

Controller.php

      $params = array('host' => $shopdata->ftphost, 'port' => 22);
            $this->load->library('sftpconnection', $params);
         
            $this->sftpconnection->login($shopdata->ftpuser, $shopdata->ftppass);
            //$this->sftpconnection->uploadFile("http://localhost/shopify/inventorymanagement/uploads/sku.csv", "/codetest/test.csv");
            //$result = $this->sftpconnection->scanFilesystem("/TEST/DESADV");
            $result = $this->sftpconnection->downloadFiles("$folder","/TEST/DESADV/");
            pre($result);
Create Library file : Sftpconnection.php

<?php
/*
  Sftp connection in PHP
  Created: Feb 18th, 2016
  Modified: Feb 18th, 2016
  Version: 1.0.1
  Auther : Jaydip Kanasagra
 */

//this function is just to make the code a little cleaner
class Sftpconnection {

    private $connection;
    private $sftp;

    public function __construct($config) {
        $host = $config['host'];
        $port = $config['port'];
        $this->connection = @ssh2_connect($host, $port);
        if (!$this->connection)
            throw new Exception("Could not connect to $host on port $port.");
    }

    public function login($username, $password) {
        if (!@ssh2_auth_password($this->connection, $username, $password))
            throw new Exception("Could not authenticate with username $username " .
            "and password $password.");

        $this->sftp = @ssh2_sftp($this->connection);
        if (!$this->sftp)
            throw new Exception("Could not initialize SFTP subsystem.");
    }

    public function uploadFile($local_file, $remote_file) {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');

        if (!$stream)
            throw new Exception("Could not open file: $remote_file");

        $data_to_send = @file_get_contents($local_file);
        if ($data_to_send === false)
            throw new Exception("Could not open local file: $local_file.");

        if (@fwrite($stream, $data_to_send) === false)
            throw new Exception("Could not send data from file: $local_file.");

        @fclose($stream);
    }

    public function receiveFile($remote_file, $local_file) {
        $sftp = $this->sftp;
        $stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');
        if (!$stream)
            throw new Exception("Could not open file: $remote_file");
        $contents = fread($stream, filesize("ssh2.sftp://$sftp$remote_file"));
        file_put_contents($local_file, $contents);
        @fclose($stream);
    }
 
    public function downloadFiles($local_dir, $remote_dir) {
        $sftp = $this->sftp;
        $dir = "ssh2.sftp://$sftp$remote_dir";
        $downloadedfiles = $files = array();
        $handle = opendir($dir);
     
        // List all the files
        while(false !== ($file = readdir($handle))) {
            if (substr("$file", 0, 1) != ".") {
                if (is_dir($file)) {
                //$tempArray[$file] = $this->scanFilesystem("$dir/$file");
                } else {
                    $files[] = $file;
                }
            }
        }
        closedir($handle);
     
        foreach ($files as $file)
        {
            $currentdir = $this->getCurrentdirfiles('uploads/ftpfiles/TEST/JAYDIP/');
            if(in_array($file, $currentdir)){
                continue;
            }
         
            echo "Copying file: $file<br>";
            if (!$remote = @fopen("ssh2.sftp://{$sftp}{$remote_dir}{$file}", 'r'))
            {
                echo "Unable to open remote file: $file\n";
                continue;
            }

            if (!$local = @fopen($local_dir . $file, 'w'))
            {
                echo "Unable to create local file: $file\n";
                continue;
            }

            $read = 0;
            $filesize = filesize("ssh2.sftp://{$sftp}/{$remote_dir}{$file}");
            while ($read < $filesize && ($buffer = fread($remote, $filesize - $read)))
            {
                $read += strlen($buffer);
                if (fwrite($local, $buffer) === FALSE)
                {
                    echo "Unable to write to local file: $file\n";
                    break;
                }
            }
            $downloadedfiles[] = $file;
            fclose($local);
            fclose($remote);
        }

        return $downloadedfiles;
    }

    function scanFilesystem($remote_file) {
        $sftp = $this->sftp;
        $dir = "ssh2.sftp://$sftp$remote_file";
        $files = array();
        $handle = opendir($dir);
     
        // List all the files
        while(false !== ($file = readdir($handle))) {
            if (substr("$file", 0, 1) != ".") {
                if (is_dir($file)) {
                //$tempArray[$file] = $this->scanFilesystem("$dir/$file");
                } else {
                    $files[] = $file;
                }
            }
        }
        closedir($handle);
        return $files;
    }
 
    function getCurrentdirfiles($dir){
        //$dir = "ftpfiles/TEST/JAYDIP/";
        $listfiles = array();
        $handle = opendir($dir);
     
        // List all the files
        while(false !== ($file = readdir($handle))) {
            if (substr("$file", 0, 1) != ".") {
                if (is_dir($file)) {
                //$tempArray[$file] = $this->scanFilesystem("$dir/$file");
                } else {
                    $listfiles[] = $file;
                }
            }
        }
        closedir($handle);
        return $listfiles;
    }

}

?>

How to connect WSDL file with XML data with Basic Authentication in php

HTTP Auth works with SOAP Client, However you cannot access password protected WSDL files 

This is Simple example to auth webservice using soapClient
<?php
ini_set('display_errors', 1);
/*$client = new SoapClient('Materials_Availability.wsdl');
var_dump($client->__getFunctions());*/
$wsdl = "http://localhost/test/soap_wsdl/stock.wsdl?wsdl";
$client = new SoapClient($wsdl, array('login' => "******", 'password' => "******"));
echo ''.print_r($client,true).'';
$result = $client->__getFunctions();
echo '<pre>';print_r($result);
//$file = file_get_contents('soapxml.xml');
//echo $file;exit;
$array = array(
    'REQUEST'=> array(
        'MATERIALS'=> array(
                0=>array(
                    'MATERIAL_NUMBER'=>123654,
                    'MATERIAL_NUMBER_TYPE'=>'JAY',
                    'REQUEST_TYPE'=>array(
                        'TYPE'=>1
                    )
                ),
                1=>array(
                    'MATERIAL_NUMBER'=>654789,
                    'MATERIAL_NUMBER_TYPE'=>'JAM',
                    'REQUEST_TYPE'=>array(
                        'TYPE'=>2
                    )
                )
            )
        )
    );
print_r($array);
$result = $client->getMaterialAvailability($array);
print_r($result);
exit;
?>

Friday 19 August 2016

How to set wait/process bar/blur div using css html

Html

Add following code in you html

<div class="wait"></div>

Css

Add following code in you css

.wait{
 background: #fff url("../img/waiting.gif") no-repeat scroll center center;
 bottom: 0;
 left: 0;
 opacity: 0.5;
 position: fixed;
 right: 0;
 top: 0;
 z-index: 1;
}

Friday 5 August 2016

Create next previous button script in jQuery, HTML

HEAD
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<style>
    .eventslider{height: 368px;overflow: hidden;}
</style>
<script type="text/javascript">
    $jq = jQuery.noConflict();
    $jq(function () {
        var $step = 2;
        $jq('.eventslider .event_detail_container').hide();
        for (i = 0; i < $step; i++) {
            $jq('.eventslider .event_detail_container').eq(i).show();
        }
        $jq('.previousvents').hide();
        $jq('.nextevents').click(function () {
            var length = $jq('.eventslider .event_detail_container').length;
            var $this = $jq(this);
            var $nextvalue = $this.attr('rel');
            if ($nextvalue < length) {
                $jq('.eventslider .event_detail_container').hide();
                var newnext = $nextvalue;
                for (i = $nextvalue; i < Number($nextvalue) + $step; i++) {
                    $jq('.eventslider .event_detail_container').eq(i).show();
                    newnext = i;
                }
                newnext = Number(newnext) + 1
                if (newnext < length) {
                    $this.attr('rel', newnext);
                    $jq('.previousvents').attr('rel', newnext);
                    $jq('.previousvents').show();
                } else {
                    $jq('.previousvents').show();
                    $jq('.previousvents').attr('rel', newnext);
                    $this.hide();
                }
            }
        });
        $jq('.previousvents').click(function () {
            var $this = $jq(this);
            var $previousvalue = $this.attr('rel');
            $previousvalue = $previousvalue - (Number($step) + 1);
            $jq('.eventslider .event_detail_container').hide();
            var newprevious = 0;
            for (i = $previousvalue; i > Number($previousvalue) - $step; i--) {
                $jq('.eventslider .event_detail_container').eq(i).show();
                newprevious = i;
            }
            newprevious = Number(newprevious) + Number($step);
            if (newprevious <= $step) {
                $this.hide();
                $jq('.nextevents').show();
                $jq('.nextevents').attr('rel', newprevious);
            } else {
                $this.attr('rel', newprevious);
                $jq('.nextevents').attr('rel', newprevious);
                $jq('.nextevents').show();
            }
        });
    });
</script>

Body
<div>
    <a href="javascript:void(0)" class="previousvents" rel="0">Previous</a>
    <div class="eventslider">
        <div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0 top">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event 3</span>
                <h2><a itemprop="url" href="/test-event-3">Test Event 3</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-06" itemprop="startDate">October 06,  2016.</span><span content="2016-10-06" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event  z</span>
                <h2><a itemprop="url" href="/test-event-z">Test Event  z</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-07" itemprop="startDate">October 07,  2016.</span><span content="2016-10-07" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Eventtt</span>
                <h2><a itemprop="url" href="/test-eventtt">Test Eventtt</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-27" itemprop="startDate">October 27,  2016.</span><span content="2016-10-27" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Evebt  sss</span>
                <h2><a itemprop="url" href="/test-evebt-sss">Test Evebt  sss</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-11-03" itemprop="startDate">November 03,  2016.</span><span content="2016-11-03" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">test event 22633</span>
                <h2><a itemprop="url" href="/test-event-2">test event 22633</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-08-12" itemprop="startDate">August 12,  2016.</span><span content="2016-08-12" itemprop="endDate">  </span> <span class="ohanah-time">4:00 am</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event 4</span>
                <h2><a itemprop="url" href="/test-event-4">Test Event 4</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-09-08" itemprop="startDate">September 08,  2016.</span><span content="2016-09-08" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">


            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event 9999</span>
                <h2><a itemprop="url" href="/test-event-9999">Test Event 9999</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-09-08" itemprop="startDate">September 08,  2016.</span><span content="2016-09-08" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">


            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">test event</span>
                <h2><a itemprop="url" href="/test-event">test event</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-04" itemprop="startDate">October 04,  2016.</span><span content="2016-10-04" itemprop="endDate">  </span> <span class="ohanah-time">4:00 am</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event 00011</span>
                <h2><a itemprop="url" href="/test-event-00011">Test Event 00011</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-04" itemprop="startDate">October 04,  2016.</span><span content="2016-10-04" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div><div itemtype="http://schema.org/Event" itemscope="" class="event_detail_container   cat-3 venue-0">
            <div class="event_detail_title">
                <span style="display:none;" itemprop="name">Test Event ZZXaaaa</span>
                <h2><a itemprop="url" href="/test-event-zzxaaaa">Test Event ZZXaaaa</a></h2>
            </div>
            <div class="event_detail_time" style="display:inline">
                <div class="date_icon"></div>
                <h3 style="display:inline">
                    <span content="2016-10-05" itemprop="startDate">October 05,  2016.</span><span content="2016-10-05" itemprop="endDate">  </span> <span class="ohanah-time">5:00 pm</span>  <span class="ohanah-time"></span> </h3>
            </div>
            <div class="event-spacer"><br><br></div>
            <div id="event-container-info">
                <span class="ohanah-registration-link" style="float: right; padding-left:12px">
                </span>
            </div>
            <div class="event-spacer"><br><br></div>
        </div>
    </div>
    <a href="javascript:void(0)" class="nextevents" rel="2">Next</a>
</div> 

Thursday 4 August 2016

Export csv after place order in magento

config.xml


<?xml version="1.0"?>
<config>
  <modules>
    <Test_Test>
      <version>0.1.0</version>
    </Test_Test>
  </modules>
  <global>
    <helpers>
      <test>
        <class>Test_Test_Helper</class>
      </test>
    </helpers>
<models>
 <test>
<class>Test_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
 </test>
</models>
    <events>
 <checkout_onepage_controller_success_action> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_onepage_controller_success_action_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>test/observer</class> <!-- observers class alias -->
            <method>exportCsvOnCheckout</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_onepage_controller_success_action_handler>
        </observers>
      </checkout_onepage_controller_success_action>
    </events>
  </global>
</config>


Observer.php

<?php
class Test_Test_Model_Observer
{
public function exportCsvOnCheckout(Varien_Event_Observer $observer)
{
$orderIds = $observer->getData('order_ids');
$order     = Mage::getModel('sales/order')->load($orderIds);
$customer = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
// echo '<pre>';print_r($customer->getName());exit;
$file_path = "order.csv";
$mage_csv = new Varien_File_Csv();
$data = array();
$data['Email'] = $customer->getEmail();
$data['name'] = $customer->getName();
$products_row[] = $data;
$mage_csv->saveData($file_path, $products_row);
}
} }

Magento Module/Extension creator First step demo

Add custom price on frontend using admin side attribute

config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Test_Test>
      <version>0.1.0</version>
    </Test_Test>
  </modules>
  <global>
    <helpers>
      <test>
        <class>Test_Test_Helper</class>
      </test>
    </helpers>
<models>
 <test>
<class>Test_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
 </test>
</models>
  </global>
  <frontend>
    <events>
 <catalog_product_collection_load_after> <!-- identifier of the event we want to catch -->
        <observers>
          <catalog_product_collection_load_after_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>test/observer</class> <!-- observers class alias -->
            <method>catalogProductLoadAfter</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </catalog_product_collection_load_after_handler>
        </observers>
      </catalog_product_collection_load_after>
    </events>
</frontend>
</config>

Observer.php

<?php
class Test_Test_Model_Observer
{
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
$collection = $observer->getEvent()->getCollection();
foreach ($collection as $product)
{
$produ = Mage::getModel('catalog/product')->load($product->getId())->getData('discountatt');
$originalprice = $product->getPrice();
$customprice = $originalprice+$produ;
$product->setPrice($customprice);
// $product->setData('my_price',$customprice);
}
// echo '<pre>';print_r($produ); exit;
// $product = $observer->getEvent()->getProduct();
/*$originalprice = $product->getPrice();
$product->setPrice($customprice);*/
}
}