myshop.php 16.9 KB
Newer Older
1 2 3
<?php

function cart_myshop_load(){
4 5
	Zotlabs\Extend\Hook::register('cart_main_myshop','addon/cart/myshop.php','cart_myshop_main',1,99);
	Zotlabs\Extend\Hook::register('cart_aside_filter','addon/cart/myshop.php','cart_myshop_aside',1,99);
6 7
	Zotlabs\Extend\Hook::register('cart_myshop_order','addon/cart/myshop.php','cart_myshop_order',1,99);
	Zotlabs\Extend\Hook::register('cart_post_myshop_item_fulfill','addon/cart/myshop.php','cart_myshop_item_fulfill',1,99);
8
	Zotlabs\Extend\Hook::register('cart_post_myshop_item_cancel','addon/cart/myshop.php','cart_myshop_item_cancel',1,99);
9 10
	Zotlabs\Extend\Hook::register('cart_post_myshop_clear_item_exception','addon/cart/myshop.php','cart_myshop_clear_item_exception',1,99);
	Zotlabs\Extend\Hook::register('cart_post_myshop_add_itemnote','addon/cart/myshop.php','cart_myshop_add_itemnote',1,99);
11
	Zotlabs\Extend\Hook::register('cart_post_myshop_add_ordernote','addon/cart/myshop.php','cart_myshop_add_ordernote',1,99);
12 13 14
	Zotlabs\Extend\Hook::register('cart_myshop_allorders','addon/cart/myshop.php','cart_myshop_allorders',1,99);
	Zotlabs\Extend\Hook::register('cart_myshop_openorders','addon/cart/myshop.php','cart_myshop_openorders',1,99);
	Zotlabs\Extend\Hook::register('cart_myshop_closedorders','addon/cart/myshop.php','cart_myshop_closedorders',1,99);
15
	Zotlabs\Extend\Hook::register('cart_post_myshop_order_markpaid','addon/cart/myshop.php','cart_myshop_order_markpaid',1,99);
DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
16
	Zotlabs\Extend\Hook::register('cart_orderpaid','addon/cart/myshop.php','cart_myshop_orderpaid_hook',1,10000);
17 18
  Zotlabs\Extend\Hook::register('cart_after_fulfill','addon/cart/cart.php','cart_myshop_itemfulfilled_hook',1,30000);
	Zotlabs\Extend\Hook::register('cart_after_cancel','addon/cart/cart.php','cart_myshop_cancelled_hook',1,30000);
19 20 21
}

function cart_myshop_unload(){
22 23
	Zotlabs\Extend\Hook::unregister('cart_main_myshop','addon/cart/myshop.php','cart_myshop_main');
	Zotlabs\Extend\Hook::unregister('cart_aside_filter','addon/cart/myshop.php','cart_myshop_aside');
24 25
	Zotlabs\Extend\Hook::unregister('cart_myshop_order','addon/cart/myshop.php','cart_myshop_order');
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_item_fulfill','addon/cart/myshop.php','cart_myshop_item_fulfill');
26
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_item_cancel','addon/cart/myshop.php','cart_myshop_item_cancel',1,99);
27 28
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_clear_item_exception','addon/cart/myshop.php','cart_myshop_clear_item_exception');
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_add_itemnote','addon/cart/myshop.php','cart_myshop_add_itemnote');
29 30 31 32 33
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_add_ordernote','addon/cart/myshop.php','cart_myshop_add_ordernote');
	Zotlabs\Extend\Hook::unregister('cart_myshop_allorders','addon/cart/myshop.php','cart_myshop_allorders');
	Zotlabs\Extend\Hook::unregister('cart_myshop_openorders','addon/cart/myshop.php','cart_myshop_openorders');
	Zotlabs\Extend\Hook::unregister('cart_myshop_closedorders','addon/cart/myshop.php','cart_myshop_closedorders');
	Zotlabs\Extend\Hook::unregister('cart_post_myshop_order_markpaid','addon/cart/myshop.php','cart_myshop_order_markpaid');
DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
34
	Zotlabs\Extend\Hook::unregister('cart_orderpaid','addon/cart/myshop.php','cart_myshop_orderpaid_hook',1,10000);
35 36
  Zotlabs\Extend\Hook::unregister('cart_after_fulfill','addon/cart/cart.php','cart_myshop_itemfulfilled_hook',1,30000);
	Zotlabs\Extend\Hook::unregister('cart_after_cancel','addon/cart/cart.php','cart_myshop_cancelled_hook',1,30000);
37 38
}

39
function cart_myshop_main (&$pagecontent) {
40

41 42
	$sellernick = argv(1);

43 44 45 46 47
        if (argv(2)!='myshop') {
		notice( t('Access Denied.') . EOL);
		goaway('/' . argv(0) . '/' . argv(1));
        }

48 49 50 51 52 53 54 55 56 57 58 59 60
	$seller = channelx_by_nick($sellernick);

	if(! $seller) {
				notice( t('Invalid channel') . EOL);
				goaway('/' . argv(0));
	}

	$observer_hash = get_observer_hash();

	$is_seller = ((local_channel()) && (local_channel() == \App::$profile['profile_uid']) ? true : false);


	$urlroot = '/' . argv(0) . '/' . argv(1) . '/myshop';
61 62 63 64 65 66
        $rendered = '';

        if (argc() > 3) {
           $hookname=preg_replace('/[^a-zA-Z0-9\_]/','',argv(3));
  	   call_hooks('cart_myshop_'.$hookname,$rendered);
        }
67 68

	if ($rendered == '') {
69
	   $rendered = cart_myshop_menu();
70
	}
71
        $templatevalues=Array('content'=>$rendered);
72 73 74 75 76 77
	$template = get_markup_template('myshop.tpl','addon/cart/');
	$pagecontent = replace_macros($template, $templatevalues);

	return ($pagecontent);
}

78
function cart_myshop_menu() {
79
	$urlroot = '/' . argv(0) . '/' . argv(1) . '/myshop';
80
	$openorders=cart_myshop_get_openorders(null,10000,0);
81
	$ordercount = cart_myshop_get_ordercount();
DM42's avatar
DM42 committed
82 83
	//$allorders=cart_myshop_get_allorders(null,10000,0);
	//$closedorders=cart_myshop_get_closedorders(null,10000,0);
84
        $rendered = '';
85
	$rendered .= "<a href='".$urlroot."'>Home</a><BR />";
86 87
	//$rendered .= "<a href='".$urlroot."/openorders'>Open Orders (".count($openorders).")</a><BR />";
	//$rendered .= "<a href='".$urlroot."/closedorders'>Closed Orders (".count($closedorders).")</a><BR />";
88
	$rendered .= "<a href='".$urlroot."/allorders'>All Orders (" . $ordercount . ")</a><BR />";
89
	call_hooks('cart_myshop_menufilter',$rendered);
90
        return $rendered;
91 92 93 94 95 96 97 98 99 100 101
}

function cart_myshop_openorders (&$pagecontent) {
  $pagecontent.="<h1>OPEN ORDERS</h1>";
}

function cart_myshop_closedorders (&$pagecontent) {
	$pagecontent.="<h1>CLOSED ORDERS</h1>";
}

function cart_myshop_allorders (&$pagecontent) {
102 103 104 105 106 107 108 109 110 111 112 113 114
/*
  myshop_orderlist.tpl variables
    $orders - results of cart_myshop_get_(.*)orders
*/
  $templatevalues=Array();
  $templatevalues["urlprefix"]="/".argv(0)."/".argv(1)."/myshop/order";
  $templatevalues["orders"] = cart_myshop_get_allorders(null,100000,0);
  $templatevalues["debug"] = print_r($templatevalues,true);
  $templateinfo = array('name'=>'myshop_orderlist.tpl','path'=>'addon/cart/');
  call_hooks('cart_filter_myshop_orderlist_template',$templateinfo);
  $template = get_markup_template($templateinfo['name'],$templateinfo['path']);
  $rendered = replace_macros($template, $templatevalues);
  $pagecontent = $rendered;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

function cart_myshop_order(&$pagecontent) {
  $orderhash = argv(4);
  $orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
  $channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
		return "<h1>".t("Order Not Found")."</h1>";
	}
  $permission=Array();
	$permissions['manualfilfillment_permitted']=true;
	call_hooks('cart_myshop_order_permissions',$permissions);
	$templatevalues=$order;
	$templatevalues['permissions']=$permissions;
131
	$templatevalues["security_token"]=get_form_security_token();
132 133 134 135

	$templateinfo = array('name'=>'myshop_order.tpl','path'=>'addon/cart/');
	call_hooks('cart_filter_myshop_order',$templateinfo);
	$template = get_markup_template($templateinfo['name'],$templateinfo['path']);
DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
136 137
        $templatevalues['added_display']=Array("order"=>$order,"content"=>"");
        call_hooks('cart_addons_myshop_order_display',$templatevalues['added_display']);
138 139 140 141
	//HOOK: cart_post_myshop_order
	$rendered = replace_macros($template, $templatevalues);
	$pagecontent = $rendered;
}
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
function cart_myshop_order_markpaid () {
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}
	$itemid = preg_replace('/[^0-9]/','',$_POST["itemid"]);
	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
		notice (t("Access Denied"));
		return;
	}
        $hookinfo=Array();
        $hookinfo["order"]=$order;
	cart_do_orderpaid ($hookinfo);
	if (isset($hookinfo["error"])) {
	        $order_meta=cart_getorder_meta ($orderhash);
	        $order_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Error marking order paid: ".$hookinfo["error"];
	        cart_updateorder_meta($order_meta,$orderhash);
		notice (t($hookinfo["error"]));
		return;
	}

DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
168 169 170 171 172

}

function cart_myshop_orderpaid_hook (&$hookdata) {
        $orderhash=$hookdata["order"]["order_hash"];
173 174 175 176
	$order_meta=cart_getorder_meta ($orderhash);
	$order_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Marked Paid";
	cart_updateorder_meta($order_meta,$orderhash);
}
177 178

function cart_myshop_item_fulfill () {
179 180 181 182
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}
183 184 185 186 187 188 189
	$itemid = preg_replace('/[^0-9]/','',$_POST["itemid"]);
	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
190
		notice (t("Access Denied"));
191 192 193 194 195 196 197 198
		return;
	}
	foreach ($order["items"] as $item) {
		if ($item["id"]==$itemid) {
			$itemtofulfill=$itemid;
		}
	}
	if (!$itemtofulfill) {
199
		notice (t("Invalid Item"));
200 201
		return;
	}
202
	$itemtofulfill=Array('order_hash'=>$orderhash,'id'=>$itemid);
203 204 205

	cart_do_fulfillitem ($itemtofulfill);
	if (isset($itemtofulfill["error"])) {
206
		notice (t($itemtofulfill["error"]));
207 208
		return;
	}
209
}
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
function cart_myshop_item_cancel () {
        notice("Cancel Item".EOL);
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}
	$itemid = preg_replace('/[^0-9]/','',$_POST["itemid"]);
	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
		notice (t("Access Denied"));
		return;
	}
	foreach ($order["items"] as $item) {
		if ($item["id"]==$itemid) {
			$itemtocancel=$itemid;
		}
	}
	if (!$itemtocancel) {
		notice (t("Invalid Item"));
		return;
	}
	$itemtocancel=Array('order_hash'=>$orderhash,'id'=>$itemid);
        logger("Call do_cancelitem",LOGGER_DEBUG);
	cart_do_cancelitem ($itemtocancel);
	if (isset($itemtocancel["error"])) {
		notice (t($itemtocancel["error"]));
		return;
	}
DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
243 244 245 246
}

function cart_myshop_itemfulfilled_hook (&$hookdata) {
        $orderhash=$hookdata["item"]["order_hash"];
247
	$item_meta=cart_getitem_meta ($itemid,$orderhash);
248
	$item_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Manual Fulfillment";
249 250 251
	cart_updateitem_meta($itemid,$item_meta,$orderhash);
}

252 253 254 255 256 257 258
function cart_myshop_itemcancelled_hook (&$hookdata) {
        $orderhash=$hookdata["item"]["order_hash"];
	$item_meta=cart_getitem_meta ($itemid,$orderhash);
	$item_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Manual Cancellation";
	cart_updateitem_meta($itemid,$item_meta,$orderhash);
}

259
function cart_myshop_clear_item_exception () {
260 261 262 263 264
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}

265 266 267 268 269 270 271
	$itemid = preg_replace('/[^0-9]/','',$_POST["itemid"]);
	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
272
		notice (t("Access Denied"));
273 274 275 276 277 278 279 280 281
		return;
	}
	$itemtoclear=null;
	foreach ($order["items"] as $item) {
		if ($item["id"]==$itemid) {
			$itemtoclear=$itemid;
		}
	}
	if (!$itemtoclear) {
282
		notice (t("Invalid Item"));
283 284 285
		return;
	}

286
	$r=q("update cart_orderitems set item_exception = 0 where order_hash = '%s' and id = %d",
287 288 289 290 291 292 293 294
			dbesc($orderhash),intval($itemid));

	$item_meta=cart_getitem_meta ($itemid,$orderhash);
	$item_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Exception Cleared";
	cart_updateitem_meta($itemid,$item_meta,$orderhash);
	return;
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
function cart_myshop_add_ordernote () {
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}

	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
		notice (t("Access Denied"));
		return;
	}

        $order_meta=cart_getorder_meta ($orderhash);
	$order_meta["notes"][]=date("Y-m-d h:i:sa T - ").filter_var($_POST["notetext"], FILTER_SANITIZE_STRING);
	cart_updateorder_meta($order_meta,$orderhash);
	return;
}

317
function cart_myshop_add_itemnote () {
318 319 320 321 322
	if (!check_form_security_token()) {
		notice (check_form_security_std_err_msg());
		return;
	}

323 324 325 326 327 328 329
	$itemid = preg_replace('/[^0-9]/','',$_POST["itemid"]);
	$orderhash = argv(4);
	$orderhash = preg_replace('/[^a-z0-9]/','',$orderhash);
	$order = cart_loadorder($orderhash);
	$channel=\App::get_channel();
	$channel_hash=$channel["channel_hash"];
	if (!$order || $order["seller_channel"]!=$channel_hash) {
330
		notice (t("Access Denied"));
331 332 333 334 335 336 337 338
		return;
	}
	$itemtonote=null;
	foreach ($order["items"] as $item) {
		if ($item["id"]==$itemid) {
			$itemtonote=$itemid;
		}
	}
339 340
	if (!$itemtonote) {
		notice (t("Invalid Item"));
341 342 343 344
		return;
	}

  $item_meta=cart_getitem_meta ($itemid,$orderhash);
345
	$item_meta["notes"][]=date("Y-m-d h:i:sa T - ").filter_var($_POST["notetext"], FILTER_SANITIZE_STRING);
346
  if (isset($_POST["exception"])) {
347
  	$r=q("update cart_orderitems set item_exception = 1 where order_hash = '%s' and id = %d",
348 349
			dbesc($orderhash),intval($itemid));
		$item_meta["notes"][]=date("Y-m-d h:i:sa T - ")."Exception Set";
350
	}
351 352 353 354 355
	cart_updateitem_meta($itemid,$item_meta,$orderhash);
	return;
}


356
function cart_myshop_aside (&$aside) {
357 358 359
	$is_seller = ((local_channel()) && (local_channel() == \App::$profile['profile_uid']) ? true : false);

	    // Determine if the observer is the channel owner so the ACL dialog can be populated
360
        if (!$is_seller) {
361
			return $aside;
362
	}
363 364

	$rendered = '';
365
	$urlroot = '/' . argv(0) . '/' . argv(1) . '/myshop';
366
	$rendered .= "<li><a href='".$urlroot."'>Home</a></li>";
367
        //$openorders=cart_myshop_get_openorders(null,10000,0);
DM42's avatar
DM42 committed
368 369
	//$allorders=cart_myshop_get_allorders(null,10000,0);
        $ordercount=cart_myshop_get_ordercount();
370 371 372
	//$closedorders=cart_myshop_get_closedorders(null,10000,0);
        //$rendered .= "<li><a href='".$urlroot."/openorders'>Open Orders (".count($openorders).")</a></li>";
	//$rendered .= "<li><a href='".$urlroot."/closedorders'>Closed Orders (".count($closedorders).")</a></li>";
DM42's avatar
DM42 committed
373
	$rendered .= "<li><a href='".$urlroot."/allorders'>All Orders (".$ordercount.")</a></li>";
374 375 376 377 378 379 380 381
	$templatevalues["content"]=$rendered;
	$template = get_markup_template('myshop_aside.tpl','addon/cart/');
	$rendered = replace_macros($template, $templatevalues);
	$aside = $rendered . $aside;

	return ($aside);
}

DM42's avatar
DM42 committed
382 383 384 385 386 387 388 389 390 391
function cart_myshop_get_ordercount () {
  $seller_hash=get_observer_hash();
  $r=q("select count(cart_orders.order_hash) as ordercount from cart_orders where
        seller_channel = '%s'
        ",
      dbesc($seller_hash));
  if (!$r) {return 0;}
  return $r[0]["ordercount"];
}

392
function cart_myshop_get_allorders ($search=null,$limit=100000,$offset=0) {
393 394 395 396 397
/**
  * search = Array of search terms:  //NOT YET IMPLEMENTED
  *   [""]
***/
  $seller_hash=get_observer_hash();
DM42.Net (Matt Dent)'s avatar
DM42.Net (Matt Dent) committed
398
  $r=q("select distinct cart_orders.order_hash,cart_orders.id from cart_orders,cart_orderitems
399
        where cart_orders.order_hash = cart_orderitems.order_hash and
400 401
        seller_channel = '%s'
				ORDER BY cart_orders.id
402
        limit %d offset %d",
403 404
      dbesc($seller_hash),
      intval($limit), intval($offset));
405
  $orders=Array();
406 407
  if (!$r) {return Array();}
  foreach ($r as $order) {
408
    $orders[] = cart_loadorder($order["order_hash"]);
409 410 411 412
  }
  return $orders;
}

413
function cart_myshop_get_openorders ($search=null,$limit=100,$offset=1) {
414 415 416 417 418
/**
  * search = Array of search terms:
  *   [""]
***/
  $seller_hash=get_observer_hash();
419 420

/*
421 422
  $r=q("select distinct cart_orders.order_hash from cart_orders,cart_orderitems
        where cart_orders.order_hash = cart_orderitems.order_hash and
423 424
        seller_channel = '%s' and cart_orderitems.item_fulfilled is NULL
        and cart_orderitems.item_confirmed is not NULL
425
        limit %d offset %d",
426 427
      dbesc($seller_hash),
      intval($limit), intval($offset));
428
*/
429

430 431 432
  $r=q("select distinct cart_orders.order_hash from cart_orders,cart_orderitems
        where cart_orders.order_hash = cart_orderitems.order_hash and
        seller_channel = '%s' and
433 434
        cart_orderitems.item_fulfilled = 0 and
        cart_orderitems.item_confirmed = 1
435 436 437
        limit %d offset %d",
      dbesc($seller_hash),
      intval($limit), intval($offset));
438 439
  if (!$r) {return Array();}

440
  $orders=Array();
441
  foreach ($r as $order) {
442
    $orders[] = cart_loadorder($order["order_hash"]);
443 444 445 446
  }
  return $orders;
}

447
function cart_myshop_get_closedorders ($search=null,$limit=100,$offset=1) {
448 449

  $seller_hash=get_observer_hash();
450
  $r=q("select distinct order_hash from cart_orders where
451 452
        seller_channel = '%s' and
        cart_orders.order_hash not in (select order_hash from cart_orderitems
453
        where item_fulfilled = 1)
454
        limit %d offset %d",
455 456 457 458 459 460 461 462 463 464
      dbesc($seller_hash),
      intval($limit), intval($offset));

  if (!$r) {return Array();}

  foreach ($r as $order) {
    $orders[$order["order_hash"]] = cart_loadorder($order["order_hash"]);
  }
  return $orders;
}