示例#1
0
 /**
  * Gets the metadata.last_modified field, creating it if it doesn't exist
  */
 public function lastModified()
 {
     if (!$this->get('metadata.last_modified')) {
         $this->set('metadata.last_modified', \Dsc\Mongo\Metastamp::getDate('now'));
     }
     return $this->get('metadata.last_modified');
 }
示例#2
0
 public function emailSubmit()
 {
     $f3 = \Base::instance();
     $email = $this->input->get('new_email', null, 'string');
     $data = array('change_email' => array('email' => $email, 'token' => (string) new \MongoId(), 'created' => \Dsc\Mongo\Metastamp::getDate('now')));
     // TODO Validate that it is an email
     if (empty($email)) {
         \Dsc\System::addMessage('Invalid Email.', 'error');
         $f3->reroute('/user/change-email');
         return;
     }
     // Verify whether or not the email address is already registered
     if (\Users\Models\Users::emailExists($email)) {
         \Dsc\System::addMessage('Email address already registered.', 'error');
         $f3->reroute('/user/change-email');
         return;
     }
     $user = $this->getIdentity();
     $user->bind($data);
     try {
         $user->save()->sendEmailChangeEmailConfirmation();
     } catch (\Exception $e) {
         \Dsc\System::addMessage('Email submission failed.', 'error');
         \Dsc\System::addMessage($e->getMessage(), 'error');
         $f3->reroute('/user/change-email');
         return;
     }
     \Dsc\System::addMessage('Email change request submitted.  Please check your inbox for a verification email from us.');
     $f3->reroute('/user/change-email/verify');
 }
示例#3
0
 public function addNote($message, $save = false)
 {
     $identity = \Dsc\System::instance()->get('auth')->getIdentity();
     array_unshift($this->notes, array('created' => \Dsc\Mongo\Metastamp::getDate('now'), 'created_by' => $identity->fullName(), 'created_by_id' => $identity->id, 'message' => $message));
     if ($save) {
         return $this->save();
     }
     return $this;
 }
示例#4
0
文件: Logs.php 项目: dioscouri/f3-lib
 protected function beforeSave()
 {
     if (empty($this->created)) {
         $this->created = \Dsc\Mongo\Metastamp::getDate('now');
     }
     if (empty($this->{'created.microtime'})) {
         $this->set('created.microtime', microtime(true));
     }
     return parent::beforeSave();
 }
 /**
  * This method returns update clause which will be later on passed to collection
  *
  * @param 	$data		Data from request
  * @param	$params		Arrays with possible additional params (for different modes of updater
  *
  * @return	Based on mode of updater, either update clause or updated document
  */
 public function getUpdateClause($data, $params = array())
 {
     // check required parameters
     if (!$this->checkParams($params)) {
         return null;
     }
     $dataset = $params['dataset'];
     $name_with_idx = $this->getNameWithIdx();
     // cleans up input
     $tz = $dataset[$name_with_idx . '_tz'];
     $time = "00:00";
     $date = (string) preg_replace('/[^0-9\\-]/i', '', $data);
     $date = ltrim($date, '.');
     if ($this->isShowTime()) {
         if (empty($dataset[$name_with_idx . '_time'])) {
             return null;
         }
         preg_match('/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/', $dataset[$name_with_idx . '_time'], $match);
         if (is_array($match) && !empty($match)) {
             $time = (string) $match[0];
         }
     }
     $res_updates = array();
     $timestamp = strtotime($date . ' ' . $time . ':00');
     $final_date = $timestamp;
     if ($tz == 'gtm') {
         $final_date += Date('Z', $timestamp);
     }
     if ($this->metastamp) {
         $final_date = \Dsc\Mongo\Metastamp::getDate($final_date);
     }
     $res_updates[$this->attribute->getAttributeCollection()] = $final_date;
     if (count($this->attribute_datetime) > 0) {
         if (!empty($this->attribute_datetime['date'])) {
             $res_updates[$this->attribute_datetime['date']] = $date;
         }
         if (!empty($this->attribute_datetime['time'])) {
             $res_updates[$this->attribute_datetime['time']] = $time;
         }
     }
     switch ($this->attribute->getUpdaterMode()) {
         case 0:
             return array('$set', $res_updates);
         case 1:
             $doc = $params['document'];
             foreach ($res_updates as $key => $value) {
                 $doc[$key] = $value;
             }
             return $doc;
     }
 }
示例#6
0
 /**
  * 
  * @param array $resource
  * @param string $action
  * @param string $diff
  * @param array $actor
  * @param string $message
  * @param string $priority
  * @param string $category
  * 
  * @return \Dsc\Mongo\Collections\AuditLogs
  */
 public static function add($resource, $action, $diff, $actor, $message = null, $priority = 'INFO', $category = 'Audit')
 {
     $model = new static();
     $model->created = \Dsc\Mongo\Metastamp::getDate('now');
     $model->set('created.microtime', microtime(true));
     $model->priority = $priority;
     $model->category = $category;
     $model->message = $message;
     $model->resource_type = !empty($resource['type']) ? $resource['type'] : null;
     $model->resource_id = !empty($resource['id']) ? $resource['id'] : null;
     $model->action = $action;
     $model->diff = $diff;
     $model->actor_type = !empty($actor['type']) ? $actor['type'] : null;
     $model->actor_id = !empty($actor['id']) ? $actor['id'] : null;
     $model->actor_name = !empty($actor['name']) ? $actor['name'] : null;
     $model->store();
     return $model;
 }
示例#7
0
 /**
  * Step 2 == POST target for the forgot password form.
  * Looks up the provided email address, and if it's found, sends an email and displays a static message.
  * If it's not found, queues a message and reroutes to the forgot-password route
  */
 public function passwordFindEmail($f3)
 {
     $email = trim(strtolower($this->input->get('email', null, 'string')));
     if (empty($email)) {
         \Dsc\System::addMessage('Please provide an email address.', 'error');
         $f3->reroute('/user/forgot-password');
     }
     $user = (new \Users\Models\Users())->setState('filter.email', $email)->getItem();
     if (empty($user->id)) {
         \Dsc\System::addMessage('That email address does not exist in our system.', 'error');
         $f3->reroute('/user/forgot-password');
         return;
     }
     // create a forgot_password array, with a MongoId and a metastamp
     $user->forgot_password = array('token' => (string) new \MongoId(), 'created' => \Dsc\Mongo\Metastamp::getDate('now'));
     $user->save()->sendEmailResetPassword();
     \Dsc\System::addMessage('Email sent');
     $f3->reroute('/user/forgot-password/email');
 }
示例#8
0
 /**
  * Adds an item to the queue
  *
  * Throws an Exception
  * 
  * @param unknown $task
  * @param unknown $parameters
  * @param unknown $options
  * @return \Dsc\Mongo\Collections\QueueTasks
  */
 public static function add($task, $parameters = array(), $options = array())
 {
     $options = $options + array('title' => null, 'when' => null, 'priority' => 0, 'batch' => null);
     $model = new static();
     $model->created = \Dsc\Mongo\Metastamp::getDate('now');
     $model->title = $options['title'];
     $model->task = $task;
     $model->parameters = $parameters;
     $model->when = $options['when'] ? (int) $options['when'] : time();
     $model->priority = (int) $options['priority'];
     $model->batch = $options['batch'];
     $sparse_options = $options;
     unset($sparse_options['title']);
     unset($sparse_options['when']);
     unset($sparse_options['priority']);
     unset($sparse_options['batch']);
     $model->options = $sparse_options;
     $model->validate()->store();
     return $model;
 }
示例#9
0
 /**
  * Method to check that publication fields are set to some defaults.
  * You will need to call this in your model's beforeSave() method.
  *
  * @return \Dsc\Traits\Models\Publishable
  */
 protected function publishableBeforeSave()
 {
     if (!empty($this->{'publication.start_date'})) {
         $string = $this->{'publication.start_date'};
         if (!empty($this->{'publication.start_time'})) {
             $string .= ' ' . $this->{'publication.start_time'};
         }
         $this->{'publication.start'} = \Dsc\Mongo\Metastamp::getDate(trim($string));
     } else {
         $this->{'publication.start'} = \Dsc\Mongo\Metastamp::getDate('now');
     }
     if (empty($this->{'publication.end_date'})) {
         unset($this->{'publication.end'});
     } elseif (!empty($this->{'publication.end_date'})) {
         $string = $this->{'publication.end_date'};
         if (!empty($this->{'publication.end_time'})) {
             $string .= ' ' . $this->{'publication.end_time'};
         }
         $this->{'publication.end'} = \Dsc\Mongo\Metastamp::getDate(trim($string));
     }
     return $this;
 }
示例#10
0
 protected function beforeSaveNotes()
 {
     if (empty($this->notes)) {
         $this->notes = array();
     }
     $last_idx_old = 0;
     $toDelete = array();
     $old_notes = array();
     $user = array();
     $identity = \Dsc\System::instance()->get('auth')->getIdentity();
     if (empty($identity->id)) {
         $user = array('id' => new \MongoId(), 'name' => 'Guest');
     } else {
         $user = array('id' => $identity->id, 'name' => $identity->fullName());
     }
     $notes = $this->notes;
     ksort($notes);
     // we need to sort array by its keys to prevent mongo saving it as object
     if (strlen((string) $this->id)) {
         // editing
         $old_document = (new static())->setState('filter.id', $this->id)->getItem();
         $old_notes = (array) $old_document->notes;
         $last_idx_old = count($old_notes);
         $toDeleteStr = trim((string) $this->__notesToDelete);
         if (strlen($toDeleteStr)) {
             $toDelete = (array) explode(',', $toDeleteStr);
         }
         for ($i = 0, $c = count($toDelete); $i < $c; $i++) {
             unset($notes[(int) $toDelete[$i]]);
         }
     }
     // no note added so just dont do anything
     if (empty($notes) && empty($old_notes)) {
         return;
     }
     // in case user will try to manually delete some notes
     $last_idx = end(array_keys($notes));
     $modified = array();
     // array with modified notes
     for ($i = 0; $i <= $last_idx; $i++) {
         // skip deleted notes
         if (in_array($i, $toDelete) !== false) {
             // if this was erased by accident, copy its copy from old document
             continue;
         }
         $old_note = array('title' => '', 'description' => '');
         if (!empty($old_notes[$i])) {
             $old_note = $old_notes[$i];
         }
         if (!empty($notes[$i])) {
         }
         $notes[$i]['title'] = trim($notes[$i]['title']);
         $notes[$i]['description'] = trim($notes[$i]['description']);
         if ($i < $last_idx_old) {
             if (empty($notes[$i])) {
                 $notes[$i] = $old_note;
                 continue;
             }
             if (empty($notes[$i]['description'])) {
                 // skip empty notes
                 continue;
             }
             if ($old_note['title'] != $notes[$i]['title'] || $old_note['description'] != $notes[$i]['description']) {
                 $notes[$i]['user'] = $user;
                 $notes[$i]['datetime'] = \Dsc\Mongo\Metastamp::getDate('now');
                 $modified[] = $notes[$i];
                 unset($notes[$i]);
             } else {
                 $notes[$i] = $old_note;
             }
         } else {
             $notes[$i]['user'] = $user;
             $notes[$i]['datetime'] = \Dsc\Mongo\Metastamp::getDate('now');
         }
     }
     for ($i = 0, $c = count($modified); $i < $c; $i++) {
         $notes[] = $modified[$i];
     }
     $this->notes = array_values($notes);
 }
示例#11
0
 /**
  * Revoke an issued credit, updating the user's balance appropriately
  *
  * @return \Shop\Models\Credits
  */
 public function revoke()
 {
     if ($this->credit_issued_to_user) {
         $user = $this->user();
         if (empty($user->id)) {
             throw new \Exception('Invalid User');
         }
         $this->balance_before = (double) $user->{'shop.credits.balance'};
         $this->balance_after = $this->balance_before - (double) $this->amount;
         // Add to the history
         $this->history[] = array('created' => \Dsc\Mongo\Metastamp::getDate('now'), 'subject' => \Dsc\System::instance()->get('auth')->getIdentity()->fullName(), 'verb' => 'revoked', 'object' => (double) $this->amount);
         $user->{'shop.credits.balance'} = (double) $this->balance_after;
         $user->save();
         $this->credit_issued_to_user = false;
         $this->save();
     }
     return $this;
 }
示例#12
0
 protected function beforeSave()
 {
     if (!empty($this->{'display.stickers'}) && !is_array($this->{'display.stickers'})) {
         $this->{'display.stickers'} = trim($this->{'display.stickers'});
         if (!empty($this->{'display.stickers'})) {
             $this->{'display.stickers'} = \Base::instance()->split((string) $this->{'display.stickers'});
         }
     } elseif (empty($this->{'display.stickers'}) && !is_array($this->{'display.stickers'})) {
         $this->{'display.stickers'} = array();
     }
     $this->set('shipping.enabled', (bool) $this->get('shipping.enabled'));
     $this->set('taxes.enabled', (bool) $this->get('taxes.enabled'));
     $this->set('prices.default', (double) $this->get('prices.default'));
     $this->set('prices.list', (double) $this->get('prices.list'));
     if ($this->get('prices.wholesale')) {
         $this->set('prices.wholesale', (double) $this->get('prices.wholesale'));
     }
     $this->set('policies.track_inventory', (bool) $this->get('policies.track_inventory'));
     $this->attributes_count = count($this->attributes);
     $this->variants_count = count($this->variants);
     $this->inventory_count = 0;
     array_walk($this->variants, function (&$item, $key) {
         $item['quantity'] = (int) $item['quantity'];
         $item['price'] = (double) $item['price'];
         if (empty($item['quantity']) && !empty($this->{'policies.track_inventory'})) {
             $item['enabled'] = 0;
         }
         if (!empty($item['quantity'])) {
             $this->inventory_count += (int) $item['quantity'];
         }
     });
     if (!empty($this->{'prices.special'}) && is_array($this->{'prices.special'})) {
         // Compress the array to just the values, then sort them by sort order
         $special_prices = array_filter(array_values($this->{'prices.special'}));
         usort($special_prices, function ($a, $b) {
             return $a['ordering'] - $b['ordering'];
         });
         array_walk($special_prices, function (&$item, $key) {
             if ($item['ordering'] != $key + 1) {
                 $item['ordering'] = $key + 1;
             }
             if (!empty($item['start_date'])) {
                 $string = $item['start_date'];
                 if (!empty($item['start_time'])) {
                     $string .= ' ' . $item['start_time'];
                 }
                 $item['start'] = \Dsc\Mongo\Metastamp::getDate(trim($string));
             } else {
                 $item['start'] = \Dsc\Mongo\Metastamp::getDate('now');
             }
             if (empty($item['end_date'])) {
                 unset($item['end']);
             } elseif (!empty($item['end_date'])) {
                 $string = $item['end_date'];
                 if (!empty($item['end_time'])) {
                     $string .= ' ' . $item['end_time'];
                 }
                 $item['end'] = \Dsc\Mongo\Metastamp::getDate(trim($string));
             }
         });
         $this->{'prices.special'} = $special_prices;
     }
     if (!$this->variantsInStock()) {
         $this->{'publication.status'} = 'unpublished';
     }
     return parent::beforeSave();
 }
示例#13
0
 public static function handleError()
 {
     $response = new \stdClass();
     $response->action = null;
     $response->redirect_route = null;
     $response->callable_handle = null;
     $response->html = null;
     $f3 = \Base::instance();
     if ($f3->get('ERROR.code') == '404') {
         $model = new \Redirect\Admin\Models\Routes();
         // lets find a proper redirection, if exists
         $path = substr($model->inputFilter()->clean($f3->hive()['PATH'], 'string'), 1);
         $redirect = null;
         $route = $model->setState('filter.url.alias', $path)->getItem();
         if (!empty($route->id)) {
             // count the number of times a redirect is hit and track the date of the last hit
             $route->hits = (int) $route->hits + 1;
             $route->last_hit = \Dsc\Mongo\Metastamp::getDate('now');
             $route->store();
             $redirect = trim($route->{'url.redirect'});
         } else {
             // TODO add a config option to disable this
             $new_route = new \Redirect\Admin\Models\Routes();
             $new_route->set('url.alias', $path);
             $new_route->hits = 1;
             $new_route->last_hit = \Dsc\Mongo\Metastamp::getDate('now');
             try {
                 $new_route->save();
             } catch (\Exception $e) {
                 // couldn't save for some reason
                 // add it to Logger?
             }
         }
         // don't redirect if the path and redirect are the same to prevent recursion
         if (!empty($redirect) && $path != $redirect) {
             // if the string doesn't begin with /, make sure it does
             if (strpos($redirect, '/') !== 0) {
                 $redirect = '/' . $redirect;
             }
             $response->action = 'redirect';
             $response->redirect_route = $redirect;
         } else {
             // use default error route specified by user in Redirect::Settings
             $settings = \Redirect\Admin\Models\Settings::fetch();
             $default_redirect = $settings->{'general.default_error_404'};
             if (!empty($default_redirect) && $path != $default_redirect) {
                 // if the string doesn't begin with /, make sure it does
                 if (strpos($default_redirect, '/') !== 0) {
                     $default_redirect = '/' . $default_redirect;
                 }
                 $response->action = 'redirect';
                 $response->redirect_route = $default_redirect;
             } else {
                 // let f3 default error handler handle it since the user didn't specify a default 404 redirect
                 $response->action = false;
             }
         }
     } else {
         // let f3 default error handler handle all other error types
         $response->action = false;
     }
     // clear any system error messages if the response is redirect
     // so that the user doesn't get displayed "invalid item" notices
     if ($response->action == 'redirect') {
         $messages = \Dsc\System::instance()->getMessages();
     }
     return $response;
 }
示例#14
0
 /**
  * Revoke an issued commission, updating the user's balance appropriately
  *
  * @return \Affiliates\Models\Commissions
  */
 public function revoke()
 {
     if ($this->issued) {
         $user = $this->affiliate();
         if (empty($user->id)) {
             throw new \Exception('Invalid Affiliate');
         }
         $this->balance_before = (double) $user->{'affiliate.commission.balance'};
         $this->balance_after = $this->balance_before - (double) $this->amount;
         // Add to the history
         $this->history[] = array('created' => \Dsc\Mongo\Metastamp::getDate('now'), 'subject' => \Dsc\System::instance()->get('auth')->getIdentity()->fullName(), 'verb' => 'revoked', 'object' => (double) $this->amount);
         $user->{'affiliate.commission.balance'} = (double) $this->balance_after;
         $user->save();
         // Undo all the actions,
         foreach ($this->actions as $key => $action) {
             switch ($action['type']) {
                 case "shop.credit":
                     if (class_exists('\\Shop\\Models\\Orders') && !empty($action['issued']) && !empty($action['credit_id'])) {
                         // Revoke the credit
                         try {
                             $credit = (new \Shop\Models\Credits())->setState('filter.id', $action['credit_id'])->getItem();
                             if (!empty($credit->id)) {
                                 $credit->revoke();
                             }
                             $this->actions[$key]['issued'] = false;
                         } catch (\Exception $e) {
                             $this->log('Could not revoke credit: ' . (string) $e);
                         }
                     }
                     break;
                 default:
                     break;
             }
         }
         // trigger Listener event so they can handle them
         // listeners can loop through the actions themselves and evaluate $action['type'] and $action['issued']
         $this->__revoke_event = \Dsc\System::instance()->trigger('onAffiliatesRevokeCommission', array('commission' => $this));
         $this->issued = false;
         $this->save();
     }
     return $this;
 }
示例#15
0
 public function sendEmailShareGiftCard($data)
 {
     \Base::instance()->set('data', $data);
     \Base::instance()->set('giftcard', $this);
     \Base::instance()->set('settings', \Shop\Models\Settings::fetch());
     $html = \Dsc\System::instance()->get('theme')->renderView('Shop/Views::emails_html/share_gift_card.php');
     $text = \Dsc\System::instance()->get('theme')->renderView('Shop/Views::emails_text/share_gift_card.php');
     $subject = 'A gift card from ' . $data['sender_name'];
     $this->__sendEmailShareGiftCard = \Dsc\System::instance()->get('mailer')->send($data['recipient_email'], $subject, array($html, $text));
     // Add the entry to the gift card's internal history log
     $this->history[] = array('created' => \Dsc\Mongo\Metastamp::getDate('now'), 'subject' => $data['sender_name'] . ' (' . $data['sender_email'] . ')', 'verb' => 'shared', 'object' => $data['recipient_name'] . ' (' . $data['recipient_email'] . ')');
     return $this->save();
 }
示例#16
0
 /**
  * 
  * @return \Users\Models\Users
  */
 public function setLastVisit()
 {
     $this->set('last_visit', \Dsc\Mongo\Metastamp::getDate('now'));
     return $this->save();
 }