Example #1
0
 /**
  * Remove user credits depending of order
  *
  * @access public
  * @param  object	          $order = Order object
  * @param  array of objects   $products = Products from order
  * @return void
  */
 public static function remove_credits($order = false, $products = false)
 {
     if (!is_numeric($order->user_id)) {
         return false;
     }
     // Get user to edit
     if (!\Sentry::user_exists((int) $order->user_id)) {
         return false;
     }
     $user = new \Sentry_User((int) $order->user_id);
     // Remove user credits
     $seek = $user->get('metadata.seek_credits') - ($order->seek > 0 ? 1 : 0);
     $seek >= 0 or $seek = 0;
     $careerone = $user->get('metadata.careerone_credits') - ($order->careerone > 0 ? 1 : 0);
     $careerone >= 0 or $careerone = 0;
     $update = array('metadata' => array('seek_credits' => $seek, 'careerone_credits' => $careerone));
     // Update user credits
     if ($user->update($update)) {
         return true;
     }
     return false;
 }
Example #2
0
 /**
  * Add user credits depending of order
  *
  * @access public
  * @param  object	          $order = Order object
  * @param  array of objects   $products = Products from order
  * @return void
  */
 public function add_credits($order = false, $products = false)
 {
     if (!is_numeric($order->user_id)) {
         return false;
     }
     // Get user to edit
     if (!\Sentry::user_exists((int) $order->user_id)) {
         return false;
     }
     $user = new \Sentry_User((int) $order->user_id);
     // Add user credits
     $update = array('metadata' => array('seek_credits' => $user->get('metadata.seek_credits') + $order->seek, 'careerone_credits' => $user->get('metadata.careerone_credits') + $order->careerone));
     // Update user credits
     if ($user->update($update)) {
         return true;
     }
     return false;
 }
Example #3
0
 public function action_create()
 {
     \View::set_global('title', 'Add New User');
     if (\Input::post()) {
         // Validate input parameters
         $val = \Validation::forge('admin_details_validation');
         $val->add('first_name', 'First Name')->add_rule('required')->add_rule('min_length', 2)->add_rule('max_length', 255);
         $val->add('last_name', 'Last Name')->add_rule('required')->add_rule('min_length', 2)->add_rule('max_length', 255);
         $val->add('email', 'Email')->add_rule('required')->add_rule('valid_email');
         $val->add('password', 'Password')->add_rule('min_length', 8);
         $val->add('confirm_password', 'Confirm Password')->add_rule('required_with', 'password')->add_rule('match_field', 'password');
         $val->add('username', 'Username')->add_rule('required')->add_rule('unique', array('users', 'username', $id));
         if ($val->run()) {
             // Get Input parameters
             $post_data = \Input::post();
             try {
                 $fields = array('username' => $post_data['username'], 'email' => $post_data['email'], 'password' => $post_data['password'], 'metadata' => array('first_name' => $post_data['first_name'], 'last_name' => $post_data['last_name']));
                 $user_group = $post_data['user_group'];
                 if (empty($post_data['password'])) {
                     unset($fields['password']);
                 }
                 $item = new \Sentry_User((int) $id);
                 $create = $item->create($fields);
                 $user = \Sentry::user($create);
                 // $item->remove_from_group((int)$fields['user_group']);
                 // $item->add_to_group((int)$fields['user_group']);
                 if ($create and $user->add_to_group($user_group)) {
                     // $user_groups = $item->groups();
                     // if(!empty($user_groups))
                     // {
                     //     // Remove user from all other groups...
                     //     foreach($user_groups as $value)
                     //     {
                     //         $item->remove_from_group((int)$value['id']);
                     //     }
                     // }
                     // $item = new \Sentry_User((int)$id);
                     // // ...and add it to selected one
                     // $item->add_to_group((int)$user_group);
                     \Messages::success('User Details Successfully Created.');
                     \Response::redirect(\Uri::admin('current'));
                 } else {
                     \Messages::error('There was an error while trying to update User details.');
                 }
             } catch (Sentry\SentryException $e) {
                 \Messages::error($e->get_message());
             }
         } else {
             if ($val->error() != array()) {
                 // Show validation errors
                 \Messages::error('<strong>There was an error while trying to update User details</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     \Theme::instance()->set_partial('content', $this->view_dir . 'create');
 }
Example #4
0
 public function action_update($id = false)
 {
     if (!is_numeric($id)) {
         \Response::redirect('admin/user/list');
     }
     // Get user to edit
     if (!\Sentry::user_exists((int) $id)) {
         \Response::redirect('admin/user/list');
     }
     \View::set_global('title', 'Edit User');
     // Get groups
     $groups = \Sentry::group()->all('front');
     // Update group details
     if (\Input::post('details', false)) {
         $item = new \Sentry_User((int) $id);
         $val = \User\Controller_Admin_Validate::forge('update', $item['id']);
         if ($val->run()) {
             // Get POST values
             $insert = \Input::post();
             array_walk($insert, create_function('&$val', '$val = trim($val);'));
             try {
                 // Generate random username
                 //$username 	= 'user' . \Str::random('numeric', 16);
                 $username = $insert['email'];
                 $email = $insert['email'];
                 $password = $insert['password'];
                 $user_group = $insert['user_group'];
                 $activated = $insert['activated'];
                 $email_client = $insert['email_client'];
                 $insert['guest'] = $user_group == 3 ? 1 : 0;
                 unset($insert['email'], $insert['password'], $insert['confirm_password'], $insert['user_group'], $insert['details'], $insert['save'], $insert['exit'], $insert['activated'], $insert['email_client']);
                 $only_billing = array('business_name', 'purchase_limit_value', 'purchase_limit_period', 'master', 'note', 'credit_account', 'guest');
                 // Set shipping data to be same as billing by default
                 /*foreach($insert as $key => $value)
                   {
                       if(!in_array($key, $only_billing) && strpos($key, 'shipping_') === false)
                       {
                           if(empty($insert['shipping_'.$key]))
                               $insert['shipping_'.$key] = $value;
                       }
                   }*/
                 // create the user - no activation required
                 $vars = array('username' => $username, 'email' => $email, 'password' => $password, 'metadata' => $insert, 'activated' => $activated);
                 // Send email to user with new password
                 if ($email_client == 1 && !empty($vars['password'])) {
                     $email_data = array('site_title' => \Config::get('site_title'), 'customer_identity' => ucwords($item->get('metadata.first_name') . ' ' . $item->get('metadata.last_name')), 'new_password' => $vars['password']);
                     $this->autoresponder($item, $email_data);
                 }
                 if (empty($vars['password'])) {
                     unset($vars['password']);
                 }
                 if ($item->update($vars)) {
                     //Change user group if needed
                     $user_groups = $item->groups();
                     if (!empty($user_groups)) {
                         // Remove user from all other groups...
                         foreach ($user_groups as $value) {
                             $item->remove_from_group((int) $value['id']);
                         }
                     }
                     $item = new \Sentry_User((int) $id);
                     // ...and add it to selected one
                     $item->add_to_group((int) $user_group);
                     \Messages::success('User successfully updated.');
                     \Response::redirect(\Input::post('exit', false) ? \Uri::create('admin/user/list/') : \Uri::admin('current'));
                 } else {
                     // show validation errors
                     \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 }
             } catch (\Sentry\SentryException $e) {
                 // show validation errors
                 \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 $errors = $e->getMessage();
                 \Messages::error($errors);
             }
         } else {
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>' . 'There was an error while trying to update user' . '</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
     }
     $user = new \Sentry_User((int) $id);
     // Get single user group
     $user_group = $user->groups();
     $user_group = current($user_group);
     $user->group = $user_group;
     \Theme::instance()->set_partial('content', $this->view_dir . 'update')->set('user', $user)->set('groups', $groups);
 }