<commit_before><?php

class Crunchbutton_Notification extends Cana_Table
{
	const TYPE_SMS   = 'sms';
	const TYPE_EMAIL = 'email';
	const TYPE_PHONE = 'phone';
	const TYPE_URL   = 'url';
	const TYPE_FAX   = 'fax';
	const TYPE_ADMIN = 'admin';
	const TYPE_STEALTH = 'stealth';

	public function send(Crunchbutton_Order $order) {

		$env = c::getEnv();
		if ($_SESSION['admin'] && c::admin()->testphone) {
			c::config()->twilio->testnumber = c::admin()->testphone;
		}

		$num = ($env == 'live' ? $this->value : c::config()->twilio->testnumber);
		$sms = ($env == 'live' ? $this->value : c::config()->twilio->testnumber);
		$mail = ($env == 'live' ? $this->value : '_EMAIL');
		$fax = ($env == 'live' ? $this->value : '_PHONE_');


		Log::debug( [ 'order' => $order->id_order, 'action' => 'send notification', 'value' => $this->value, 'notification type' => $this->type, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

		switch ($this->type) {

			case Crunchbutton_Notification::TYPE_STEALTH:

				$mail = new Crunchbutton_Email_Order_Stealthfax( [ 'order' => $order ] );

				$temp = tempnam('/tmp','fax');
				file_put_contents($temp, $mail->message());
				//chmod($temp, 0777);
				rename($temp, $temp.'.html');

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'phaxio';
				$log->date = date('Y-m-d H:i:s');
				$log->id_order = $order->id_order;
				$log->save();

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send fax stealth', 'fax stealth' => $fax, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$fax = new Phaxio([
					'to' => $fax,
					'file' => $temp.'.html',
					'id_notification_log' => $log->id_notification_log
				]);

				unlink($temp.'.html');

				if ($fax->success) {
					$log->remote = $fax->faxId;
					$log->status = 'queued';
					$log->save();
				} else {
					$log->status = 'error';
					$log->data = json_encode($fax);
					$log->save();
					// Send a sms informing the error
					$this->smsFaxError( $order );
				}

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					$order->queConfirmFaxWasReceived();
				}

				break;

			case 'fax':
				$mail = new Email_Order([
					'order' => $order
				]);

				$temp = tempnam('/tmp','fax');
				file_put_contents($temp, $mail->message());
				//chmod($temp, 0777);
				rename($temp, $temp.'.html');

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'phaxio';
				$log->date = date('Y-m-d H:i:s');
				$log->id_order = $order->id_order;
				$log->save();

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send fax', 'fax' => $fax, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$fax = new Phaxio([
					'to' => $fax,
					'file' => $temp.'.html',
					'id_notification_log' => $log->id_notification_log
				]);

				unlink($temp.'.html');

				if ($fax->success) {
					$log->remote = $fax->faxId;
					$log->status = 'queued';
					$log->save();
				} else {
					$log->status = 'error';
					$log->data = json_encode($fax);
					$log->save();
					// Send a sms informing the error
					$this->smsFaxError( $order );
				}

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					$order->queConfirmFaxWasReceived();
				}

				return $fax->success;

				break;

			case 'sms':

				$message = Crunchbutton_Message_Sms::greeting( $order->user()->firstName() );
				$message .= $order->message('sms');

				Crunchbutton_Message_Sms::send([
					'to' => $sms,
					'from' => 'restaurant',
					'message' => $message,
					'reason' => Crunchbutton_Message_Sms::CUSTOMER_ORDER
				]);

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					// If the restaurant has fax notification don't send the confimation now, CB should wait the fax finished #1239
					if( !$order->restaurant()->hasFaxNotification() ){
						$order->queConfirm();
					} else {
						Log::debug( [ 'order' => $order->id_order, 'action' => 'sms - restaurant has fax notification - wait the fax confirm', 'hasFaxNotification' => $order->restaurant()->hasFaxNotification(), 'type' => 'notification' ] );
					}
				}
				break;

			case 'phone':

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send call', 'num' => $num, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'twilio';
				$log->id_order = $order->id_order;
				$log->save();


				$twilio = new Services_Twilio(c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token);
				$call = $twilio->account->calls->create(
					c::config()->twilio->{$env}->outgoingRestaurant,
					'+1'.$num,
					'http://'.c::config()->host_callback.'/api/order/'.$order->id_order.'/say?id_notification='.$this->id_notification,
					[
						'StatusCallback' => 'http://'.c::config()->host_callback.'/api/notification/'.$log->id_notification_log.'/callback',
						'FallbackUrl' => c::config()->twilio->fallbackUrl
//						'IfMachine' => 'Hangup'
					]
				);

				$log->remote = $call->sid;
				$log->status = $call->status;
				$log->save();

				break;

			case 'email':

				// Log<commit_msg>partial #4500<commit_after><?php

class Crunchbutton_Notification extends Cana_Table
{
	const TYPE_SMS   = 'sms';
	const TYPE_EMAIL = 'email';
	const TYPE_PHONE = 'phone';
	const TYPE_URL   = 'url';
	const TYPE_FAX   = 'fax';
	const TYPE_ADMIN = 'admin';
	const TYPE_STEALTH = 'stealth';

	public function send(Crunchbutton_Order $order) {

		$env = c::getEnv();
		if ($_SESSION['admin'] && c::admin()->testphone) {
			c::config()->twilio->testnumber = c::admin()->testphone;
		}

		$num = ($env == 'live' ? $this->value : c::config()->twilio->testnumber);
		$sms = ($env == 'live' ? $this->value : c::config()->twilio->testnumber);
		$mail = ($env == 'live' ? $this->value : '_EMAIL');
		$fax = ($env == 'live' ? $this->value : '_PHONE_');

		Log::debug( [ 'order' => $order->id_order, 'action' => 'send notification', 'value' => $this->value, 'notification type' => $this->type, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

		switch ($this->type) {

			case Crunchbutton_Notification::TYPE_STEALTH:

				$mail = new Crunchbutton_Email_Order_Stealthfax( [ 'order' => $order ] );

				$temp = tempnam('/tmp','fax');
				file_put_contents($temp, $mail->message());
				//chmod($temp, 0777);
				rename($temp, $temp.'.html');

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'phaxio';
				$log->date = date('Y-m-d H:i:s');
				$log->id_order = $order->id_order;
				$log->save();

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send fax stealth', 'fax stealth' => $fax, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$fax = new Phaxio([
					'to' => $fax,
					'file' => $temp.'.html',
					'id_notification_log' => $log->id_notification_log
				]);

				unlink($temp.'.html');

				if ($fax->success) {
					$log->remote = $fax->faxId;
					$log->status = 'queued';
					$log->save();
				} else {
					$log->status = 'error';
					$log->data = json_encode($fax);
					$log->save();
					// Send a sms informing the error
					$this->smsFaxError( $order );
				}

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					$order->queConfirmFaxWasReceived();
				}

				break;

			case 'fax':
				$mail = new Email_Order([
					'order' => $order
				]);

				$temp = tempnam('/tmp','fax');
				file_put_contents($temp, $mail->message());
				//chmod($temp, 0777);
				rename($temp, $temp.'.html');

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'phaxio';
				$log->date = date('Y-m-d H:i:s');
				$log->id_order = $order->id_order;
				$log->save();

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send fax', 'fax' => $fax, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$fax = new Phaxio([
					'to' => $fax,
					'file' => $temp.'.html',
					'id_notification_log' => $log->id_notification_log
				]);

				unlink($temp.'.html');

				if ($fax->success) {
					$log->remote = $fax->faxId;
					$log->status = 'queued';
					$log->save();
				} else {
					$log->status = 'error';
					$log->data = json_encode($fax);
					$log->save();
					// Send a sms informing the error
					$this->smsFaxError( $order );
				}

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					$order->queConfirmFaxWasReceived();
				}

				return $fax->success;

				break;

			case 'sms':

				$message = $order->message('sms');

				Crunchbutton_Message_Sms::send([
					'to' => $sms,
					'from' => 'restaurant',
					'message' => $message,
					'reason' => Crunchbutton_Message_Sms::REASON_CUSTOMER_ORDER
				]);

				if ($order->restaurant()->confirmation && !$order->_confirm_trigger) {
					$order->_confirm_trigger = true;
					// If the restaurant has fax notification don't send the confimation now, CB should wait the fax finished #1239
					if( !$order->restaurant()->hasFaxNotification() ){
						$order->queConfirm();
					} else {
						Log::debug( [ 'order' => $order->id_order, 'action' => 'sms - restaurant has fax notification - wait the fax confirm', 'hasFaxNotification' => $order->restaurant()->hasFaxNotification(), 'type' => 'notification' ] );
					}
				}
				break;

			case 'phone':

				// Log
				Log::debug( [ 'order' => $order->id_order, 'action' => 'send call', 'num' => $num, 'host' => c::config()->host_callback, 'type' => 'notification' ]);

				$log = new Notification_Log;
				$log->id_notification = $this->id_notification;
				$log->status = 'pending';
				$log->type = 'twilio';
				$log->id_order = $order->id_order;
				$log->save();

				$twilio = new Services_Twilio(c::config()->twilio->{$env}->sid, c::config()->twilio->{$env}->token);
				$call = $twilio->account->calls->create(
					c::config()->twilio->{$env}->outgoingRestaurant,
					'+1'.$num,
					'http://'.c::config()->host_callback.'/api/order/'.$order->id_order.'/say?id_notification='.$this->id_notification,
					[
						'StatusCallback' => 'http://'.c::config()->host_callback.'/api/notification/'.$log->id_notification_log.'/callback',
						'FallbackUrl' => c::config()->twilio->fallbackUrl
//						'IfMachine' => 'Hangup'
					]
				);

				$log->remote = $call->sid;
				$log->status = $call->status;
				$log->save();

				break;

			case 'email':

				// Log
{'fraction_non_alphanumeric': 0.21713870733478577, 'fraction_numerical': 0.0027233115468409588, 'mean_word_length': 3.2438366718027734, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 313, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404827', 'n_tokens_mistral': 4805, 'n_tokens_neox': 3788, 'n_words': 701}