File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Customers/Kms/CustomerRepository.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma Mediadesign
*/
namespace KommaApp\Customers\Kms;
use Carbon\Carbon;
use Illuminate\Mail\Message;
use Illuminate\Support\Collection;
use KommaApp\Kms\Core\KmsSiteRepository;
use KommaApp\Customers\Models\Customer;
use KommaApp\Kms\SidebarListItem;
class CustomerRepository extends KmsSiteRepository
{
/**
* This method gets an Customer model or null
* When $customer is null, call newModel()
* This is Called in KmsSection@loadEntity.
*
* @param $customer Customer or null
* @return Customer
*/
public function getModel($customer)
{
//Check if customer is null, return new model
if ($customer == null) return $this->newModel();
//Set the extra form fields, eg. title, thumbnail
$this->setExtraModelFields($customer);
return $customer;
}
/**
* This method will set extra modelFields.
* eg title and thumbnail for the form.
*
* @param $model
*/
public function setExtraModelFields(&$model, $new = false)
{
//Set title
$model->title = $model->first_name . ' ' . $model->name_insertion . ' ' . $model->last_name;
//Set the thumbnail
$this->setThumbnail($model);
}
/**
* Set the thumbnail for the model.
*
* @param $model
*/
public function setThumbnail(&$model)
{
//Set the thumbnail
$model->thumbnail = ['image_url' => '/images/kms/structure/users.png'];
}
/**
* This method will generate a new Customer container.
* It is only used to create the new customer Form.
*
* @param null $siteId
* @return Customer
*/
public function newModel($siteId = null)
{
$customer = new Customer();
//Set the thumbnail
$this->setThumbnail($customer);
return $customer;
}
/**
* This method will get all the models.
* And add these to the sidebarList.
*
* @param $siteId
* @return sidebarList
*/
public function getModels($siteId)
{
//Load the Customers
$customers = Customer::where('site_id', $siteId)->get();
//Create an empty sidebarList
$sidebarList = [];
//Loop trough the shippingCosts
foreach ($customers as $customer) {
//New SidebarListItem
$sidebarListItem = new SidebarListItem();
//Set the values for the sidebar
$sidebarListItem->setId($customer->id);
$sidebarListItem->setStatus($customer->active);
//Check if account is expired. e.g. active until date is in the past.
$activeUntil = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $customer->active_until);
$now = Carbon::now();
$expired = ($now->gt($activeUntil));
$sidebarListItem->setExpired($expired);
//set the name as 'Shipping to {country name}'
$sidebarListItem->setName(
$customer->first_name . ' ' .
$customer->name_insertion . ' ' .
$customer->last_name
);
$sidebarListItem->setThumbnail($customer->id);
$sidebarList[] = $sidebarListItem;
}
return $sidebarList;
}
/**
* This method will save the Customer.
*
* @param $input
* @param null $customer KommaApp\Customers\Models\Customer
* @return Customer id
*/
public function saveModel($input, $customer = null)
{
$new = false;
//Check if the customer is null
if ($customer == null) {
//Create a new customer
$customer = new Customer();
//New customer
$customer->site_id = $this->kms->getCurrentSiteId();
$new = true;
}
$customer->active = (!empty($input['active']) ? $input['active'] : 0);
$customer->active_from = $input['active_from'];
$customer->active_until = $input['active_until'];
$customer->customer_number = $input['customer_number'];
$customer->username = $input['email'];
$customer->language_id = (int)$input['language_id'];
$customer->email = $input['email'];
$customer->first_name = $input['first_name'];
$customer->name_insertion = $input['name_insertion'];
$customer->last_name = $input['last_name'];
$customer->company = $input['company'];
$customer->company_vat = $input['company_vat'];
$customer->postal = $input['postal'];
$customer->city = $input['city'];
$customer->street = $input['street'];
$customer->house_number = $input['house_number'];
$customer->house_number_suffix = $input['house_number_suffix'];
$customer->country = $input['country'];
$customer->telephone = $input['telephone'];
//Save the customer
$customer->save();
//Send the email to the customer when it is an new customer
if ($new) $this->sendActivationEmail($customer);
return $customer->id;
}
public function destroyModel($customer)
{
$customer->delete();
}
public function getOrdersByCustomerId($siteId, $languageId, $customerId)
{
$records = [];
if (is_numeric($customerId)) {
$records = Order::where('customer_id', $customerId)->get();
}
return $records;
}
public function getLatestRegistrations($maxRecords = 10, $siteId = null)
{
$query = Customer::select('first_name', 'name_insertion', 'last_name', 'created_at', 'company')
->take($maxRecords)
->orderBy('created_at', 'desc');
if ($siteId) $query->where('site_id', $siteId);
return $query->get();
}
private function sendActivationEmail($customer)
{
$customer->active = 0;
$customer->save();
\App::setLocale($customer->language->iso_2);
//user found, so create an send activate email
\Password::customer()->sendResetLink(['email'=> $customer->email], function (Message $message) {
$message->subject(trans('auth.password-activate-email.subject'));
});
}
}