@@ -12,16 +12,18 @@ public function __construct(BuyCraft $main){
1212 $ this ->main = $ main ;
1313 $ this ->categories = [];
1414 $ this ->packages = [];
15+ $ this ->pageSize = $ this ->main ->getConfig ()->get ('packagePageSize ' );
1516 }
1617 public function addCategory ($ id , $ name , $ desc , $ item ){
17- $ this ->categories [$ id ] = new Category ($ id , $ name , $ desc , $ item );
18+ $ this ->categories [] = new Category ($ id , $ name , $ desc , $ item, count ( $ this -> categories ) );
1819 }
1920 public function addPackage ($ categoryId , $ id , $ item , $ name , $ desc , $ price ){
20- if (isset ($ this ->categories [$ categoryId ])){
21- $ this ->packages [$ id ] = new Package ($ id , $ name , $ desc , $ price , $ item , $ this ->categories [$ categoryId ]);
21+ $ category = $ this ->getCategoryById ($ categoryId );
22+ if ($ category instanceof Category){
23+ $ this ->packages [] = new Package ($ id , $ name , $ desc , $ price , $ item , $ category );
2224 }
2325 else {
24- $ this ->packages [$ id ] = new Package ($ id , $ name , $ desc , $ price , $ item );
26+ $ this ->packages [] = new Package ($ id , $ name , $ desc , $ price , $ item );
2527 }
2628 }
2729 public function cleanCategories (){
@@ -30,18 +32,42 @@ public function cleanCategories(){
3032 unset($ this ->categories [$ i ]);
3133 }
3234 }
35+ foreach ($ this ->packages as $ i => $ p ){
36+ $ p ->setNiceId ($ i );
37+ }
3338 }
3439 public function getCategories (){
3540 return $ this ->categories ;
3641 }
37- public function getCategory ($ id ){
38- return $ this ->categories [$ id ];
42+ public function getCategory ($ niceId ){
43+ return (isset ($ this ->categories [$ niceId ]) ? $ this ->categories [$ niceId ] : false );
44+ }
45+ public function getCategoryById ($ id ){
46+ foreach ($ this ->getCategories () as $ category ){
47+ if ($ category ->getId () === $ id ){
48+ return $ category ;
49+ }
50+ }
51+ return false ;
3952 }
4053 public function getPackages (){
4154 return $ this ->packages ;
4255 }
43- public function getPackage ($ id ){
44- return $ this ->packages [$ id ];
56+ public function getPackage ($ niceId ){
57+ return (isset ($ this ->packages [$ niceId ]) ? $ this ->packages [$ niceId ] : false );
58+ }
59+ public function getPage ($ page = 0 , $ category = 0 ){
60+ $ start = $ page * $ this ->pageSize ;
61+ if ($ category === false ){
62+ $ outArray = array_slice ($ this ->getPackages (), $ start , $ this ->pageSize );
63+ }
64+ elseif ($ this ->getCategory ($ category ) instanceof Category){
65+ $ outArray = array_slice ($ this ->getCategory ($ category )->getPackages (), $ start , $ this ->pageSize );
66+ }
67+ else {
68+ $ outArray = false ;
69+ }
70+ return $ outArray ;
4571 }
4672 public function reset (){
4773 $ this ->categories = [];
0 commit comments