Skip to content

Commit

Permalink
improved class Table
Browse files Browse the repository at this point in the history
1. modify ccbuy, fix foreign key problem, now foreign key has to been
writen in the config file
2. bause of changing logic of forign key , table class has to be updated
  • Loading branch information
taoyu65 committed Dec 12, 2016
1 parent 19e506d commit 8eecd7a
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 396 deletions.
505 changes: 188 additions & 317 deletions .idea/workspace.xml

Large diffs are not rendered by default.

73 changes: 33 additions & 40 deletions app/Foundations/Table.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace App\Foundations;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
Expand Down Expand Up @@ -42,6 +41,8 @@ public function __construct($tableName, $columnName = '*', $operate = true)
} elseif (is_array($columnName)) {
$this->columnName = $columnName;
}
if ($columnName === null) //if give a wrong table name than return null
return ;
//making sure has id column in array columnName because of order by
if (!in_array('id', $this->columnName)) {
array_unshift($this->columnName, 'id');
Expand Down Expand Up @@ -436,17 +437,21 @@ private function getLabelHtml($data, $fieldName)
*/
private function getDropDownList($columnName, $foreignId, $tab)
{
$arr = explode('_', $columnName);
if (count($arr) <= 1){
$tableName = $columnName;
}else{
$tableName = $arr[0];
$configDropDownList = Config::get($this->configFileName . '.dropDownName');
if(!array_key_exists($this->tableName, $configDropDownList)){
echo '';return false;
}
$rules = $configDropDownList[$this->tableName];
$tableName = $showDropDownName = '';
foreach ($rules as $rule) {
if ($rule['foreignKey'] === $columnName) {
$tableName = $rule['foreignTableName'];
$showDropDownName = $rule['columnShow'];
}
}
//
$showDropDownName = $this->getDropDownName($tableName);
$data = DB::table($tableName)->get();
$html = '<select class="form-control input-sm" id="'.$tab.$columnName.'" name="'.$tab.$columnName.'">';
$html .= '<option value="">'.trans('cc_admin/table.dropList').'</option>';
$html .= '<option value="">请选择</option>';
foreach ($data as $d) {
if ($foreignId == $d->id) {
$html .= '<option value="'.$d->id.'" selected>'.$d->$showDropDownName.'</option>';
Expand All @@ -458,20 +463,6 @@ private function getDropDownList($columnName, $foreignId, $tab)
return $html;
}

/**
* @param $tableName
* @return string - return the column name that showing in the drop down list as text will be set up in the ccbuy config
*/
private function getDropDownName($tableName)
{
$nameList = Config::get($this->configFileName . '.dropDownName');
if (array_key_exists($tableName, $nameList)) {
return $nameList[$tableName];
}
//anything happened will be showing id column
return 'id';
}

/**
* @param $id
* @return string
Expand Down Expand Up @@ -504,12 +495,12 @@ public function getHtmlToDelete($id)
switch ($type) {
case 'interlock':
foreach ($b as $foreignTableName => $field) {
foreach ($field as $f => $fieldShow) {
$warning = $this->getWarningHtml_interlock($foreignTableName, $id, $fieldShow);
$html .= $warning;
if ($warning != '')
$deleteString .= $foreignTableName . ':' . $this->tableName . '_id=' . $id . ',';
}
$foreignId = $field['columnName'];
$fieldShow = $field['field'];
$warning = $this->getWarningHtml_interlock($foreignTableName, $id, $fieldShow, $foreignId);
$html .= $warning;
if ($warning != '')
$deleteString .= $foreignTableName . ':' . $foreignId . '=' . $id . ',';
}
break;
case 'existing':
Expand Down Expand Up @@ -539,31 +530,33 @@ public function getHtmlToDelete($id)

/**
* only delete record when no another table's record related this record
* @param $foreignTableName - foreign table from config file
* @param $rules - foreign table from config file
* @param $id
* @return string
*/
private function getWarningHtml_existing($foreignTableName, $id)
private function getWarningHtml_existing($rules, $id)
{
$html = '';
$foreignId = $this->tableName . '_id';
$result = DB::table($foreignTableName)->where($foreignId, $id)->first();
if ($result)
$html = '<script type="application/javascript">$("#btsubmit").attr("disabled",true);</script><div class="alert alert-danger" role="alert"><strong>警告! </strong>不能删除此条记录! 仍然有数据在使用此条记录! 请先删除相关记录</div>';
return $html;
foreach ($rules as $foreignTableName => $foreignId) {
$result = DB::table($foreignTableName)->where($foreignId, $id)->first();
if ($result) {
$html = '<script type="application/javascript">$("#btsubmit").attr("disabled",true);</script><div class="alert alert-danger" role="alert"><strong>警告! </strong>不能删除此条记录! 表:' . $foreignTableName . ' 仍然有数据在使用此条记录! 请先删除相关记录</div>';
return $html;
}
}
}

/**
* interlock delete
* @param $foreignTableName - foreign table name which is related to
* @param $id - foreign id
* @param $showingColumn - title
* @param $foreignId
* @return string
*/
private function getWarningHtml_interlock($foreignTableName, $id, $showingColumn)
private function getWarningHtml_interlock($foreignTableName, $id, $showingColumn, $foreignId)
{
$foreignKey = $this->tableName . '_id';
$data = DB::table($foreignTableName)->where($foreignKey, $id)->get();
//$foreignKey = $this->tableName . '_id';
$data = DB::table($foreignTableName)->where($foreignId, $id)->get();
$html = '';
if (count($data) != 0) {
$html .= '<div class="form-group">';
Expand Down
10 changes: 0 additions & 10 deletions app/Http/Controllers/ccTableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Controllers;

use App\Foundations\Table;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
Expand All @@ -23,15 +22,6 @@ public function showTable($table)
return view('cc_admin/table', ['html' => $html, 'table' => $table]);
}

public function showTableBySearch($table)
{
//get the column which will be shown on the page from ccbuy config
$showingColumn = Config::get($this->configFileName . '.showColumn.'.$table);
//set second para to show column that you want to review
$data = new Table($table, $showingColumn);
$html = $data->getHtml();
return view('cc_admin/table', ['html' => $html, 'table' => $table]);
}
/**
* edit page to show
* @param $tableName
Expand Down
42 changes: 28 additions & 14 deletions config/ccbuy.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,23 @@
]
],
/* on the back end table edit pages , use for located which column will be showing on the drop down list
'tableName' => 'showingField' - if 'id' of table A as a foreign key is showing on table B, than 'showingField' will be showing on the output of drop down list
* can be multiple foreign key set up on one table
*/
'dropDownName' => [
'carts' => 'rename',
'customs' => 'customName',
'incomes' => '',
'items' => 'itemName',
'stores' => 'storeName',
'users' => 'name'
'carts' => [
[
'foreignKey' => 'customs_id', //foreign key column name
'foreignTableName' => 'customs', //table's name relative the table that has its foreign key
'columnShow' => 'customName' //column name use for showing on the web page
],
],
'items' => [
[
'foreignKey' => 'stores_id',
'foreignTableName' => 'stores',
'columnShow' => 'storeName'
],
],
],
//show which field(column) will be showing - all the table name array must be present when you new table (class) and showColum as a parameter
'showColumn' => [
Expand All @@ -167,21 +175,27 @@
'delete' => [
'carts' => [
'interlock' => [
'items' => [
'field' => [ //use for showing on the output page
'id' => 'ID',
'itemName' => '物品名称',
'items' => [
'columnName'=> 'carts_id', //column name(foreign key) for locating the items will be deleted
'field' => [ //use for showing on the output page
'id' => 'ID',
'itemName' => '物品名称',
'sellPrice' => '出售金额',
'date' => '日期'
'date' => '日期'
]
]
]
],
'customs' => [
'existing' => 'carts'
'existing' => [
'carts' => 'customs_id' //check if id in the existing table is exist or not
//'otherTable' => 'foreignId'
]
],
'stores' => [
'existing' => 'items'
'existing' => [
'items' => 'stores_id'
]
]
],
/*special operation (only focus on the effect of data change) like delete one record from A table, than the data of a record from table B will be changed. the role can be customized
Expand Down
1 change: 0 additions & 1 deletion resources/views/cc_admin/table.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

@extends('layouts.ccadmin')
@section('content1')
<script type="text/javascript" src='{{url("ui/layer/layer.js")}}'></script>
<script type="text/javascript" src='{{url("ui/laydate/laydate.js")}}'></script>
<script type="text/javascript">
//change hover css when change different table
Expand Down
28 changes: 14 additions & 14 deletions resources/views/statisticAllItem.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@
<canvas id="myChart" width="600" height="300"></canvas>
<script>
var color = [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)',
'rgba(199,21,133, 1)',
'rgba(95,158,160, 1)',
'rgba(85,107,47, 1)',
'rgba(0,255,0, 1)',
'rgba(165,42,42, 1)',
'rgba(105,105,105, 1)',
'rgba(255, 99, 132, 0.5)',
'rgba(54, 162, 235, 0.5)',
'rgba(255, 206, 86, 0.5)',
'rgba(75, 192, 192, 0.5)',
'rgba(153, 102, 255, 0.5)',
'rgba(255, 159, 64, 0.5)',
'rgba(199,21,133, 0.5)',
'rgba(95,158,160, 0.5)',
'rgba(85,107,47, 0.5)',
'rgba(0,255,0, 0.5)',
'rgba(165,42,42, 0.5)',
'rgba(105,105,105, 0.5)',
];
var returnData = '{!! $dataSet !!}';
Expand All @@ -74,11 +74,11 @@
var i = 0;
for(var label in jsonData){
i++;
if(i > 12) {
if(i > 11) {
var r = GetRandomNum(0, 250).toString();
var g = GetRandomNum(0, 250).toString();
var b = GetRandomNum(0, 250).toString();
color.push('rgba('+r+', '+g+', '+b+', 0.8)');
color.push('rgba('+r+', '+g+', '+b+', 0.5)');
}
var json = {
label: label,
Expand Down

0 comments on commit 8eecd7a

Please sign in to comment.