SHARE
TWEET

Untitled

a guest Aug 25th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class ItemShell extends AppShell
  4. {
  5.  
  6.     public $uses = array('Item', 'Exchange', 'User', 'Business');
  7.  
  8.     public function count() {
  9.         if(!isset($this->args[0])) {
  10.             return $this->out('Exchange shortname is required as 1st Param');
  11.         }
  12.         $exchange = $this->Exchange->find('first', array(
  13.             'conditions'    => ['Exchange.shortname' => $this->args[0]]
  14.         ));
  15.         if(empty($exchange)) {
  16.             return $this->out("Exchange shortname not found");
  17.         }
  18.         $count = $this->Item->find('count', array(
  19.             'conditions'    => array('Item.exchange_id' => $exchange['Exchange']['id'])
  20.         ));
  21.         return $this->out("Total " . $count . " items found.");
  22.     }
  23.  
  24.     public function clear() {
  25.         if(!isset($this->args[0])) {
  26.             return $this->out('Exchange shortname is required as 1st Param');
  27.         }
  28.         $exchange = $this->Exchange->find('first', array(
  29.             'conditions'    => ['Exchange.shortname' => $this->args[0]]
  30.         ));
  31.         if(empty($exchange)) {
  32.             return $this->out("Exchange shortname not found");
  33.         }
  34.         $this->out( "Are you sure you want to do this?  Type 'yes' to continue: ");
  35.         $handle = fopen ("php://stdin","r");
  36.         $line = fgets($handle);
  37.         if(trim($line) != 'yes'){
  38.             return $this->out( "ABORTING!\n");
  39.         }
  40.         fclose($handle);
  41.         $this->Item->deleteAll(['Item.exchange_id' => $exchange['Exchange']['id']]);
  42.         $this->out( "Records deleted successfully.\n");
  43.     }
  44.  
  45.     public function delete() {
  46.         if(!isset($this->args[0])) {
  47.             return $this->out('Exchange shortname is required as 1st Param');
  48.         }
  49.         $exchange = $this->Exchange->find('first', array(
  50.             'conditions'    => ['Exchange.shortname' => $this->args[0]]
  51.         ));
  52.         if(empty($exchange)) {
  53.             return $this->out("Exchange shortname not found");
  54.         }
  55.         if(!isset($this->args[1])) {
  56.             return $this->out('Item id required as 2nd param');
  57.         }
  58.         $this->out( "Are you sure you want to do this?  Type 'yes' to continue: ");
  59.         $handle = fopen ("php://stdin","r");
  60.         $line = fgets($handle);
  61.         if(trim($line) != 'yes'){
  62.             $this->out( "ABORTING!\n");
  63.             exit;
  64.         }
  65.         fclose($handle);
  66.  
  67.         $item = $this->Item->find('count', array(
  68.             "conditions"    => array("Item.id"   => $this->args[1], "Item.exchange_id"  => $exchange['Exchange']['id'])
  69.         ));
  70.         if($item > 0) {
  71.             $this->Item->delete($this->args[1]);
  72.  
  73.             $this->out( "Record deleted successfully.\n");
  74.         } else {
  75.             $this->out( "Item not found.\n");
  76.         }
  77.     }
  78.  
  79.     public function create() {
  80.         if(!isset($this->args[0])) {
  81.             return $this->out('Exchange shortname is required as 1st Param');
  82.         }
  83.         $exchange = $this->Exchange->find('first', array(
  84.             'conditions'    => ['Exchange.shortname' => $this->args[0]]
  85.         ));
  86.         if(empty($exchange)) {
  87.             return $this->out("Exchange shortname not found");
  88.         }
  89.         if(!isset($this->args[1])) {
  90.             return $this->out('No of records required as 2nd param');
  91.         }
  92.         $business = $this->Business->find('first', array(
  93.             'conditions'    => array('Business.exchange_id' => $exchange['Exchange']['id'])
  94.         ));
  95.         $lipsum = new joshtronic\LoremIpsum();
  96.  
  97.         $i = 1;
  98.         $items = array();
  99.         $date = new DateTime();
  100.         $date->add(new DateInterval("P1Y"));
  101.         while($i <= intval($this->args[1])) {
  102.             $item = array(
  103.                 'type'          => "Item",
  104.                 'exchange_id'   => $exchange['Exchange']['id'],
  105.                 'name'          => $lipsum->words(rand(1, 5)),
  106.                 'description'   => $lipsum->paragraphs(rand(1, 4)),
  107.                 'business_id'   => $business['Business']['id'],
  108.                 'barter'        => rand(100,500),
  109.                 'inventory'     => rand(200,800),
  110.                 'stop'          => $date->format('Y-m-d')
  111.             );
  112.             array_push($items, $item);
  113.             $i++;
  114.         }
  115.  
  116.         if(count($items) > 0) {
  117.             $resp = $this->Item->saveMany($items);
  118.  
  119.             return $this->out(count($items) . " Item(s) Saved Successfully!");
  120.         }
  121.     }
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top