public function action_delete($id = null) { if (!Sentry::user()->has_access('groups_delete')) { self::no_access(); } if (Sentry::group(intval($id))->delete()) { Session::set_flash('success', 'Group successfuly deleted'); Response::redirect('groups'); } else { Session::set_flash('error', 'Could not delete the group'); } Response::redirect('groups'); }
public function up() { \Config::load('sentry', true); try { // create groups $this->create_group('users', 1); $this->create_group('moderators', 50); $this->create_group('administrators', 100, true); // create user data $user_id = $this->create_user('admin', 'logica'); \Sentry::user($user_id)->add_to_group(\Sentry::group('administrators')->get('id')); $group_mod = \Sentry::group('moderators')->get('id'); $group_user = \Sentry::group('users')->get('id'); for ($i = 1; $i <= 100; $i++) { $user_id = $this->create_user('user' . $i, 'password'); $group = $i % 3 ? $group_mod : $group_user; $user = \Sentry::user($user_id); $user->add_to_group($group); } } catch (SentryUserException $e) { echo $e->getMessage(); return false; } }
public function action_edit($id = null) { // redirect if no right access if (!Sentry::user()->has_access('users_edit') && $this->current_user->id != $id) { Session::set_flash('error', 'You don\'t have the right to edit a user'); Response::redirect('users'); } $user = Sentry::user(intval($id)); $group = $user->groups(); View::set_global('user', $user); View::set_global('groups', Sentry::group()->all()); View::set_global('user_group', $group); // if receive a post update the user if (Input::method() == 'POST') { $val = Model_Users::validate('edit'); if (!$val->run()) { // the data are not valid Session::set_flash('error', implode(', ', $val->error())); } else { // valid data if (Input::post('email') && Input::post('group')) { // remove the user from is actual group if (!empty($group[0]['name'])) { $remove = $user->remove_from_group($group[0]['name']); if (!$remove) { Session::set_flash('error', 'Couldn\'t remove the user from his group.'); } } // set a new group for the user $add = $user->add_to_group(Input::post('group')); if (!$add) { Session::set_flash('error', 'Couldn\'t add the user to the new group.'); } // update the other informations $update = $user->update(array('email' => Input::post('email'), 'metadata' => array('first_name' => Input::post('first_name'), 'last_name' => Input::post('last_name'), 'department' => Input::post('department')))); if (!$update) { Session::set_flash('error', 'Couldn\'t update the user.'); } else { Session::set_flash('success', 'Successfully updated ' . $user->username . '.'); Response::redirect('users'); } } else { if (Input::post('new_password') || Input::post('c_new_password') || Input::post('old_password')) { if (Input::post('new_password') != Input::post('c_new_password')) { Session::set_flash('error', 'Please repeat the same password'); } else { if (Input::post('old_password') == '') { Session::set_flash('error', 'Please enter your previous password'); } else { if (!$user->check_password(Input::post('old_password'))) { Session::set_flash('error', 'Old password is incorrect'); } else { $user->change_password(Input::post('new_password'), Input::post('old_password')); Session::set_flash('success', $user->username . ' password has been successfuly changed'); } } } } } } } $this->template->h2 = $this->template->title = isset($user['username']) ? 'Editing ' . ucwords($user['username']) . '\'s profile' : 'User not found'; $this->template->js .= Asset::js(array('mylibs/jquery.validate.js', 'script.js')); $this->template->content = View::forge('users/edit'); }
public function create_user() { // Get groups $groups = \Sentry::group()->all('front'); if (\Input::post()) { // Get POST values $insert = \Input::post(); $register_type = 'register'; if (\Input::post('register')) { $register_type = $insert['register']; } $ship_to = 'billing'; if ($insert['ship'] == 'other') { $ship_to = 'shipping'; } $val = \User\Controller_Validate::forge($register_type == 'register' ? 'create' : 'guest', false, $ship_to == 'shipping' ? 'shipping' : false); if ($val->run()) { array_walk($insert, create_function('&$val', '$val = trim($val);')); try { // Generate random username $email = $insert['email']; $user_group = 3; if ($register_type == 'guest') { $username = '******' . \Str::random('numeric', 16); $insert['guest'] = 1; $random_password = '******' . \Str::random(unique); $password = $random_password; } else { $username = $email; $insert['guest'] = 0; $password = $insert['password']; } unset($insert['email'], $insert['password'], $insert['confirm_password'], $insert['user_group'], $insert['details'], $insert['save'], $insert['update']); $only_billing = array('email'); $billing_data = \Arr::filter_prefixed($insert, "billing_"); // Set shipping data to be same as billing by default if ($ship_to_billing) { foreach ($billing_data as $key => $value) { if (!in_array($key, $only_billing)) { $insert['shipping_' . $key] = $value; } } } $metadata = \Arr::remove_prefixed($insert, "billing_") + $billing_data; $table = \DB::table_prefix('users_metadata'); $columns = \DB::list_columns($table); $insert = array_intersect_key($metadata, $columns); // create the user - no activation required $vars = array('username' => $username, 'email' => $email, 'password' => $password, 'metadata' => $insert); $user_id = \Sentry::user()->create($vars); $user = \Sentry::user($user_id); // Add user to 'customer' group (id = 3) if ($user_id and $user->add_to_group($user_group)) { if ($register_type == 'account') { \Messages::success('User successfully created.'); } if ($register_type == 'guest') { \Messages::success('You register as a guest.'); } $login_column = \Config::get('sentry.login_column', 'email'); if (\Sentry::login(${$login_column}, $password, true)) { \Response::redirect(\Uri::create('order/checkout/cost')); } else { if ($register_type == 'account') { \Messages::error('There was an error while trying to create account. Please try to create new account.'); } if ($register_type == 'guest') { \Messages::error('There was an error. Please try to login with your account details.'); } } } else { // show validation errors \Messages::error('There was an error while trying to create account.'); } } catch (\Sentry\SentryException $e) { // show validation errors \Messages::error('There was an error while trying to create user.'); $errors = $e->getMessage(); \Messages::error($errors); } } else { if ($val->error() != array()) { // show validation errors \Messages::error('There was an error while trying to create user.'); foreach ($val->error() as $e) { \Messages::error($e->get_message()); } } } } }
/** * Users * * @access public * @return void */ public function action_users() { \View::set_global('title', 'Users'); // Get current user $user = \Sentry::user(); $master_user = false; if (isset($user['metadata']['master']) && $user['metadata']['master']) { $master_user = true; } if (!$master_user) { \Messages::error("You don't have permssion to view this page."); \Response::redirect(\Uri::front_create('user/account')); } $user_group = $user->groups(); $user_group = $user_group[0]; $users = \Sentry::group($user_group['id'])->users(); // Reset to empty array if there are no result found by query if (is_null($users)) { $users = array(); } // Remove current user foreach ($users as $key => $group_user) { if ($group_user['id'] == $user->id) { unset($users[$key]); } } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($users), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $users = array_slice($users, $pagination->offset, $pagination->per_page); \Theme::instance()->set_partial('content', $this->view_dir . 'users')->set('user', $user, false)->set('user_group', $user_group, false)->set('users', $users, false)->set('pagination', $pagination, false); }
<?php } ?> <?php if (in_array('user_group_admin', $options)) { ?> <div class="filter_width <?php echo $margin(); ?> "> <?php echo \Form::label('User Group', null, array('class' => 'm_r_15')); ?> <?php echo \Form::select('user_group', \Input::get('user_group', isset($values['user_group']) ? $values['user_group'] : false), array('false' => 'Select') + \User\Model_Group::fetch_pair('id', 'name', array(), false, \Sentry::group()->all('admin')), array('class' => 'select_init filter_width')); ?> </div> <?php } ?> <?php if (in_array('country', $options)) { ?> <div class="filter_width <?php echo $margin(); ?> "> <?php echo \Form::label('Country'); ?>
public function get_search_customers($group_id = false) { // Override group_id if its a search $group_id = \Input::get('user_group', $group_id); $activated = \Input::get('activated', false); if (\Input::get()) { if ($group_id && \Sentry::group_exists((int) $group_id)) { // Get only group users \View::set_global('group', \Sentry::group((int) $group_id)); $items = \Sentry::group((int) $group_id)->users(); } else { // Get all users and remove admin users from array $items = \Sentry::user()->all('front'); } } // Reset to empty array if there are no result found by query if (empty($items)) { $items = array(); } // Get user objects if (!empty($items)) { foreach ($items as $key => $item) { $items[$key] = \Sentry::user((int) $item['id']); } // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } $full_name = $item->get('metadata.first_name') . ' ' . $item->get('metadata.last_name'); $customer_id = $item->get('metadata.user_id'); if (stripos($full_name, $value) === false && stripos($customer_id, $value) === false) { unset($items[$number]); } } break; case 'email': foreach ($items as $number => $item) { if (stripos($item->email, $value) === false) { unset($items[$number]); } } break; case 'country': if ($value && $value !== 'false') { foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if (stripos($item->get('metadata.country'), $value) === false) { unset($items[$number]); } } } break; case 'postcode_from': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if ($item->get('metadata.postcode') < $value) { unset($items[$number]); } } break; case 'postcode_to': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if ($item->get('metadata.postcode') > $value) { unset($items[$number]); } } break; case 'activated': if ($value !== 'false') { foreach ($items as $number => $item) { if ($item->activated != $value) { unset($items[$number]); } } } break; } } } } // Reset to empty array if there are no result found by query if (empty($items)) { $items = array(); } return array('items' => $items); }
<?php if (!$group && empty($groups)) { ?> <div class="wide"><span class="req">Note: </span> There are no user groups yet.</div> <?php } else { ?> <ul class="treeview" id="tree"> <?php if (!empty($groups)) { foreach ($groups as $key => $group_item) { $group_item = (object) $group_item; $users = Sentry::group($group_item->id)->users(); ?> <li> <div class="radio_link_holder"> <a href="<?php echo \Uri::create('admin/user/list/' . $group_item->id); ?> " <?php echo $selected == $group_item->id ? 'class="active"' : ''; ?> > <?php echo $group_item->name; ?> <?php echo $users ? '<span class="tree_count">(' . count($users) . ')</span>' : '';
<th scope="col">Customer Groups</th> <th scope="col" class="center" style="width: 40px;">Users</th> <th scope="col" class="center" style="width: 40px;">Edit</th> <th scope="col" class="center" style="width: 40px;">Delete</th> </tr> </thead> <tbody> <?php foreach ($items as $item) { ?> <?php $item = (object) $item; ?> <?php $users = \Sentry::group($item->id)->users(); ?> <tr> <td><?php echo $item->name; ?> </td> <td class="center"> <a href="<?php echo \Uri::create('admin/user/list/' . $item->id); ?> "> <strong><?php echo count($users) == 0 ? '/' : count($users); ?> </strong>
?> </div> </div> <div class="formRow"> <label>Email</label> <div> <?php echo Form::input('email', $user_data['email'] ? $user_data['email'] : \Input::post('email')); ?> </div> </div> <div class="formRow"> <label>User Group</label> <div> <?php echo \Form::select('user_group', \Input::get('user_group', isset($user_data['user_group']) ? $user_data['user_group'] : false), \User\Model_Group::fetch_pair('id', 'name', array(), false, \Sentry::group()->all('admin')), array('style' => 'height: 28px; width:97%;')); ?> </div> </div> <div class="formRow"> <label>New Password</label> <div> <?php echo Form::password('password', \Input::post('password')); ?> </div> </div> <div class="formRow"> <label>Repeat New Password</label> <div> <?php
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 = '******' . \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); }
public function action_delete($id = false) { if (is_numeric($id)) { // Get news item to edit if ($item = \Sentry::group((int) $id)) { // Some groups are not del if ($item->deletable) { if (!$item->users()) { // Delete item try { // Delete group $item->delete(); \Messages::success('Group successfully deleted.'); } catch (\Sentry\SentryGroupException $e) { // show validation errors \Messages::error('<strong>There was an error while trying to delete group</strong>'); // Uncomment lines below to show database errors $errors = $e->getMessage(); \Messages::error($errors); } } else { // show validation errors \Messages::error('<strong>There was an error while trying to delete group</strong>'); \Messages::error('In order to delete this group first remove all members from it.'); } } else { // show validation errors \Messages::error('<strong>There was an error while trying to delete group</strong>'); \Messages::error('This group can\'t be deleted.'); } } } \Response::redirect(\Input::referrer()); }
<?php // Get all groups and remove admin groups from array $groups = \Sentry::group()->all('front'); $group = isset($group) ? $group : false; $link = isset($link) ? $link : 'update'; $selected = isset($selected) ? $selected : false; ?> <div class="side_tree_holder" style="display:none;"> <div class="tree_heading"> <h4>User Groups</h4> <div id="sidetreecontrol" class="sidetreecontrol"><a href="#">Collapse All</a><a href="#">Expand All</a></div> </div> <div class="tree_content"> <div id="sidetree"> <?php if (!$group && empty($groups)) { ?> <div class="wide"><span class="req">Note: </span> There are no user groups yet.</div> <?php } else { ?> <ul class="treeview" id="tree"> <?php if (!empty($groups)) { foreach ($groups as $key => $group_item) { $group_item = (object) $group_item; ?>
public function remove_base_rule_from_group($group_id, $rule_id) { try { $group = Sentry::group($group_id); $rule = Rule::find($rule_id); $permissions = array(); if (isset($group['permissions'])) { $permissions = json_decode($group['permissions'], true); } if (isset($permissions[$rule->rule])) { unset($permissions[$rule->rule]); DB::table('config_user_groups')->where('id', '=', $group_id)->update(array('permissions' => json_encode($permissions))); return true; } return false; } catch (Sentry\SentryException $e) { throw new Sentry\SentryException($e->getMessage); } }
public function get_search_items($group_id = false) { // Override group_id if its a search $group_id = \Input::get('user_group', $group_id); if ($group_id && \Sentry::group_exists((int) $group_id)) { // Get only group users \View::set_global('group', \Sentry::group((int) $group_id)); $items = \Sentry::group((int) $group_id)->users(); } else { $items = \Sentry::user()->all('admin'); } // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Get user objects if (!empty($items)) { foreach ($items as $key => $item) { $items[$key] = \Sentry::user((int) $item['id']); } // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } $full_name = $item->get('metadata.first_name') . ' ' . $item->get('metadata.last_name'); if (stripos($full_name, $value) === false) { unset($items[$number]); } } break; case 'email': foreach ($items as $number => $item) { if (stripos($item->email, $value) === false) { unset($items[$number]); } } break; case 'country': if ($value && $value !== 'false') { foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if (stripos($item->get('metadata.country'), $value) === false) { unset($items[$number]); } } } break; case 'postcode_from': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if ($item->get('metadata.postcode') < $value) { unset($items[$number]); } } break; case 'postcode_to': foreach ($items as $number => $item) { if (empty($item['metadata'])) { unset($items[$number]); continue; } if ($item->get('metadata.postcode') > $value) { unset($items[$number]); } } break; } } } } // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); return array('items' => $items, 'pagination' => $pagination); }
protected function master_user($user_id = false) { if (!($user = \Sentry::user((int) $user_id))) { return false; } if ($user->get('metadata.master') == 1) { return $user_id; } $user_group = $user->groups(); $user_group = $user_group[0]; $users = \Sentry::group($user_group['id'])->users(); foreach ($users as $user_item) { if ($user_tmp = \Sentry::user((int) $user_item['id'])) { if ($user_tmp->get('metadata.master') == 1) { return $user_tmp->get('id'); } } } return false; }
/** * Update product price * * @access public * @return Response */ public function action_update($id = false) { $flag = true; if (!is_numeric($id)) { \Response::redirect('admin/product/list'); } // Get product item to edit if (!($item = Model_Product::find_one_by_id($id))) { \Response::redirect('admin/product/list'); } // Redirect to attribute group if (\Input::get('attribute_group', false) === false && !empty($item->attributes)) { foreach ($item->attributes as $attr_obj) { if ($attr_obj->attribute_group_id > 0) { \Response::redirect(\Uri::create(\Uri::admin(), array(), array('attribute_group' => $attr_obj->attribute_group_id))); } } } // NRB-Gem: Comment out; Logic does not apply anymore // if(\Input::get('user_group', false) === false) // { // \Response::redirect(\Uri::create(\Uri::admin(), array(), array('user_group' => 3) + \Input::get())); // } \View::set_global('title', 'Edit Product Price'); \View::set_global('quick_menu_save', 'form.main_form'); // Update if (\Input::post()) { $post = \Input::post(); $return_to = is_numeric(\Input::post('return_to')) ? '#' . \Input::post('return_to') : ''; //$val = Model_Attribute::validate(); try { if (isset($post[$post['price_type']])) { foreach ($post[$post['price_type']] as $key => $price) { if ($update_price = Model_Attribute_Price::find_one_by_id($key)) { if (isset($post['active'][$key])) { $update_price->active = $post['active'][$key]; } if (isset($post['product_group_discount_id'][$key])) { $update_price->product_group_discount_id = $post['product_group_discount_id'][$key]; } $update_price->save(); } } } $multiple_images = array(); $image_new = false; foreach ($post['attributes'] as $key => $attribute) { if (!empty($post['delete_items'])) { $delete_items = explode(',', $post['delete_items']); $result1 = \DB::delete('product_attributes')->where('id', 'in', $delete_items)->execute(); $result2 = \DB::delete('product_attribute_price')->where('product_attribute_id', 'in', $delete_items)->execute(); } // Check existing product attribute group $existing_attribute_group = Model_Attribute::find(function ($query) use($post) { $query->where('product_id', $post['product_id']); $query->and_where_open(); $query->where('attribute_group_id', null, \DB::expr('IS NOT NULL')); // $query->and_where('attribute_group_id', '!=' , 0); $query->and_where('attribute_group_id', '!=', $post['attribute_group_id']); $query->and_where_close(); }); //if($existing_attribute_group && $post['attribute_group_id'] != 0) if ($existing_attribute_group) { foreach ($existing_attribute_group as $item) { $delete_attribute = $item->id; $item->delete(); $attribute_option = Model_Attribute_Price::find_one_by_product_attribute_id($delete_attribute); if ($attribute_option) { $attribute_option->delete(); } } } // Update if (isset($post['update_items'][$key])) { // Lightmedia - michael: check if product attribute code is exists on the product if ($this->check_attr_code_exists($id, $post['update_items'][$key], $post['product_code'][$key])) { $flag = false; \Messages::error($post['product_code'][$key] . ' code already used'); continue; } $update = Model_Attribute::find_one_by_id($post['update_items'][$key]); if ($update) { $item_images = array(); $data = array('product_id' => $post['product_id'], 'attribute_group_id' => $post['attribute_group_id'], 'attributes' => $attribute, 'product_code' => $post['product_code'][$key], 'retail_price' => $post['retail_price'][$key], 'sale_price' => $post['sale_price'][$key], 'stock_quantity' => $post['stock_quantity'][$key], 'active' => isset($post['active']) && $post['active'][$key] ? $post['active'][$key] : $post['active_new'][$key]); // Default radio $data['default'] = 0; if (isset($post['default']) && $post['default'] == $key) { $this->reset_default($item->id); $data['default'] = 1; } $update->set($data); $update->save(); $attr_id = $update->id; // Get combinations for multiple images if (isset($post['apply_image']) && isset($post['action'])) { if (in_array($attr_id, $post['action'])) { $multiple_images['action'][$attr_id] = $attr_id; } } if (isset($_FILES['image_new_' . $attr_id])) { // Upload image and display errors if there are any $image = $this->upload_image('image'); if ($image['errors'] && $image['exists']) { \Messages::error('<strong>There was an error while trying to upload product attribute image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } // if($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) if ($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) { /** IMAGES **/ // Get all alt texts to update if there is no image change foreach (\Arr::filter_prefixed(\Input::post(), 'alt_text_') as $image_id => $alt_text) { if (strpos($image_id, 'new_') === false) { $item_images[$image_id] = array('id' => $image_id, 'data' => array('alt_text' => \Input::post('alt_text_' . $image_id, ''))); if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } } } // Save images if new files are submitted if (isset($this->_image_data) && $image['exists'] !== false) { foreach ($this->_image_data as $image_data) { $cover_count = count($update->images); if (strpos($image_data['field'], 'new_') === false) { // Update existing image if (str_replace('image_', '', $image_data['field']) != 0) { $image_id = (int) str_replace('image_', '', $image_data['field']); $cover_count--; $item_images[$image_id] = array('id' => $image_id, 'data' => array('content_id' => $attr_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_' . $image_id, ''))); //$this->delete_image(\Input::post('image_db_' . $image_id, '')); } if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } } else { // Save new image $image_tmp = str_replace('image_new_', '', $image_data['field']); $image_new = $item_images[0] = array('id' => 0, 'data' => array('content_id' => $attr_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } // Multiple images if (isset($post['apply_image']) && isset($post['action']) && false) { foreach ($post['action'] as $action_value) { if ($action_value == $attr_id) { continue; } $item_images = array(); if ($action_value > 0) { $image_new[0] = array('id' => 0, 'data' => array('content_id' => $action_value, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); } if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } } } } } } /** END OF IMAGES **/ } } if (isset($post['active_new'][$key])) { //NRB-Gem: insert attributes to existing Products added to Pricing Groups $this->add_attribute_price($post, array('attr_id' => $attr_id, 'key' => $key)); } } } else { if ($this->check_attr_code_exists($post['product_id'], false, $post['product_code_new'][$key])) { $flag = false; \Messages::error($post['product_code_new'][$key] . ' code already used'); continue; } $item_images = array(); $insert = Model_Attribute::forge(array('product_id' => $post['product_id'], 'attribute_group_id' => $post['attribute_group_id'], 'attributes' => $attribute, 'product_code' => $post['product_code_new'][$key], 'retail_price' => $post['retail_price_new'][$key], 'active' => $post['active_new'][$key], 'sale_price' => $post['sale_price_new'][$key], 'stock_quantity' => $post['stock_quantity'][$key])); // Default radio $insert->default = 0; if (isset($post['default']) && $post['default'] == $key) { $this->reset_default($item->id); $insert->default = 1; } $insert->save(); $attr_id = $insert->id; // Get combinations for multiple images if (isset($post['apply_image']) && isset($post['action_new'][$key]) && $post['action_new'][$key] == 1) { $multiple_images['action_new'][$attr_id] = $attr_id; } if (isset($_FILES['_image_new_' . $key])) { // Upload image and display errors if there are any $image = $this->upload_image('image'); if ($image['errors'] && $image['exists']) { \Messages::error('<strong>There was an error while trying to upload product attribute image</strong>'); foreach ($image['errors'] as $error) { \Messages::error($error); } } // if($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false) && empty($item->images))) if ($image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) { /** IMAGES **/ // Save images if new files are submitted if (isset($this->_image_data) && $image['exists'] !== false) { foreach ($this->_image_data as $image_data) { $cover_count = 0; // Save new image $image_tmp = str_replace('_image_new_', '', $image_data['field']); $image_new = $item_images[0] = array('id' => 0, 'data' => array('content_id' => $attr_id, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('_alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } // Multiple images if (isset($post['apply_image']) && isset($post['action_new']) && false) { foreach ($post['action_new'] as $action_key => $action_value) { if ($action_value == $attr_id) { continue; } $item_images = array(); if ($action_value > 0) { $image_new[0] = array('id' => 0, 'data' => array('content_id' => $action_value, 'image' => $image_data['saved_as'], 'alt_text' => \Input::post('alt_text_new_' . $image_tmp, ''), 'cover' => $cover_count == 0 ? 1 : 0, 'sort' => $cover_count + 1)); } if (!empty($item_images)) { Model_Attribute::bind_images($item_images); } } } } } /** END OF IMAGES **/ } } if (isset($post[$post['price_type'] . '_new'][$key])) { //NRB-Gem: insert attributes to existing Products added to Pricing Groups $this->add_attribute_price($post, array('attr_id' => $attr_id, 'key' => $key)); } } } // Update/insert multiple images if (!empty($multiple_images) && !empty($image_new) && isset($image_new['data'])) { $content_id = $image_new['data']['content_id']; if (isset($multiple_images['action']) && !empty($multiple_images['action'])) { foreach ($multiple_images['action'] as $value) { $item_images = array(); if ($content_id == $value) { continue; } $image_new['data']['content_id'] = $value; $item_images[0] = $image_new; Model_Attribute::bind_images($item_images); } } if (isset($multiple_images['action_new']) && !empty($multiple_images['action_new'])) { foreach ($multiple_images['action_new'] as $value) { $item_images = array(); if ($content_id == $value) { continue; } $image_new['data']['content_id'] = $value; $item_images[0] = $image_new; Model_Attribute::bind_images($item_images); } } } if ($flag) { \Messages::success('Product successfully updated.'); } } catch (\Database_Exception $e) { // show validation errors \Messages::error('<strong>There was an error while trying to update product attributes</strong>'); // Uncomment lines below to show database errors // $errors = $e->getMessage(); // \Messages::error($errors); \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to); } \Response::redirect(\Uri::create(\Uri::admin('current'), array(), \Input::get()) . $return_to); } // Get current product $product = Model_Product::find_one_by_id($id); // Create array for attribute group select $attribute_groups_select = \Attribute\Model_Attribute_Group::fetch_pair('id', 'title', array(), array('0' => '- No Attributes -')); // Set user group for lazy load if (\Input::get('user_group')) { Model_Attribute::set_user_group(); } // Set tier price group for lazy load if (\Input::get('tier_price')) { Model_Attribute::set_tier_price(); } $param = \Input::get(); // Find attributes for current product $items_db = Model_Attribute::find(function ($query) use($id, $param) { $query->select('product_attributes.*'); $query->join('attribute_groups', 'LEFT')->on('attribute_groups.id', '=', 'product_attributes.attribute_group_id'); $query->where('product_id', $id); if (isset($param['attribute_group']) && is_numeric($param['attribute_group'])) { $query->and_where('product_attributes.attribute_group_id', $param['attribute_group']); } $query->order_by('attribute_groups.sort', 'asc'); }, 'id'); // Decode attribute json $combinations_db_arr = $this->decode_attribute_json($items_db); // Find current attribute group $attribute_group = array(); if (\Input::get('attribute_group')) { $attribute_group = \Attribute\Model_Attribute_Group::find_one_by_id(\Input::get('attribute_group')); } // Set vars $combinations = array(); $combinations_data = array(); $combinations_tmp = array(); $attributes_order = array(); $compared = array(); $update_items = array(); $delete_items = ''; // Create attribute combinations if ($attribute_group && $attribute_group->attributes) { $i = 0; foreach ($attribute_group->attributes as $attribute) { $attributes_order[] = $attribute->id; if ($attribute->options) { foreach ($attribute->options as $option) { $combinations[$i][] = $attribute->title . ': ' . $option->title; //$combinations[$i][] = array($attribute->title => $option->title); $combinations_tmp[$i][] = array($attribute->id => $option->id); } } $i++; } // Create combinations from current attributes $combinations = $this->combos($combinations); $combinations_tmp = $this->combos($combinations_tmp); // Sort product atributes from database if ($combinations_db_arr) { $sorted_db_arr = $this->sort_array(array_keys($attribute_group->attributes), $combinations_db_arr); if ($sorted_db_arr) { $compared = $this->compare_array($combinations_tmp, $sorted_db_arr, $combinations_db_arr); } } } // Something crazy if (!empty($compared) && !empty($items_db)) { if (!empty($compared['not_exist_in1'])) { $delete_items = array_keys($compared['not_exist_in1']); $delete_items = implode(',', $delete_items); } if (!empty($compared['exist_id'])) { $update_items = $compared['exist_id']; } } // Sort array asc $combinations_sorted = $this->sort_array_asc($combinations_tmp); foreach ($combinations_sorted as $key => $value) { $combinations_data[$key] = json_encode($value); } // Select user groups $user_groups = \Sentry::group()->all('front'); if (!empty($user_groups)) { $user_groups = \Model_Base::fetch_pair('id', 'name', array(), false, $user_groups); } else { $user_groups = array(); } $price_select = array(3 => 'Sale Price'); $price_select_fields = array(3 => 'sale_price'); // Set default user price type or use selected $price_field = 'sale_price'; if (isset($price_select_fields[\Input::get('special')])) { $price_field = $price_select_fields[\Input::get('special')]; } $listing = true; if (\Input::get('special') == 2 && !\Input::get('tier_price')) { $listing = false; } // Product pricing group if ($item->pricing_group) { $pricing_group = $item->pricing_group->id; } else { $pricing_group = false; } // NRB-Gem: removed logic // Find and select tier price // $tier_price = array(); // if(\Input::get('user_group') && is_numeric(\Input::get('user_group')) && $pricing_group) // { // $tier_price = \Product\Model_Group_Discounts::find(array( // 'where' => array( // 'user_group_id' => \Input::get('user_group'), // 'product_group_id' => $pricing_group, // ), // 'order_by' => array( // 'discount' => 'asc' // ) // )); // } // Reset to empty array if there are no result found by query if (is_null($combinations)) { $combinations = array(); } if (is_null($combinations_data)) { $combinations_data = array(); } $number_of_combinations = count($combinations); // Pagination per page default $per_page = \Input::get('per_page', 'all') == 'all' ? 9999 : \Input::get('per_page'); $show_all = $per_page > $number_of_combinations ? true : false; // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($combinations), 'per_page' => $per_page, 'uri_segment' => null)); // Remove unwanted items, and show only required ones $combinations = array_slice($combinations, $pagination->offset, $pagination->per_page, true); $combinations_data = array_slice($combinations_data, $pagination->offset, $pagination->per_page, true); \Theme::instance()->set_partial('content', $this->view_dir . 'attributes')->set('product', $product)->set('price_select', $price_select, false)->set('combinations', $combinations, false)->set('combinations_data', $combinations_data, false)->set('attribute_groups_select', $attribute_groups_select, false)->set('delete_items', $delete_items, false)->set('update_items', $update_items, false)->set('items_db', $items_db, false)->set('price_field', $price_field)->set('listing', $listing)->set('pagination', $pagination, false)->set('number_of_combinations', $number_of_combinations)->set('show_all', $show_all); }
} else { ?> <td><?php echo $group['name']; ?> </td> <?php } ?> <?php $right = Model_Group::access($group['id']); ?> <td><?php echo count(Sentry::group($group['id'])->users()); ?> </td> <td><?php echo round(count(isset(json_decode($right[0]['permissions'])->superuser) ? new SplFixedArray($total) : (array) json_decode($right[0]['permissions'])) * 100 / $total, 0, PHP_ROUND_HALF_EVEN); ?> /100</td> <?php if (Sentry::user()->has_access('groups_edit') || Sentry::user()->has_access('groups_delete')) { ?> <td><?php if (Sentry::user()->has_access('groups_edit')) { echo Html::anchor('groups/edit/' . $group['id'], 'Edit'); } if (Sentry::user()->has_access('groups_delete')) {