cakephp

parse json response from cakephp query and prepoulate the selectbox

$(document).ready(function(){
$(“#cat”).change(function(){
var subcatid = $(“#cat”).val();
$.ajax({
type: “POST”,
url: “webroot; ?>users/subcatfind”,
data: { id: subcatid},
success:function(data){
var sel = $(“#subc”);
sel.empty();
var jso=$.parseJSON(data);
for(i=0;i<= jso.length;i++){
sel.append('’+jso[i].Subcategorie.title+”);;
}
}

});

});
});

Advertisements

Room booking query

$out=$this->Booking->find(‘list’,array(‘conditions’=>array(‘Booking.datein =’.$da),’fields’=>’Booking.room_no’));
if(empty($out)){
$this->set(‘rooms’,$this->Room->find(‘all’));
}
else
{
$out=$this->Room->find(‘all’,array(‘conditions’=>array(‘NOT’=>array(‘Room.room_no’=>$out))));
$this->set(‘rooms’,$out);

}

Using dropbox api/access dropbox files

1)Create account on dropbox.com and find secret and key.2)Download files from

http://fabi.me/en/php-projects/dropphp-dropbox-api-client/comment-page-1/

3)Set the below in sample.php

$dropbox = new DropboxClient(array(
‘app_key’ => “ztjdoqcp6fokpdg”,
‘app_secret’ => “1zcrmy0eja21lcl”,
‘app_full_access’ => true,
),’en’);

4)Create folder tokens in the downloaded folder if it is not there.

5)The output looks like

loaded access token:

Account:
stdClass Object
(
[referral_link] => http://db.tt/fnZNU4dH
[display_name] => pardeep poria
[uid] => 209317516
[country] => IN
[quota_info] => stdClass Object
(
[shared] => 0
[quota] => 2147483648
[normal] => 498318
)

[email] =>
)

Files:
Array
(
[0] => Getting Started.pdf
[1] => test_download_Getting Started.pdf
)

Meta data of /Getting Started.pdf:
stdClass Object
(
[revision] => 1
[rev] => 113bbff14
[thumb_exists] =>
[bytes] => 249159
[modified] => Mon, 02 Sep 2013 05:20:04 +0000
[client_mtime] => Mon, 02 Sep 2013 05:20:04 +0000
[path] => /Getting Started.pdf
[is_dir] =>
[icon] => page_white_acrobat
[root] => dropbox
[mime_type] => application/pdf
[size] => 243.3 KB
)

Downloading /Getting Started.pdf:
stdClass Object
(
[revision] => 1
[rev] => 113bbff14
[thumb_exists] =>
[bytes] => 249159
[modified] => Mon, 02 Sep 2013 05:20:04 +0000
[client_mtime] => Mon, 02 Sep 2013 05:20:04 +0000
[path] => /Getting Started.pdf
[is_dir] =>
[icon] => page_white_acrobat
[root] => dropbox
[mime_type] => application/pdf
[size] => 243.3 KB
)

Uploading test_download_Getting Started.pdf:
stdClass Object
(
[revision] => 2
[rev] => 213bbff14
[thumb_exists] =>
[bytes] => 249159
[modified] => Mon, 02 Sep 2013 11:42:01 +0000
[client_mtime] => Mon, 02 Sep 2013 11:42:01 +0000
[path] => /test_download_Getting Started.pdf
[is_dir] =>
[icon] => page_white_acrobat
[root] => dropbox
[mime_type] => application/pdf
[size] => 243.3 KB
)

done!

Revisions of test_download_Getting Started.pdf:
Array
(
[0] => stdClass Object
(
[revision] => 2
[rev] => 213bbff14
[thumb_exists] =>
[bytes] => 249159
[modified] => Mon, 02 Sep 2013 11:42:01 +0000
[client_mtime] => Mon, 02 Sep 2013 11:42:01 +0000
[path] => /test_download_Getting Started.pdf
[is_dir] =>
[icon] => page_white_acrobat
[root] => dropbox
[mime_type] => application/pdf
[size] => 243.3 KB
)

)

Searching for JPG files:
Nothing found.

scrapping data from a webpage using php,curl and dom object:inesrt into database

<?php
set_time_limit(0);
require_once(‘simple_html_dom.php’);
    // Defining the basic cURL function
    function curl($url) {
        $ch = curl_init();  // Initialising cURL
        curl_setopt($ch, CURLOPT_URL, $url);    // Setting cURL’s URL option with the $url variable passed into the function
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL’s option to return the webpage data
        $data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
        curl_close($ch);    // Closing cURL
        return $data;   // Returning the data from the function
    }
    $scraped_website = curl(“http://www.yellowpages.com/los-angeles-ca/doctors?g=los+angeles%2Cca&#8221;);
    
    $doc = new DOMDocument();
  $data=@$doc->loadHTML($scraped_website);
  //print_r($data);
  $achr=$doc->getElementById(‘search-results’);
 
 // echo “<pre>”;
// print_r($achr);
$xpath = new DomXpath($doc);
$div = $xpath->query(‘//*[@class=”street-address”]’);
 for ($i = $div->length – 1; $i > -1; $i–) {
        $result[] = $div->item($i)->textContent;

    }

   // echo “<pre>”;
    //print_r($result);
    
$di = $xpath->query(‘//*[@class=”business-phone phone”]’);
 for ($i = $di->length – 1; $i > -1; $i–) {
        $re[] = $di->item($i)->textContent;
    }

   // echo “<pre>”;
    //print_r($re);    
    
$ca = $xpath->query(‘//*[@class=”city-state”]’);
 for ($i = $ca->length – 1; $i > -1; $i–) {
        $c[] = $ca->item($i)->textContent;
    }

   // echo “<pre>”;
   // print_r($c);
    
$tit = $xpath->query(‘//*[@class=”srp-business-name”]’);
 for ($i = $tit->length – 1; $i > -1; $i–) {
        $t[] = $tit->item($i)->textContent;
    }

    //echo “<pre>”;
    print_r($t);
    
$conn=mysql_connect(“localhost”,”root”,””);
mysql_select_db(‘scrapping’,$conn);
for($i=0;$i<count($t); $i++)
{
$query=”Insert into search_results values(”,'”.addslashes($result[$i]).”‘,'”.addslashes($re[$i]).”‘,'”.addslashes($c[$i]).”‘,'”.addslashes($t[$i]).”‘)”;
mysql_query($query) or die(mysql_error());
}
    exit();
?>

Html 2 PDF cakephp

1.Download html2pdf
2.Include the files in controller method as below

<?php
$this->autoRender = false;
$this->layout=false;
$id = $this->params[‘pass’][0];
App::import(“Model”,”Order”);
$this->Order=new Order();
$order = $this->Order->find(‘first’, array(‘conditions’=>array(‘Order.id’=>$id)));
$view = new View($this, false);
$view->set(“order”,$order);
$view->viewPath = ‘orders’;
$view_output = $view->render(‘admin_order_pdf’);

App::import(‘Vendor’,’xtcpdf’);
$tcpdf = new XTCPDF();
$textfont = ‘freesans’; // looks better, finer, and more condensed than ‘dejavusans’

$tcpdf->SetAuthor(“Worldwide Stone Order Details”);
$tcpdf->SetAutoPageBreak( false );
$tcpdf->setHeaderFont(array($textfont,”,10));
$tcpdf->xheadercolor = array(150,250,170);
$tcpdf->xheadertext = ”;
$tcpdf->xfootertext = ‘Copyright © %d Worldwide Stone Order Details. All rights reserved.’;
$html_for_pdf=$view_output;
$tcpdf->AddPage();
//$tcpdf->Cell(0,14, $html_for_pdf, 0,1,’L’);

$html_for_pdf=$view_output;
$tcpdf->writeHTMLCell($w=0, $h=0, $x=”, $y=”, $html_for_pdf, $border=0, $ln=1, $fill=0, $reseth=true, $align=”, $autopadding=true);
ob_start();
$tcpdf->Output(‘test.pdf’, ‘D’);
ob_end_flush();
/****************************************************************************/
if (!$id)
{
$this->Session->setFlash(‘Sorry, there was no property ID submitted.’);
$this->redirect(array(‘action’=>’admin_order_list’), null, true);
}
Configure::write(‘debug’,0); // Otherwise we cannot use this method while developing

$id = intval($id);

// $property = $this->__admin_order_view($id); // here the data is pulled from the database and set for the view
$property = $this->Order->find(‘first’,array(‘conditions’=>array(‘Order.id’=>$id)));
// pr($property);

if (empty($property))
{
$this->Session->setFlash(‘Sorry, there is no property with the submitted ID.’);
$this->redirect(array(‘action’=>’admin_order_list’), null, true);
}

$this->layout = ‘pdf’; //this will use the pdf.ctp layout
$this->render();
}

?>
3.The view file to be converted to pdf is

<div><h4><b>Order Details Of <?php echo $order[‘Order’][‘order’];?></b></h4></div>
<h2>Order Information</h2>
<img width=”100″ height=”100″ src=”<?php echo BASE_URL.”img/fancy_images/”.$order[‘Template’][‘image’];?>” />
<p>Order Name :<span><?php echo $order[‘Order’][‘order’];?><br/>
Template Name : <?php echo $order[‘Template’][‘title’];?><br/>
Material  Name : <?php echo $order[‘Material’][‘name’];?><br/>
Material  Id : <?php echo $order[‘Material’][‘material_id’];?>
</p>
<h2>Template Information</h2>
<p>Template Title : <?php if( $order[‘Template’][‘title’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘title’];}?><br/>
Template Price : <?php if( $order[‘Template’][‘price’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘price’];}?><br/>
Template Price : <?php if( $order[‘Template’][‘price’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘price’];}?><br/>
Template Created : <?php if( $order[‘Template’][‘created’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘created’];}?><br/>
Die Id  : <?php if( $order[‘Template’][‘die_id’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘die_id’];}?><br/>
Die Styles : <?php if( $order[‘Template’][‘styles’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘styles’];}?><br/>
Die Styles Codes : <?php if( $order[‘Template’][‘style_codes’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘style_codes’];}?><br/>
Die Styles Complexity : <?php if( $order[‘Template’][‘complexity’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘complexity’];}?><br/>
Die Styles Instances : <?php if( $order[‘Template’][‘instances’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘instances’];}?><br/>
Die Styles Special Die Aplicables :<?php if( $order[‘Template’][‘sd_aplicables’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘sd_aplicables’];}?><br/>
Original width(Single/Double) : <?php if( $order[‘Template’][‘original_width_sds’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘original_width_sds’];}?><br/>
Width in Inches :<?php if( $order[‘Template’][‘width_ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘width_ins’];}?><br/>
Height in Inches  :<?php if( $order[‘Template’][‘height_ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘height_ins’];}?><br/>
Estimated Area in inches  :<?php if( $order[‘Template’][‘estimated_area_ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘estimated_area_ins’];}?><br/>
Estimated Area in cms :  :<?php if( $order[‘Template’][‘estimated_area_cms’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘estimated_area_cms’];}?><br/>
Thickness 4ins :<?php if( $order[‘Template’][‘thickness_4ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘thickness_4ins’];}?><br/>
Thickness 6ins :<?php if( $order[‘Template’][‘thickness_6ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘thickness_6ins’];}?><br/>
Thickness 8ins :<?php if( $order[‘Template’][‘thickness_8ins’] ==”){echo “Not Available”;}else {echo $order[‘Template’][‘thickness_8ins’];}?>
</p>

<h2>Material Information</h2>

<p>Material Name :<span><?php echo $order[‘Material’][‘name’];?><br/>
Material Price :<span><?php echo $order[‘Material’][‘price’];?><br/>
Material Country Of Origin :<?php if( $order[‘Material’][‘country_of_rigin’] ==”){echo “Not Available”;}else {echo $order[‘Material’][‘country_of_rigin’];}?><br/>
Material Stone Type : <?php echo $order[‘Material’][‘stone_type’];?><br/>
Material Color Tone :<?php echo $order[‘Material’][‘color_tone_#’];?><br/>
Material Instance : <?php echo $order[‘Material’][‘instance’];?><br/>
Material Color Code :<?php echo $order[‘Order’][‘order’];?><br/>
</p>

Validation in cakephp

class User extends AppModel {
var $name = ‘User’;
var $validate = array(
‘firstname’ => array(
‘rule’ => ‘/^[a-zA-Z]{0,}$/i’,
‘message’ => “Enter Firstname (Alphabets Only).”,
‘allowEmpty’=>false
),
‘lastname’ => array(
‘rule’ => ‘/^[a-zA-Z]{0,}$/i’,
‘message’ => “Enter Lastname (Alphabets Only).”,
‘allowEmpty’=>false
),

‘start_date’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Select Date of Hire.”
),
‘primary_phone_number’ => array(
‘rule’ => array(‘phone’, ‘/^\s*[0-9\-\+\s]+$/i’, ‘us’),
‘message’ => “Enter valid phone number.”,
‘allowEmpty’=>true
),
‘s_designation’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter Designation.”
),
‘p_designation’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter Designation.”
),
‘secondary_phone_number’ => array(
‘rule’ => array(‘phone’, ‘/^\s*[0-9\-\+\s]+$/i’, ‘us’),
‘message’ => “Enter valid phone number.”,
‘allowEmpty’=>true
),
‘primary_email’ => array(
‘ruleName2’ => array(
‘rule’ => array(’email’),
‘message’ => “Enter valid email address.”
),

),
‘secondary_email’ => array(
‘ruleName2’ => array(
‘rule’ => array(’email’),
‘message’ => “Enter valid email address.”,
‘allowEmpty’=>true
),

),
‘password’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter your password.”
),
‘password1’ => array(
‘loginRule-1’ => array(
‘rule’    => ‘notEmpty’,
‘message’ => ‘Enter your password.’,
),
‘rule2’ => array(
‘rule’ => array(‘minLength’, 5),
‘message’ => ‘Minimum length of 5 characters.’
),
‘rule3’ => array(
‘rule’ => array(‘maxLength’, 25),
‘message’ => ‘Maximum length of 25 characters.’
)
),

’email’ => array(
‘notEmpty’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter your email.”,
‘last’ => true
),
‘ruleName2’ => array(
‘rule’ => array(’email’),
‘message’ => “Enter valid email address.”
),

),
‘old_password’ => array(
‘ruleName’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter old password.”,
‘last’ => true
),
‘ruleName2’ => array(
‘rule’ => array(‘isOldPasswordExists’),
‘message’ => “Old password doesnot exists.”
)
),
‘confirm_password’ => array(
‘ruleName’ => array(
‘rule’ => ‘notEmpty’,
‘message’ => “Enter confirm password.”
),
‘ruleName2’ => array(
‘rule’ => array(‘matchPasswords’,’password’),
‘message’ => “Passwords do not match.”
)
),
‘dob’ => array(
‘rule’ => array(‘validDOB’),
‘message’ => “Date of birth should not be greater than today’s date.”
)
);

and in controller
$this->Model->save($this->data, array(‘validate’=>false));

Paypal direct payment method

1.firstly you shoud have paypal account to use the payment method
2.Then you need credentials of credit card from the customer so that he can pay
$api_version = ‘98.0’;
$api_endpoint = ‘https://api-3t.sandbox.paypal.com/nvp&#8217;;
$api_username = ”;
$api_password = ”;
$api_signature =”;
// Store request params in an array
$request_params = array
(
‘METHOD’ => ‘DoDirectPayment’,
‘USER’ => $api_username,
‘PWD’ => $api_password,
‘SIGNATURE’ => $api_signature,
‘VERSION’ => $api_version,
‘PAYMENTACTION’ => ‘Sale’,
‘IPADDRESS’ => $_SERVER[‘REMOTE_ADDR’],
‘CREDITCARDTYPE’ => $ctype,
‘ACCT’ => $cnumber,
‘EXPDATE’ => $e,
‘CVV2’ => $cvv,
‘FIRSTNAME’ => $fname,
‘LASTNAME’ => ‘Testerson’,
‘STREET’ => ‘707 W. Bay Drive’,
‘CITY’ => ‘Largo’,
‘STATE’ => ‘FL’,
‘COUNTRYCODE’ => ‘US’,
‘ZIP’ => $zip,
‘AMT’ => $amount,
‘CURRENCYCODE’ => ‘USD’,
‘DESC’ => ‘Testing Payments Pro’
);

// Loop through $request_params array to generate the NVP string.
$nvp_string = ”;
foreach($request_params as $var=>$val)
{
$nvp_string .= ‘&’.$var.’=’.urlencode($val);
}

// Send NVP string to PayPal and store response
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 100);
curl_setopt($curl, CURLOPT_URL, $api_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);

$result = curl_exec($curl);

curl_close($curl);

//echo $result.'<br /><br/>’;
parse_str($result, $arr); //this will convert the response url to array
$data[‘result’]=$arr;
$data[‘file’]=”/thankyou”;
//$this->load->view(‘content’,$data);

}