| @@ -197,19 +197,31 @@ | ||
| 197 | 197 | /** |
| 198 | 198 | * Save changes directly to current document |
| 199 | 199 | * |
| 200 | 200 | * @param array $fields |
| 201 | + * @param array $args | |
| 201 | 202 | * |
| 202 | 203 | * @return array |
| 203 | 204 | * @throws Exception |
| 204 | 205 | */ |
| 205 | - public function getList( array $fields = [] ) | |
| 206 | + public function getList( array $fields = [], $args = [] ) | |
| 206 | 207 | { |
| 207 | 208 | $columnsName = !empty( $fields ) ? $fields : ['id', 'name', 'slug', 'author', 'sections_count', 'created_at', 'modified_at', 'thumbnail', 'status']; |
| 208 | - $documents = $this->document()->reselect( $columnsName ) | |
| 209 | - ->where('parent', '0') | |
| 210 | - ->findAll()->get(); | |
| 209 | + $numberOfPages = ''; | |
| 210 | + if ( !empty( $args['page'] ) && !empty( $args['perPage'] ) ) { | |
| 211 | + $pager = $this->select( $fields )->paginate( $args['perPage'], $args['page'] ); | |
| 212 | + if ( $pager ) { | |
| 213 | + $numberOfPages = $pager->getNumberOfPages(); | |
| 214 | + $documents = $pager->getResults(); | |
| 215 | + } else { | |
| 216 | + $documents = []; | |
| 217 | + } | |
| 211 | 218 | |
| 219 | + } else { | |
| 220 | + $documents = $this->select( $fields )->findAll()->get(); | |
| 221 | + } | |
| 222 | + | |
| 223 | + | |
| 212 | 224 | $documents = $documents ? $documents->toArray() : []; |
| 213 | 225 | |
| 214 | 226 | if ( $documents && empty( $fields ) ) { |
| 215 | 227 | $uploadDir = wp_upload_dir(); |
| @@ -223,9 +235,33 @@ | ||
| 223 | 235 | } |
| 224 | 236 | } |
| 225 | 237 | } |
| 226 | 238 | |
| 239 | + if ( !empty( $numberOfPages ) ) { | |
| 240 | + return [ | |
| 241 | + 'page' => $args['page'], | |
| 242 | + 'perPage' => $args['perPage'], | |
| 243 | + 'numberOfPages' => $numberOfPages, | |
| 244 | + 'documents' => $documents | |
| 245 | + ]; | |
| 246 | + } | |
| 247 | + | |
| 227 | 248 | return $documents; |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Queries records of documents with specified fields | |
| 253 | + * | |
| 254 | + * @param array $fields | |
| 255 | + * | |
| 256 | + * @return Document | |
| 257 | + * @throws Exception | |
| 258 | + */ | |
| 259 | + public function select( array $fields = [] ) | |
| 260 | + { | |
| 261 | + $columnsName = !empty( $fields ) ? $fields : ['id', 'name', 'slug', 'author', 'sections_count', 'created_at', 'modified_at', 'thumbnail', 'status']; | |
| 262 | + return $this->document()->reselect( $columnsName ) | |
| 263 | + ->where('parent', '0'); | |
| 228 | 264 | } |
| 229 | 265 | |
| 230 | 266 | /** |
| 231 | 267 | * Save changes directly to current document |