@@ -1134,21 +1134,32 @@ protected function parseGetParameterArray($get,$name,$characters) {
1134
1134
return $ values ;
1135
1135
}
1136
1136
1137
- protected function applyBeforeHandler (&$ action ,&$ database ,&$ table ,&$ id ,&$ callback ,&$ input ) {
1137
+ protected function applyBeforeHandler (&$ action ,&$ database ,&$ table ,&$ ids ,&$ callback ,&$ inputs ) {
1138
1138
if (is_callable ($ callback ,true )) {
1139
- $ callback ($ action ,$ database ,$ table ,$ id ,$ input );
1139
+ $ max = max (count ($ ids ),count ($ inputs ));
1140
+ for ($ i =0 ;$ i <$ max ;$ i ++) {
1141
+ $ id = isset ($ ids [$ i ])?$ ids [$ i ]:false ;
1142
+ $ input = isset ($ inputs [$ i ])?$ inputs [$ i ]:false ;
1143
+ $ callback ($ action ,$ database ,$ table ,$ id ,$ input );
1144
+ }
1140
1145
}
1141
1146
}
1142
1147
1143
- protected function applyAfterHandler ($ parameters ,$ output ) {
1148
+ protected function applyAfterHandler ($ parameters ,$ outputs ) {
1144
1149
$ callback = $ parameters ['after ' ];
1145
1150
if (is_callable ($ callback ,true )) {
1146
1151
$ action = $ parameters ['action ' ];
1147
1152
$ database = $ parameters ['database ' ];
1148
1153
$ table = $ parameters ['tables ' ][0 ];
1149
- $ id = $ parameters ['key ' ][0 ];
1150
- $ input = $ parameters ['inputs ' ];
1151
- $ callback ($ action ,$ database ,$ table ,$ id ,$ input ,$ output );
1154
+ $ ids = $ parameters ['key ' ][0 ];
1155
+ $ inputs = $ parameters ['inputs ' ];
1156
+ $ max = max (count ($ ids ),count ($ inputs ));
1157
+ for ($ i =0 ;$ i <$ max ;$ i ++) {
1158
+ $ id = isset ($ ids [$ i ])?$ ids [$ i ]:false ;
1159
+ $ input = isset ($ inputs [$ i ])?$ inputs [$ i ]:false ;
1160
+ $ output = is_array ($ outputs )?$ outputs [$ i ]:$ outputs ;
1161
+ $ callback ($ action ,$ database ,$ table ,$ id ,$ input ,$ output );
1162
+ }
1152
1163
}
1153
1164
}
1154
1165
@@ -1299,7 +1310,7 @@ protected function processKeyParameter($key,$tables,$database) {
1299
1310
if ($ key ===false ) return false ;
1300
1311
$ fields = $ this ->findPrimaryKeys ($ tables [0 ],$ database );
1301
1312
if (count ($ fields )!=1 ) $ this ->exitWith404 ('1pk ' );
1302
- return array ($ key ,$ fields [0 ]);
1313
+ return array (explode ( ' , ' , $ key) ,$ fields [0 ]);
1303
1314
}
1304
1315
1305
1316
protected function processOrderingsParameter ($ orderings ) {
@@ -1453,7 +1464,7 @@ protected function retrieveObject($key,$fields,$filters,$tables) {
1453
1464
$ this ->convertOutputs ($ sql ,$ params ,$ fields [$ table ]);
1454
1465
$ sql .= ' FROM ! ' ;
1455
1466
$ params [] = $ table ;
1456
- $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ]);
1467
+ $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ][ 0 ] );
1457
1468
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1458
1469
$ object = null ;
1459
1470
if ($ result = $ this ->db ->query ($ sql ,$ params )) {
@@ -1465,10 +1476,10 @@ protected function retrieveObject($key,$fields,$filters,$tables) {
1465
1476
1466
1477
protected function retrieveObjects ($ key ,$ fields ,$ filters ,$ tables ) {
1467
1478
$ keyField = $ key [1 ];
1468
- $ keys = explode ( ' , ' , $ key [0 ]) ;
1479
+ $ keys = $ key [0 ];
1469
1480
$ rows = array ();
1470
1481
foreach ($ keys as $ key ) {
1471
- $ result = $ this ->retrieveObject (array ($ key ,$ keyField ),$ fields ,$ filters ,$ tables );
1482
+ $ result = $ this ->retrieveObject (array (array ( $ key) ,$ keyField ),$ fields ,$ filters ,$ tables );
1472
1483
if ($ result ===null ) {
1473
1484
return null ;
1474
1485
}
@@ -1519,7 +1530,7 @@ protected function updateObject($key,$input,$filters,$tables) {
1519
1530
$ params [] = $ k ;
1520
1531
$ params [] = $ v ;
1521
1532
}
1522
- $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ]);
1533
+ $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ][ 0 ] );
1523
1534
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1524
1535
$ result = $ this ->db ->query ($ sql ,$ params );
1525
1536
if (!$ result ) return null ;
@@ -1529,14 +1540,14 @@ protected function updateObject($key,$input,$filters,$tables) {
1529
1540
protected function updateObjects ($ key ,$ inputs ,$ filters ,$ tables ) {
1530
1541
if (!$ inputs ) return false ;
1531
1542
$ keyField = $ key [1 ];
1532
- $ keys = explode ( ' , ' , $ key [0 ]) ;
1543
+ $ keys = $ key [0 ];
1533
1544
if (count ($ inputs )!=count ($ keys )) {
1534
1545
$ this ->exitWith404 ('subject ' );
1535
1546
}
1536
1547
$ rows = array ();
1537
1548
$ this ->db ->beginTransaction ();
1538
1549
foreach ($ inputs as $ i =>$ input ) {
1539
- $ result = $ this ->updateObject (array ($ keys [$ i ],$ keyField ),$ input ,$ filters ,$ tables );
1550
+ $ result = $ this ->updateObject (array (array ( $ keys [$ i ]) ,$ keyField ),$ input ,$ filters ,$ tables );
1540
1551
if ($ result ===null ) {
1541
1552
$ this ->db ->rollbackTransaction ();
1542
1553
return null ;
@@ -1551,7 +1562,7 @@ protected function deleteObject($key,$filters,$tables) {
1551
1562
$ table = $ tables [0 ];
1552
1563
$ sql = 'DELETE FROM ! ' ;
1553
1564
$ params = array ($ table );
1554
- $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ]);
1565
+ $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ][ 0 ] );
1555
1566
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1556
1567
$ result = $ this ->db ->query ($ sql ,$ params );
1557
1568
if (!$ result ) return null ;
@@ -1560,11 +1571,11 @@ protected function deleteObject($key,$filters,$tables) {
1560
1571
1561
1572
protected function deleteObjects ($ key ,$ filters ,$ tables ) {
1562
1573
$ keyField = $ key [1 ];
1563
- $ keys = explode ( ' , ' , $ key [0 ]) ;
1574
+ $ keys = $ key [0 ];
1564
1575
$ rows = array ();
1565
1576
$ this ->db ->beginTransaction ();
1566
1577
foreach ($ keys as $ key ) {
1567
- $ result = $ this ->deleteObject (array ($ key ,$ keyField ),$ filters ,$ tables );
1578
+ $ result = $ this ->deleteObject (array (array ( $ key) ,$ keyField ),$ filters ,$ tables );
1568
1579
if ($ result ===null ) {
1569
1580
$ this ->db ->rollbackTransaction ();
1570
1581
return null ;
@@ -1595,7 +1606,7 @@ protected function incrementObject($key,$input,$filters,$tables,$fields) {
1595
1606
$ params [] = $ k ;
1596
1607
}
1597
1608
}
1598
- $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ]);
1609
+ $ this ->addFilter ($ filters ,$ table ,'and ' ,$ key [1 ],'eq ' ,$ key [0 ][ 0 ] );
1599
1610
$ this ->addWhereFromFilters ($ filters [$ table ],$ sql ,$ params );
1600
1611
$ result = $ this ->db ->query ($ sql ,$ params );
1601
1612
if (!$ result ) return null ;
@@ -1605,14 +1616,14 @@ protected function incrementObject($key,$input,$filters,$tables,$fields) {
1605
1616
protected function incrementObjects ($ key ,$ inputs ,$ filters ,$ tables ,$ fields ) {
1606
1617
if (!$ inputs ) return false ;
1607
1618
$ keyField = $ key [1 ];
1608
- $ keys = explode ( ' , ' , $ key [0 ]) ;
1619
+ $ keys = $ key [0 ];
1609
1620
if (count ($ inputs )!=count ($ keys )) {
1610
1621
$ this ->exitWith404 ('subject ' );
1611
1622
}
1612
1623
$ rows = array ();
1613
1624
$ this ->db ->beginTransaction ();
1614
1625
foreach ($ inputs as $ i =>$ input ) {
1615
- $ result = $ this ->incrementObject (array ($ keys [$ i ],$ keyField ),$ input ,$ filters ,$ tables ,$ fields );
1626
+ $ result = $ this ->incrementObject (array (array ( $ keys [$ i ]) ,$ keyField ),$ input ,$ filters ,$ tables ,$ fields );
1616
1627
if ($ result ===null ) {
1617
1628
$ this ->db ->rollbackTransaction ();
1618
1629
return null ;
@@ -1664,20 +1675,19 @@ protected function findRelations($tables,$database,$auto_include) {
1664
1675
}
1665
1676
1666
1677
protected function retrieveInputs ($ data ) {
1667
- $ input = (object )array ();
1668
- if (strlen ($ data )>0 ) {
1669
- if ($ data [0 ]=='{ ' || $ data [0 ]=='[ ' ) {
1670
- $ input = json_decode ($ data );
1671
- } else {
1672
- parse_str ($ data , $ input );
1673
- foreach ($ input as $ key => $ value ) {
1674
- if (substr ($ key ,-9 )=='__is_null ' ) {
1675
- $ input [substr ($ key ,0 ,-9 )] = null ;
1676
- unset($ input [$ key ]);
1677
- }
1678
+ if (strlen ($ data )==0 ) {
1679
+ $ input = false ;
1680
+ } else if ($ data [0 ]=='{ ' || $ data [0 ]=='[ ' ) {
1681
+ $ input = json_decode ($ data );
1682
+ } else {
1683
+ parse_str ($ data , $ input );
1684
+ foreach ($ input as $ key => $ value ) {
1685
+ if (substr ($ key ,-9 )=='__is_null ' ) {
1686
+ $ input [substr ($ key ,0 ,-9 )] = null ;
1687
+ unset($ input [$ key ]);
1678
1688
}
1679
- $ input = (object )$ input ;
1680
1689
}
1690
+ $ input = (object )$ input ;
1681
1691
}
1682
1692
return is_array ($ input )?$ input :array ($ input );
1683
1693
}
@@ -1874,34 +1884,23 @@ protected function getParameters($settings) {
1874
1884
if ($ column_authorizer ) $ this ->applyColumnAuthorizer ($ column_authorizer ,$ action ,$ database ,$ fields );
1875
1885
1876
1886
// input
1877
- $ multi = (strpos ($ key [0 ],', ' )!==false ) || (strlen ($ post )?($ post [0 ]=='[ ' ):false );
1878
- if ($ before && $ action == 'delete ' && $ multi ) {
1879
- $ inputs = array ();
1880
- for ($ i =1 ; $ i <= count (explode (', ' , $ key [0 ])); $ i ++) {
1881
- $ inputs [] = (object ) array ();
1882
- }
1883
- } else {
1884
- $ inputs = $ this ->retrieveInputs ($ post );
1887
+ $ inputs = $ this ->retrieveInputs ($ post );
1888
+ if ($ before ) {
1889
+ $ this ->applyBeforeHandler ($ action ,$ database ,$ tables [0 ],$ key [0 ],$ before ,$ inputs );
1885
1890
}
1886
- $ originalAction = $ action ;
1891
+
1887
1892
foreach ($ inputs as $ k =>$ context ) {
1888
- //echo $k.' - ';
1889
1893
$ input = $ this ->filterInputByFields ($ context ,$ fields [$ tables [0 ]]);
1890
1894
1891
1895
if ($ tenancy_function ) $ this ->applyInputTenancy ($ tenancy_function ,$ action ,$ database ,$ tables [0 ],$ input ,$ fields [$ tables [0 ]]);
1892
1896
if ($ input_sanitizer ) $ this ->applyInputSanitizer ($ input_sanitizer ,$ action ,$ database ,$ tables [0 ],$ input ,$ fields [$ tables [0 ]]);
1893
1897
if ($ input_validator ) $ this ->applyInputValidator ($ input_validator ,$ action ,$ database ,$ tables [0 ],$ input ,$ fields [$ tables [0 ]],$ context );
1894
1898
1895
- $ this ->convertInputs ($ input ,$ fields [$ tables [0 ]]);
1896
-
1897
- if ($ before ) {
1898
- $ action = $ originalAction ;
1899
- $ this ->applyBeforeHandler ($ action ,$ database ,$ tables [0 ],$ ids [$ k ],$ before ,$ input );
1900
- }
1901
-
1899
+ $ this ->convertInputs ($ input ,$ fields [$ tables [0 ]]);
1902
1900
$ inputs [$ k ] = $ input ;
1903
1901
}
1904
- return compact ('action ' ,'database ' ,'tables ' ,'key ' ,'page ' ,'filters ' ,'fields ' ,'orderings ' ,'transform ' ,'multi ' ,'inputs ' ,'collect ' ,'select ' ,'before ' ,'after ' );
1902
+
1903
+ return compact ('action ' ,'database ' ,'tables ' ,'key ' ,'page ' ,'filters ' ,'fields ' ,'orderings ' ,'transform ' ,'inputs ' ,'collect ' ,'select ' ,'before ' ,'after ' );
1905
1904
}
1906
1905
1907
1906
protected function addWhereFromFilters ($ filters ,&$ sql ,&$ params ) {
@@ -2049,7 +2048,7 @@ protected function listCommandInternal($parameters) {
2049
2048
2050
2049
protected function readCommand ($ parameters ) {
2051
2050
extract ($ parameters );
2052
- if ($ multi ) $ object = $ this ->retrieveObjects ($ key ,$ fields ,$ filters ,$ tables );
2051
+ if (count ( $ key [ 0 ])> 1 ) $ object = $ this ->retrieveObjects ($ key ,$ fields ,$ filters ,$ tables );
2053
2052
else $ object = $ this ->retrieveObject ($ key ,$ fields ,$ filters ,$ tables );
2054
2053
if (!$ object ) $ this ->exitWith404 ('object ' );
2055
2054
$ this ->startOutput ();
@@ -2060,27 +2059,27 @@ protected function readCommand($parameters) {
2060
2059
protected function createCommand ($ parameters ) {
2061
2060
extract ($ parameters );
2062
2061
if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('input ' );
2063
- if ($ multi ) return $ this ->createObjects ($ inputs ,$ tables );
2062
+ if (count ( $ inputs )> 1 ) return $ this ->createObjects ($ inputs ,$ tables );
2064
2063
return $ this ->createObject ($ inputs [0 ],$ tables );
2065
2064
}
2066
2065
2067
2066
protected function updateCommand ($ parameters ) {
2068
2067
extract ($ parameters );
2069
2068
if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('subject ' );
2070
- if ($ multi ) return $ this ->updateObjects ($ key ,$ inputs ,$ filters ,$ tables );
2069
+ if (count ( $ inputs )> 1 ) return $ this ->updateObjects ($ key ,$ inputs ,$ filters ,$ tables );
2071
2070
return $ this ->updateObject ($ key ,$ inputs [0 ],$ filters ,$ tables );
2072
2071
}
2073
2072
2074
2073
protected function deleteCommand ($ parameters ) {
2075
2074
extract ($ parameters );
2076
- if ($ multi ) return $ this ->deleteObjects ($ key ,$ filters ,$ tables );
2075
+ if (count ( $ key [ 0 ])> 1 ) return $ this ->deleteObjects ($ key ,$ filters ,$ tables );
2077
2076
return $ this ->deleteObject ($ key ,$ filters ,$ tables );
2078
2077
}
2079
2078
2080
2079
protected function incrementCommand ($ parameters ) {
2081
2080
extract ($ parameters );
2082
2081
if (!$ inputs || !$ inputs [0 ]) $ this ->exitWith404 ('subject ' );
2083
- if ($ multi ) return $ this ->incrementObjects ($ key ,$ inputs ,$ filters ,$ tables ,$ fields );
2082
+ if (count ( $ inputs )> 1 ) return $ this ->incrementObjects ($ key ,$ inputs ,$ filters ,$ tables ,$ fields );
2084
2083
return $ this ->incrementObject ($ key ,$ inputs [0 ],$ filters ,$ tables ,$ fields );
2085
2084
}
2086
2085
0 commit comments