Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ItemShell extends AppShell
- {
- public $uses = array('Item', 'Exchange', 'User', 'Business');
- public function count() {
- if(!isset($this->args[0])) {
- return $this->out('Exchange shortname is required as 1st Param');
- }
- $exchange = $this->Exchange->find('first', array(
- 'conditions' => ['Exchange.shortname' => $this->args[0]]
- ));
- if(empty($exchange)) {
- return $this->out("Exchange shortname not found");
- }
- $count = $this->Item->find('count', array(
- 'conditions' => array('Item.exchange_id' => $exchange['Exchange']['id'])
- ));
- return $this->out("Total " . $count . " items found.");
- }
- public function clear() {
- if(!isset($this->args[0])) {
- return $this->out('Exchange shortname is required as 1st Param');
- }
- $exchange = $this->Exchange->find('first', array(
- 'conditions' => ['Exchange.shortname' => $this->args[0]]
- ));
- if(empty($exchange)) {
- return $this->out("Exchange shortname not found");
- }
- $this->out( "Are you sure you want to do this? Type 'yes' to continue: ");
- $handle = fopen ("php://stdin","r");
- $line = fgets($handle);
- if(trim($line) != 'yes'){
- return $this->out( "ABORTING!\n");
- }
- fclose($handle);
- $this->Item->deleteAll(['Item.exchange_id' => $exchange['Exchange']['id']]);
- $this->out( "Records deleted successfully.\n");
- }
- public function delete() {
- if(!isset($this->args[0])) {
- return $this->out('Exchange shortname is required as 1st Param');
- }
- $exchange = $this->Exchange->find('first', array(
- 'conditions' => ['Exchange.shortname' => $this->args[0]]
- ));
- if(empty($exchange)) {
- return $this->out("Exchange shortname not found");
- }
- if(!isset($this->args[1])) {
- return $this->out('Item id required as 2nd param');
- }
- $this->out( "Are you sure you want to do this? Type 'yes' to continue: ");
- $handle = fopen ("php://stdin","r");
- $line = fgets($handle);
- if(trim($line) != 'yes'){
- $this->out( "ABORTING!\n");
- exit;
- }
- fclose($handle);
- $item = $this->Item->find('count', array(
- "conditions" => array("Item.id" => $this->args[1], "Item.exchange_id" => $exchange['Exchange']['id'])
- ));
- if($item > 0) {
- $this->Item->delete($this->args[1]);
- $this->out( "Record deleted successfully.\n");
- } else {
- $this->out( "Item not found.\n");
- }
- }
- public function create() {
- if(!isset($this->args[0])) {
- return $this->out('Exchange shortname is required as 1st Param');
- }
- $exchange = $this->Exchange->find('first', array(
- 'conditions' => ['Exchange.shortname' => $this->args[0]]
- ));
- if(empty($exchange)) {
- return $this->out("Exchange shortname not found");
- }
- if(!isset($this->args[1])) {
- return $this->out('No of records required as 2nd param');
- }
- $business = $this->Business->find('first', array(
- 'conditions' => array('Business.exchange_id' => $exchange['Exchange']['id'])
- ));
- $lipsum = new joshtronic\LoremIpsum();
- $i = 1;
- $items = array();
- $date = new DateTime();
- $date->add(new DateInterval("P1Y"));
- while($i <= intval($this->args[1])) {
- $item = array(
- 'type' => "Item",
- 'exchange_id' => $exchange['Exchange']['id'],
- 'name' => $lipsum->words(rand(1, 5)),
- 'description' => $lipsum->paragraphs(rand(1, 4)),
- 'business_id' => $business['Business']['id'],
- 'barter' => rand(100,500),
- 'inventory' => rand(200,800),
- 'stop' => $date->format('Y-m-d')
- );
- array_push($items, $item);
- $i++;
- }
- if(count($items) > 0) {
- $resp = $this->Item->saveMany($items);
- return $this->out(count($items) . " Item(s) Saved Successfully!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement