Merge pull request #22 from 4xmen/master

optimized and test improved
pull/28/head
Farzad AsA 1 year ago committed by GitHub
commit 40cc0a0061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,12 +9,18 @@ use App\Models\Setting;
use Xmen\StarterKit\Helpers\TDate; use Xmen\StarterKit\Helpers\TDate;
use Xmen\StarterKit\Models\Category; use Xmen\StarterKit\Models\Category;
use Xmen\StarterKit\Models\Menu; use Xmen\StarterKit\Models\Menu;
use Xmen\StarterKit\Models\MenuItem;
use Xmen\StarterKit\Models\Post; use Xmen\StarterKit\Models\Post;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
/***
* get setting by key
* @param string $key setting key
* @return false|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed|string|null
*/
function getSetting($key) function getSetting($key)
{ {
if ( !isset($_SERVER['SERVER_NAME']) || !\Schema::hasTable('settings')){ if (!isset($_SERVER['SERVER_NAME']) || !\Schema::hasTable('settings')) {
return false; return false;
} }
$x = Setting::where('key', $key)->first(); $x = Setting::where('key', $key)->first();
@ -24,6 +30,12 @@ function getSetting($key)
} }
return $x->value; return $x->value;
} }
/***
* get category form setting by key
* @param string $key setting key
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|string|Category|null
*/
function getSettingCategory($key) function getSettingCategory($key)
{ {
$x = Setting::where('key', $key)->first(); $x = Setting::where('key', $key)->first();
@ -31,8 +43,14 @@ function getSettingCategory($key)
$a = new \stdClass(); $a = new \stdClass();
return ''; return '';
} }
return Category::where('id',$x->value)->first(); return Category::where('id', $x->value)->first();
} }
/***
* get product category by setting key
* @param string $key setting key
* @return Cat|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|string|null
*/
function getSettingCat($key) function getSettingCat($key)
{ {
$x = Setting::where('key', $key)->first(); $x = Setting::where('key', $key)->first();
@ -40,15 +58,23 @@ function getSettingCat($key)
$a = new \stdClass(); $a = new \stdClass();
return ''; return '';
} }
return Cat::where('id',$x->value)->first(); return Cat::where('id', $x->value)->first();
} }
/***
* get stock types
* @return array
*/
function stockTypes() function stockTypes()
{ {
return ["IN_STOCK" => __("In stock"), "OUT_STOCK" => __("Out stock"), "BACK_ORDER" => __("Back order")]; return ["IN_STOCK" => __("In stock"), "OUT_STOCK" => __("Out stock"), "BACK_ORDER" => __("Back order")];
} }
/***
* make meta with key and value
* @param $metaz
* @return array
*/
function metaing($metaz) function metaing($metaz)
{ {
$out = []; $out = [];
@ -61,16 +87,22 @@ function metaing($metaz)
return $out; return $out;
} }
function getPriceableMeta(Product $pro){ /***
* get just pricable meta of product
* @param Product $pro
* @return array
*/
function getPriceableMeta(Product $pro)
{
$metas = $pro->getAllMeta()->toArray(); $metas = $pro->getAllMeta()->toArray();
if ( count($metas) == 0 ){ if (count($metas) == 0) {
return []; return [];
} }
$pricables = $pro->category->props()->where('priceable',1)->pluck('name')->toArray(); $pricables = $pro->category->props()->where('priceable', 1)->pluck('name')->toArray();
$result = []; $result = [];
if (is_array($pricables)){ if (is_array($pricables)) {
foreach ($pricables as $price){ foreach ($pricables as $price) {
if (isset($metas[$price])){ if (isset($metas[$price])) {
$result[$price] = $metas[$price]; $result[$price] = $metas[$price];
} }
} }
@ -78,7 +110,13 @@ function getPriceableMeta(Product $pro){
return $result; return $result;
} }
/***
* show product categories in node with ul-li
* @param $cats category node
* @param $liClass class of lis
* @param $ulClass class of uls
* @return string
*/
function showCats($cats = [], $liClass = '', $ulClass = '') function showCats($cats = [], $liClass = '', $ulClass = '')
{ {
if ($cats == []) { if ($cats == []) {
@ -101,6 +139,11 @@ function showCats($cats = [], $liClass = '', $ulClass = '')
return $txt; return $txt;
} }
/***
* show menu node for manage
* @param $arr
* @return string
*/
function showMenuMange($arr) function showMenuMange($arr)
{ {
$back = ''; $back = '';
@ -127,6 +170,11 @@ LI;
return $back; return $back;
} }
/***
* show menu node
* @param array $arr
* @return string
*/
function showMenu($arr) function showMenu($arr)
{ {
$back = ''; $back = '';
@ -182,6 +230,12 @@ LI;
return $back; return $back;
} }
/***
* show input products categories node
* @param Cat[] $arr
* @return string
*/
function showCatNode($arr) function showCatNode($arr)
{ {
$ret = ''; $ret = '';
@ -195,6 +249,11 @@ function showCatNode($arr)
return $ret; return $ret;
} }
/***
* show menu items as types
* @param MenuItem[] $items
* @return string
*/
function MenuShowItems($items) function MenuShowItems($items)
{ {
$out = ''; $out = '';
@ -234,7 +293,7 @@ function MenuShowItems($items)
$out .= '</ul>'; $out .= '</ul>';
break; break;
case "cat-news": case "cat-news":
$out .= '<a class="menu-link">'.$item->title .'</a>'; $out .= '<a class="menu-link">' . $item->title . '</a>';
$cat = Category::whereId($item->menuable_id)->firstOrFail(); $cat = Category::whereId($item->menuable_id)->firstOrFail();
$news = $cat->posts()->limit(10)->get(['slug', 'title']); $news = $cat->posts()->limit(10)->get(['slug', 'title']);
$out .= '<ul>'; $out .= '<ul>';
@ -255,12 +314,21 @@ function MenuShowItems($items)
return $out; return $out;
} }
/***
* show menu by name
* @param $menu_name
* @return string
*/
function MenuShowByName($menu_name) function MenuShowByName($menu_name)
{ {
$menu = Menu::whereName($menu_name)->firstOrFail(); $menu = Menu::whereName($menu_name)->firstOrFail();
return MenuShowItems($menu->menuItems()->whereNull('parent')->get()); return MenuShowItems($menu->menuItems()->whereNull('parent')->get());
} }
/***
* make breadcrumbs for admin panel
* @return void
*/
function lastCrump() function lastCrump()
{ {
$routes = explode('.', Route::currentRouteName()); $routes = explode('.', Route::currentRouteName());
@ -335,59 +403,106 @@ function lastCrump()
} }
/***
* get main product categories
* @param int $limit
* @return Cat[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|\LaravelIdea\Helper\App\Models\_IH_Cat_C
*/
function getMainCats($limit = 10) function getMainCats($limit = 10)
{ {
return Cat::whereNull('parent_id')->limit($limit)->get(); return Cat::whereNull('parent_id')->limit($limit)->get();
} }
/***
* get products by category id
* @param int $id
* @param string $order
* @param string $orderMethod
* @param int $limit
* @return Product[]|array|\Illuminate\Database\Eloquent\Collection|\LaravelIdea\Helper\App\Models\_IH_Product_C
*/
function getProductByCat($id, $order = 'id', $orderMethod = 'desc', $limit = 10) function getProductByCat($id, $order = 'id', $orderMethod = 'desc', $limit = 10)
{ {
$cat = Cat::where('id', $id)->first(); $cat = Cat::where('id', $id)->first();
if ($cat == null){ if ($cat == null) {
return []; return [];
} }
return $cat->active_products()->where('active',1) return $cat->active_products()->where('active', 1)
->orderBy($order, $orderMethod)->limit($limit)->get(); ->orderBy($order, $orderMethod)->limit($limit)->get();
} }
/***
* get product by category id where `stock_quantity` more than 0
* @param int $id
* @param string $order
* @param int $limit
* @return Product[]|array|\Illuminate\Database\Eloquent\Collection|\LaravelIdea\Helper\App\Models\_IH_Product_C
*/
function getProductByCatQ($id, $order = 'id', $limit = 10) function getProductByCatQ($id, $order = 'id', $limit = 10)
{ {
$cat = Cat::where('id', $id)->first(); $cat = Cat::where('id', $id)->first();
if ($cat == null){ if ($cat == null) {
return []; return [];
} }
return $cat->products()->where('active',1)->where('stock_quantity','>', 0) return $cat->products()->where('active', 1)->where('stock_quantity', '>', 0)
->orderByDesc('id')->limit($limit)->get(); ->orderByDesc('id')->limit($limit)->get();
} }
/***
* get sub product categories by id
* @param $id
* @return Cat[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\LaravelIdea\Helper\App\Models\_IH_Cat_C
*/
function getSubCats($id) function getSubCats($id)
{ {
return Cat::where('parent_id', $id)->get(); return Cat::where('parent_id', $id)->get();
} }
/***
* get products with sort
* @param string $order
* @param string $orderMethod
* @param int $limit
* @return Product[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection|\LaravelIdea\Helper\App\Models\_IH_Product_C
*/
function getProducts($order = 'id', $orderMethod = 'desc', $limit = 10) function getProducts($order = 'id', $orderMethod = 'desc', $limit = 10)
{ {
return Product::orderBy($order, $orderMethod)->limit($limit)->get(); return Product::orderBy($order, $orderMethod)->limit($limit)->get();
} }
/***
* get property by name
* @param string $name
* @return Prop|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
*/
function getProp($name) function getProp($name)
{ {
return Prop::where('name', $name)->first(); return Prop::where('name', $name)->first();
} }
/***
* get unit of property by name
* @param string $name
* @return \Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed|string|null
*/
function getPropUnit($name) function getPropUnit($name)
{ {
if ( Prop::where('name', $name)->count() == 0) { if (Prop::where('name', $name)->count() == 0) {
return ''; return '';
} }
return Prop::where('name', $name)->first()->unit; return Prop::where('name', $name)->first()->unit;
} }
/***
* get label of property by name
* @param string $name
* @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|\Illuminate\Database\Eloquent\HigherOrderBuilderProxy|mixed|string|null
*/
function getPropLabel($name) function getPropLabel($name)
{ {
if (Prop::where('name', $name)->count() == 0){ if (Prop::where('name', $name)->count() == 0) {
return ''; return '';
} }
$n = Prop::where('name', $name)->first(); $n = Prop::where('name', $name)->first();
@ -402,31 +517,55 @@ function getPropLabel($name)
} }
} }
/***
* check is json or not
* @param string $str
* @return bool
*/
function isJson($str) function isJson($str)
{ {
$json = json_decode($str); $json = json_decode($str);
return $json && $str != $json; return $json && $str != $json;
} }
/***
* json decode and sort
* @param array $json
* @return mixed
*/
function jsonOrder($json) function jsonOrder($json)
{ {
$data = json_decode( $json ,true); $data = json_decode($json, true);
asort($data); asort($data);
return $data; return $data;
} }
function showMetaValue($key, $value){ /***
* get meta value
* @param string $key
* @param string $value
* @return void
*/
function getMetaValue($key, $value)
{
$p = Prop::whereName($key)->first(); $p = Prop::whereName($key)->first();
foreach (json_decode($p->options) as $n){ foreach (json_decode($p->options) as $n) {
if ($n->value == $value){ if ($n->value == $value) {
return $n->title; return $n->title;
} }
} }
} }
/***
* show meta with values
* @param string $key
* @param string $value
* @return string
*/
function showMeta($key, $value) function showMeta($key, $value)
{ {
if (Prop::whereName('warranty')->count()== 0 && $key == 'warranty'){ if (Prop::whereName('warranty')->count() == 0 && $key == 'warranty') {
return ''; return '';
} }
@ -435,15 +574,15 @@ function showMeta($key, $value)
} }
if ($key == 'warranty') { if ($key == 'warranty') {
$p = Prop::whereName('warranty')->first(); $p = Prop::whereName('warranty')->first();
foreach (json_decode($p->options) as $n){ foreach (json_decode($p->options) as $n) {
if ($n->value == $value){ if ($n->value == $value) {
return $n->title; return $n->title;
} }
} }
} }
if ($key == 'color') { if ($key == 'color') {
return '<div style="background: '.$value.';min-width: 15px; height:15px; display: inline-block; margin-right: 4px; margin-left: 4px" ></div><b style="position: relative;top: -3px;padding:2px;display: inline-block">'.getColorName($value).'</b>'; return '<div style="background: ' . $value . ';min-width: 15px; height:15px; display: inline-block; margin-right: 4px; margin-left: 4px" ></div><b style="position: relative;top: -3px;padding:2px;display: inline-block">' . getColorName($value) . '</b>';
} }
$txt = $value; $txt = $value;
@ -456,38 +595,61 @@ function showMeta($key, $value)
return trim($txt . ' ' . getPropUnit($key)); return trim($txt . ' ' . getPropUnit($key));
} }
/***
* time to persian date
* @param $date
* @param $format
* @return float|int|string|\Xmen\StarterKit\Helpers\timestamp
*/
function time2persian($date, $format = 'Y/m/d') function time2persian($date, $format = 'Y/m/d')
{ {
$dt = new TDate(); $dt = new TDate();
return $dt->PDate($format, $date); return $dt->PDate($format, $date);
} }
function getColors(){ /***
$colors = Prop::where('name','color')->first(); * get colors
if ($colors == null){ * @return array
*/
function getColors()
{
$colors = Prop::where('name', 'color')->first();
if ($colors == null) {
return []; return [];
} }
$data = json_decode($colors->options,true); $data = json_decode($colors->options, true);
$result = []; $result = [];
foreach ($data as $color) { foreach ($data as $color) {
$result[$color['value']] = $color['title']; $result[$color['value']] = $color['title'];
} }
return $result; return $result;
} }
function getColorName($colorVal){
$colors = Prop::where('name','color')->first(); /***
if ($colors == null){ * get color name
* @param $colorVal
* @return mixed|string
*/
function getColorName($colorVal)
{
$colors = Prop::where('name', 'color')->first();
if ($colors == null) {
return 'رنگ'; return 'رنگ';
} }
$data = json_decode($colors->options,true); $data = json_decode($colors->options, true);
foreach ($data as $color) { foreach ($data as $color) {
if ($colorVal == $color['value']){ if ($colorVal == $color['value']) {
return $color['title']; return $color['title'];
} }
} }
return 'رنگ'; return 'رنگ';
} }
/***
* make chartjs
* @param $pro
* @return string
*/
function makeChart($pro) function makeChart($pro)
{ {
$times = $pro->prices_history()->pluck('created_at')->toArray(); $times = $pro->prices_history()->pluck('created_at')->toArray();
@ -506,7 +668,13 @@ TXT;
return $text; return $text;
} }
function makeProductBreadcrumb($p, $c) /***
* make breadcrumb
* @param Product $p
* @param Cat $c
* @return array[]
*/
function makeProductBreadcrumb(Product $p, Cat $c)
{ {
$items = [ $items = [
[ [
@ -530,6 +698,11 @@ function makeProductBreadcrumb($p, $c)
return $items; return $items;
} }
/***
* show markup breadcrumb
* @param $itemz
* @return void
*/
function showBreadcrumb($itemz = []) function showBreadcrumb($itemz = [])
{ {
$text = <<<DOC $text = <<<DOC
@ -561,17 +734,28 @@ DOC;
echo str_replace('ITEMS', $items, $text); echo str_replace('ITEMS', $items, $text);
} }
function cardCount(){ /***
return count(array_merge(unserialize(session('card', serialize([]))),unserialize(session('qcard', serialize([]))))); * get items count in shoppig card
* @return int
*/
function cardCount()
{
return count(array_merge(unserialize(session('card', serialize([]))), unserialize(session('qcard', serialize([])))));
} }
/***
function sendSMSText($number,$content){ * send sms
* @param string $number phone number
* @param string $content sms content
* @return bool|string
*/
function sendSMSText($number, $content)
{
$url = 'http://rest.payamak-panel.com/api/SendSMS/SendSMS'; $url = 'http://rest.payamak-panel.com/api/SendSMS/SendSMS';
$options =array( $options = array(
'content-type' => 'application/x-www-form-urlencoded', 'content-type' => 'application/x-www-form-urlencoded',
'postman-token' => '3e37c158-2c35-75aa-1ae7-f76d90ebbe8f', 'postman-token' => '3e37c158-2c35-75aa-1ae7-f76d90ebbe8f',
'cache-control' => 'no-cache' 'cache-control' => 'no-cache'
@ -580,7 +764,7 @@ function sendSMSText($number,$content){
'username' => config('app.sms_user'), 'username' => config('app.sms_user'),
'password' => config('app.sms_pass'), 'password' => config('app.sms_pass'),
'to' => $number, 'to' => $number,
'from' =>config('app.sms_number'), 'from' => config('app.sms_number'),
'text' => $content, 'text' => $content,
'isflash' => 'false' 'isflash' => 'false'
)); ));
@ -589,38 +773,59 @@ function sendSMSText($number,$content){
$ch = curl_init(); $ch = curl_init();
//set the url, number of POST vars, POST data //set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
//So that curl_exec returns the contents of the cURL; rather than echoing it //So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post //execute post
$result = curl_exec($ch); $result = curl_exec($ch);
return $result; return $result;
} }
function remTitle($str){ /***
* remove title of html code
* @param $str
* @return array|string|string[]|null
*/
function remTitle($str)
{
$re = '/<title>(.*?)<\/title>/m'; $re = '/<title>(.*?)<\/title>/m';
return preg_replace($re,'',$str); return preg_replace($re, '', $str);
} }
function showJsonByKey($json,$key){
/***
* show json by key
* @param $json
* @param $key
* @return mixed|string
*/
function showJsonByKey($json, $key)
{
$x = json_decode($json); $x = json_decode($json);
$x = json_decode($x->data,true); $x = json_decode($x->data, true);
$items = json_decode(\App\Helpers\getProp('size')->options,true); $items = json_decode(\App\Helpers\getProp('size')->options, true);
foreach ($items as $item) { foreach ($items as $item) {
if ($x[$key] == $item['value']){ if ($x[$key] == $item['value']) {
return $item['title']; return $item['title'];
} }
} }
return 'x'; return 'x';
} }
function findLink($html){
/***
* find html links
* @param $html
* @return string|null
*/
function findLink($html)
{
$url = preg_match('/<a href="(.+)">/', $html, $match); $url = preg_match('/<a href="(.+)">/', $html, $match);
if (!isset($match[1])){ if (!isset($match[1])) {
return null; return null;
} }

@ -58,7 +58,7 @@
{{__('Expire date')}} {{__('Expire date')}}
</label> </label>
<input placeholder="{{__("Expire date")}}" type="text" data-reuslt="#exp-date" <input placeholder="{{__("Expire date")}}" type="text" data-reuslt="#exp-date"
class="form-control @error('expire') is-invalid @enderror dtp" @if(isset($discount)) value="{{\App\Helpers\time2persian($discount->expire)}}" @endif> class="form-control @error('expire') is-invalid @enderror dtp" @if(isset($discount)) value="{{$discount->expire->jdate('Y/m/d')}}" @endif>
<input type="hidden" name="expire" id="exp-date" @if(isset($discount)) value="{{strtotime($discount->expire)}}" @endif> <input type="hidden" name="expire" id="exp-date" @if(isset($discount)) value="{{strtotime($discount->expire)}}" @endif>
</div> </div>
</div> </div>

@ -65,7 +65,7 @@
@if($n->expire == null) @if($n->expire == null)
- -
@else @else
{{\App\Helpers\time2persian($n->expire)}} {{$n->expire->jdate('Y/m/d')}}
@endif @endif
</td> </td>
<td> <td>

@ -98,7 +98,7 @@
{{$dis->code}} {{$dis->code}}
</td> </td>
<td> <td>
{{\App\Helpers\time2persian($dis->expire)}} {{$dis->expire->jdate('Y/m/d')}}
</td> </td>
<td> <td>
<button type="button" class="btn btn-danger" data-id="{{$dis->id}}"> <button type="button" class="btn btn-danger" data-id="{{$dis->id}}">

@ -179,7 +179,7 @@
@endforeach @endforeach
</div> </div>
<div class="mb-1 text-muted"> <div class="mb-1 text-muted">
{{\App\Helpers\time2persian($p->created_at)}} {{$p->created_at->jdate('Y/m/d')}}
</div> </div>
<p> <p>
{{$p->subtitle}} {{$p->subtitle}}

@ -27,7 +27,7 @@
@endforeach @endforeach
</div> </div>
<div class="mb-1 text-muted"> <div class="mb-1 text-muted">
{{\App\Helpers\time2persian($p->created_at)}} {{$p->created_at->jdate('Y/m/d')}}
</div> </div>
<p> <p>
{{$p->subtitle}} {{$p->subtitle}}

@ -0,0 +1,67 @@
<?php
namespace Tests\Feature;
use App\Models\Cat;
use App\Models\Product;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Xmen\StarterKit\Models\Post;
class websitePagesTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_index()
{
$response = $this->get('/');
$response->assertStatus(200);
}
public function test_products()
{
$response = $this->get(route('products'));
$response->assertStatus(200);
}
public function test_posts()
{
$response = $this->get(route('posts'));
$response->assertStatus(200);
}
public function test_single_post()
{
$response = $this->get(route('post',Post::inRandomOrder()->first()->slug));
$response->assertStatus(200);
}
public function test_single_product()
{
$response = $this->get(route('product',Product::inRandomOrder()->first()->slug));
$response->assertStatus(200);
}
public function test_single_product_category()
{
$response = $this->get(route('cat',Cat::inRandomOrder()->first()->slug));
$response->assertStatus(200);
}
public function test_card_empty(){
$this->get(route('reset'));
$response = $this->get(route('card.show'));
$response->assertStatus(200);
}
public function test_card_with_products(){
$this->get(route('card.add',Product::inRandomOrder()->first()->slug));
$this->get(route('card.add',Product::inRandomOrder()->first()->slug));
$response = $this->get(route('card.show'));
$response->assertStatus(200);
}
}
Loading…
Cancel
Save