/**
  * Deleted checked coursecategories.
  */
 public function index_onDelete()
 {
     if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
         foreach ($checkedIds as $coursecategoryId) {
             if (!($coursecategory = CourseCategory::find($coursecategoryId))) {
                 continue;
             }
             $coursecategory->delete();
         }
         Flash::success(Lang::get('abnmt.mrc::lang.coursecategories.delete_selected_success'));
     } else {
         Flash::error(Lang::get('abnmt.mrc::lang.coursecategories.delete_selected_empty'));
     }
     return $this->listRefresh();
 }
Beispiel #2
0
 /**
  * Update Courses from CRM
  */
 public function onUpdateFromCrm()
 {
     // Flash::info("Processing Courses…");
     $crmcourses = CRMCourse::isNotUpdated()->with(['events', 'types', 'categories'])->get();
     if (count($crmcourses) > 0) {
         $crmcourses->each(function ($course) {
             $cmscourse = Course::firstOrNew(['crm_course_id' => $course->id]);
             $cmscourse->fill($course->toArray());
             $cmscourse->is_updated = true;
             $course->update(['is_updated' => true]);
             foreach ($course->types->toArray() as $type) {
                 $cmstype = CourseType::where('crm_type_id', $type['id'])->first();
                 if (!is_null($cmstype)) {
                     $course->types()->updateExistingPivot($type['id'], ['is_updated' => true]);
                     $cmscourse->type()->associate($cmstype);
                 }
             }
             foreach ($course->categories->toArray() as $category) {
                 $cmscategory = CourseCategory::where('crm_category_id', $category['id'])->first();
                 if (!is_null($cmscategory)) {
                     $course->categories()->updateExistingPivot($category['id'], ['is_updated' => true]);
                     $cmscourse->category()->associate($cmscategory);
                 }
             }
             $cmscourse->save();
             // foreach ($course->types->toArray() as $type) {
             //     $cmstype = CourseType::where('crm_type_id', $type['id'])->first();
             //     if (!is_null($cmstype) && !$cmscourse->types->contains($cmstype->id)) {
             //         $course->types()->updateExistingPivot($type['id'], ['is_updated' => true]);
             //         $cmscourse->types()->attach($cmstype->id, ['is_updated' => true]);
             //     }
             // }
             // foreach ($course->categories->toArray() as $category) {
             //     $cmscategory = CourseCategory::where('crm_category_id', $category['id'])->first();
             //     if (!is_null($cmscategory) && !$cmscourse->categories->contains($cmscategory->id)) {
             //         $course->categories()->updateExistingPivot($category['id'], ['is_updated' => true]);
             //         $cmscourse->categories()->attach($cmscategory->id, ['is_updated' => true]);
             //     }
             // }
             // echo " [+]\n";
         });
         // echo "\n";
         Flash::success("Courses is update");
     } else {
         Flash::info("No Courses for Create/Update");
     }
 }
Beispiel #3
0
 public function getResult()
 {
     $types = TypeModel::select('id', 'title', 'sort_order', 'description')->get()->toArray();
     $categories = CategoryModel::select('id', 'title', 'sort_order')->get()->toArray();
     $courses = CourseModel::select('id', 'title')->with(['types', 'categories'])->with(['events' => function ($q) {
         $q->where('date_start', '>', \Carbon::now())->where('is_closed', '<>', 1);
     }])->get();
     foreach ($types as $type_id => &$type) {
         // $_courses = array_filter($courses, function ($course) {
         //     return $course->types->contains('id', $type->id);
         // });
         $_courses = $courses->filter(function ($course) use($type) {
             return $course->types->contains('id', $type['id']);
         });
         $type['courses'] = $_courses;
         $_cat = [];
         foreach ($categories as $cat_id => &$category) {
             // $__courses = array_filter($_courses, function ($course) {
             //     return $course->categories->contains('id', $category->id);
             // });
             $__courses = $_courses->filter(function ($course) use($category) {
                 return $course->categories->contains('id', $category['id']);
             });
             $category['courses'] = $__courses;
             $_cat[] = $category;
         }
         $type['categories'] = $_cat;
     }
     // $types->each(function ($type) use ($categories, $courses) {
     //     $_courses = $courses->filter(function ($course) use ($type) {
     //         return $course->types->contains('id', $type->id);
     //     });
     //     $type['_courses'] = $_courses;
     //     $categories->each(function ($category) use ($_courses, &$type) {
     //         $__courses = $_courses->filter(function ($course) use ($category) {
     //             return $course->categories->contains('id', $category->id);
     //         });
     //         // $type->categories[$category->id]['__courses'] = $__courses;
     //         $category['__courses'] = $__courses;
     //         // CW::info(['__', $__courses]);
     //     });
     //     // $type['_categories'] = $categories;
     //     $type['_categories'] = $_cats;
     // });
     CW::info(['Result' => $types]);
     return;
 }
Beispiel #4
0
 /**
  * Execute the console command.
  * @return void
  */
 public function fire()
 {
     // $cmstypes = CMSType::all();
     echo "\nProcessing Types: \n";
     $crmtypes = CRMType::isNotUpdated()->get();
     if (count($crmtypes) > 0) {
         $crmtypes->each(function ($type) {
             $cmstype = CMSType::firstOrNew(['crm_type_id' => $type->id]);
             $cmstype->fill(['title' => $type->title]);
             $cmstype->is_updated = true;
             $type->is_updated = true;
             $cmstype->save();
             $type->save();
             echo " [^]\n";
         });
         echo "\n";
     } else {
         $this->info("  No Types for Create/Update");
     }
     echo "\nProcessing Categories: \n";
     $crmcategories = CRMCategory::isNotUpdated()->get();
     if (count($crmcategories) > 0) {
         $crmcategories->each(function ($category) {
             $cmscategory = CMSCategory::firstOrNew(['crm_category_id' => $category->id]);
             $cmscategory->fill(['title' => $category->title]);
             $cmscategory->is_updated = true;
             $category->is_updated = true;
             $cmscategory->save();
             $category->save();
             echo " [^]\n";
         });
         echo "\n";
     } else {
         $this->info("  No Categories for Create/Update");
     }
     echo "\nProcessing Courses: \n";
     $crmcourses = CRMCourse::isNotUpdated()->with(['events', 'types', 'categories'])->get();
     if (count($crmcourses) > 0) {
         $crmcourses->each(function ($course) {
             $cmscourse = CMSCourse::firstOrNew(['crm_course_id' => $course->id]);
             $cmscourse->fill($course->toArray());
             $cmscourse->is_updated = true;
             $course->update(['is_updated' => true]);
             $cmscourse->save();
             foreach ($course->types->toArray() as $type) {
                 $cmstype = CMSType::where('crm_type_id', $type['id'])->first();
                 if (!is_null($cmstype) && !$cmscourse->types->contains($cmstype->id)) {
                     $course->types()->updateExistingPivot($type['id'], ['is_updated' => true]);
                     $cmscourse->types()->attach($cmstype->id, ['is_updated' => true]);
                 }
             }
             foreach ($course->categories->toArray() as $category) {
                 $cmscategory = CMSCategory::where('crm_category_id', $category['id'])->first();
                 if (!is_null($cmscategory) && !$cmscourse->categories->contains($cmscategory->id)) {
                     $course->categories()->updateExistingPivot($category['id'], ['is_updated' => true]);
                     $cmscourse->categories()->attach($cmscategory->id, ['is_updated' => true]);
                 }
             }
             echo " [+]\n";
         });
         echo "\n";
     } else {
         $this->info("  No Courses for Create/Update");
     }
     echo "\nProcessing Events: \n";
     $crmevents = CRMEvent::isNotUpdated()->with(['course', 'types', 'categories'])->get();
     if (count($crmevents) > 0) {
         $crmevents->each(function ($event) {
             $cmsevent = CMSEvent::firstOrNew(['crm_event_id' => $event->id]);
             $cmsevent->fill($event->toArray());
             print_r($cmsevent->toArray());
             $cmscourse = CMSCourse::where('crm_course_id', $event->course->id)->first();
             // $cmstypes = CMSType::whereIn('crm_type_id', $event->types->pluck('id'))->get();
             // $cmscategories = CMSCategory::whereIn('crm_category_id', $event->categories->pluck('id'))->get();
             $cmsevent->course()->associate($cmscourse);
             $cmsevent->type()->associate($cmscourse->types->first());
             $cmsevent->category()->associate($cmscourse->categories->first());
             $cmsevent->is_updated = true;
             $event->update(['is_updated' => true]);
             // $cmsevent = $cmscourse->events()->save($cmsevent);
             $cmsevent->save();
             echo " [+]\n";
         });
         echo "\n";
     } else {
         $this->info("  No Events to Create/Update");
     }
     // echo "\nProcessing Courses Types: \n";
     // $_test = CRMType::courses()->get();
     // print_r($_test->toArray());
     // $crmcourses = CRMCourse::find(1)->types()->where('types.is_updated', '=', true)->get();
     // if (count($crmcourses) > 0) {
     //     print_r($crmcourses->toArray());
     // } else {
     //     $this->info("  No Courses Types to Create/Update");
     // }
     // $crmcourses = CRMCourse::with(['types'])->whereHas('types', function ($q) {
     //     // $q->wherePivot('is_updated', '=', false);
     //     $q
     //         ->where('type_id', '=', 1)
     //         // ->withPivot('is_updated')
     //         ->wherePivot('is_updated', '=', true)
     //     ;
     // })->get();
     // if (count($crmcourses) > 0) {
     //     print_r($crmcourses->toArray());
     // } else {
     //     $this->info("  No Courses Types to Create/Update");
     // }
 }
Beispiel #5
0
 /**
  *  List Courses
  */
 protected function listCourses()
 {
     // Assign params
     $params = $this->params;
     $type_id = $params['type'];
     $cat_id = $params['category'];
     $search = '';
     $post = post();
     // CW::info(['POST' => $post]);
     // Check POST
     if (count($post) > 0) {
         if (array_key_exists('type', $post)) {
             $type_id = $post['type'];
         }
         if (array_key_exists('category', $post)) {
             $cat_id = $post['category'] | 0;
         }
         if (array_key_exists('search', $post)) {
             $search = $post['search'] | "";
         }
     }
     // Fetch All Types
     $types = TypeModel::select('id', 'title', 'sort_order', 'description', 'content');
     if ($type_id != 0) {
         $types = $types->where('id', $type_id);
     }
     $types = $types->get()->toArray();
     CW::info(['Types' => TypeModel::all()]);
     // Fetch All Categories
     $categories = CategoryModel::select('id', 'title', 'sort_order');
     if ($cat_id != 0) {
         $categories = $categories->where('id', $cat_id);
     }
     $categories = $categories->get()->toArray();
     $courses = new CourseModel();
     // Search query
     $search = trim($search);
     if (strlen($search)) {
         $searchableFields = ['title', 'content'];
         $courses = $courses->searchWhere($search, $searchableFields);
     }
     // Main query
     $courses = $courses->select('id', 'title', 'content', 'course_type_id', 'course_category_id', 'description_long')->with(['type' => function ($q) {
         $q->select('id');
     }, 'category' => function ($q) {
         $q->select('id');
     }, 'events' => function ($q) {
         $q->where('date_start', '>', \Carbon::now())->where('is_closed', '<>', 1)->orderBy('date_start', 'asc')->select('id', 'date_start', 'date_end', 'price', 'hours', 'course_id', 'teacher');
     }])->get();
     // Check logged User and fetch UserCourses (UserEvents) with States
     if (!is_null($cmsuser = $this->user())) {
         $user_courses = $cmsuser->userevents()->with(['course', 'state'])->get();
         // CW::info(['user_courses', $user_courses]);
     } else {
         $user_courses = null;
     }
     // Preparing Result
     $_types = [];
     foreach ($types as &$type) {
         // Filter Courses list by Type
         $_courses = $courses->filter(function ($course) use($type) {
             // return $course->types->contains('id', $type['id']);
             return $course->course_type_id == $type['id'];
         });
         // Skip empty courses type
         if ($_courses->count() == 0) {
             continue;
         }
         $type['courses'] = $_courses;
         // JSON fix
         if (is_string($type['content'])) {
             $type['content'] = json_decode($type['content'], true);
         }
         $_categories = [];
         foreach ($categories as &$category) {
             // Filter Courses list by Category
             $__courses = $_courses->filter(function ($course) use($category) {
                 // return $course->categories->contains('id', $category['id']);
                 return $course->course_category_id == $category['id'];
             });
             // Skip empty courses type
             if ($__courses->count() == 0) {
                 continue;
             }
             $__courses = $__courses->each(function (&$course) {
                 // CW::info(['Events Count' => $course->events->count()]);
                 // Add Last Event to Course
                 if (!$course->events->count()) {
                     $last_event = $course->events()->orderBy('date_start', 'desc')->select('id', 'price', 'hours', 'course_id', 'teacher')->take(1)->first();
                     $last_event['last'] = true;
                     $course['events'][] = $last_event;
                     // CW::info(['Last Event' => $course]);
                 }
             });
             // Sorting Courses by Event dates
             $__courses = $__courses->sortByDesc(function ($course) {
                 if ($course->events->first() && ($start = $course->events->first()->date_start)) {
                     return -$start->timestamp;
                 } else {
                     return false;
                 }
             });
             // CW::info(['Courses' => $__courses]);
             $___courses = $__courses->toArray();
             foreach ($___courses as &$course) {
                 // Fetch UserEvents with States
                 if (!is_null($user_courses) && !is_null($user_course = $user_courses->where('course_id', $course['id'])->where('course_type_id', $type['id'])->where('course_category_id', $category['id'])->first())) {
                     // extract($user_course->toArray());
                     $course['state'] = $user_course->state->title;
                 }
                 // CW::info(['Event Count' => count($course['events'])]);
                 // if ($events_count = count($course['events']) == 0) {
                 //     $last_event = CourseModel::where('id', $course['id'])
                 //         ->with([
                 //             'events' => function ($q) {
                 //                 $q
                 //                     ->orderBy('date_start', 'desc')
                 //                     ->select('id', 'date_start', 'date_end', 'price', 'hours', 'course_id', 'teacher')
                 //                     ->take(1)
                 //                 ;
                 //             },
                 //         ])
                 //         ->get()
                 //     ;
                 //     CW::info(['Last Event' => $last_event]);
                 // }
                 // Assign fast select fields
                 $course['prices'] = [];
                 $course['teachers'] = [];
                 $course['hours'] = [];
                 foreach ($course['events'] as $event) {
                     if (trim($event['price']) != '') {
                         $course['prices'][] = $event['price'];
                     }
                     if (trim($event['teacher']) != '') {
                         $course['teachers'][] = $event['teacher'];
                     }
                     if (trim($event['hours']) != '') {
                         $course['hours'][] = $event['hours'];
                     }
                 }
                 // CW::info($course);
             }
             $category['courses'] = $___courses;
             $_categories[$category['id']] = $category;
         }
         $type['categories'] = $_categories;
         $_types[$type['id']] = $type;
     }
     //
     CW::info(['Result' => $_types]);
     return $_types;
 }
Beispiel #6
0
 public function run()
 {
     // // Models
     // $data = [
     //     'Abnmt\MRC\Models\Course' => require_once 'data/courses.php',
     //     // 'Abnmt\MRC\Models\Person'      => $this->MultiSort(require_once 'data/people.php', ['family_name' => [SORT_ASC, SORT_STRING]]),
     // ];
     // // $path     = "./storage/app/images";
     // // $fileData = $this->fillArrayWithFileNodes(new \DirectoryIterator($path), ["jpg", "png"]);
     // // print_r($fileData);
     $types = (require_once 'data/types.php');
     $categories = (require_once 'data/categories.php');
     // $states     = require_once 'data/states.php';
     echo "types\n";
     foreach ($types as $type) {
         \Abnmt\MRC\Models\CourseType::create($type);
     }
     echo "cats\n";
     foreach ($categories as $category) {
         \Abnmt\MRC\Models\CourseCategory::create($category);
     }
     // echo "states\n";
     // foreach ($states as $state) {
     //     \Abnmt\MRC\Models\UserEventState::create($state);
     // }
     // echo "courses ";
     // foreach ($data as $modelName => $models) {
     //     foreach ($models as $model) {
     //         echo "+";
     //         $model = $this->createModel($modelName, $model);
     //         // $this->assignImages($model, $fileData);
     //     }
     // }
     // echo "\n";
     // // RANDOM Events
     // $types      = \Abnmt\MRC\Models\CourseType::all();
     // $categories = \Abnmt\MRC\Models\CourseCategory::all();
     // $courses    = \Abnmt\MRC\Models\Course::all();
     // $_hours  = [120, 18, 20, 24, 28, 34, 40, 72];
     // $_prices = [0, 10000, 14000, 16000, 18000, 3700, 5000, 6000, 6500, 7000, 8000];
     // $events = array_fill(0, 60, 'bla bla');
     // echo "events ";
     // foreach ($events as $event) {
     //     echo "+";
     //     $course = $courses->random();
     //     $type   = $course->types()->get()->random();
     //     // $category = $course->categories()->where('id', 2)->first();
     //     $category = $course->categories()->get()->random();
     //     $title = $course->title;
     //     $hours = $_hours[array_rand($_hours)];
     //     $price = $_prices[array_rand($_prices)];
     //     // echo $now = \Carbon::now()->toDateTimeString();
     //     $now = \Carbon::now()->format('Y-m-d');
     //     $now = strtotime($now);
     //     $date = \Carbon::createFromTimestamp($now)->subWeek(1)->addWeek(rand(1, 10))->addDay(rand(1, 7));
     //     echo " ";
     //     echo $date_start = $date->format('Y-m-d');
     //     echo " ";
     //     echo $date_end = $date->addDay(rand(4, 14))->format('Y-m-d');
     //     echo "\n";
     //     $event = compact('title', 'course', 'type', 'category', 'hours', 'price', 'date_start', 'date_end');
     //     \Abnmt\MRC\Models\Event::create($event);
     // }
     // echo "\n";
     // // News
     // $news = require_once 'data/news.php';
     // echo "news\n";
     // $fileData = $this->fillArrayWithFileNodes(new \DirectoryIterator('./plugins/abnmt/mrc/updates/data/media'), ["jpg", "png"]);
     // // print_r($fileData);
     // foreach ($news as $post) {
     //     echo "+";
     //     $now = \Carbon::now()->format('Y-m-d H:i');
     //     $now = strtotime($now);
     //     $post['published_at'] = \Carbon::createFromTimestamp($now)->subWeek(3)->addWeek(rand(0, 6))->addDay(rand(0, 7))->addHour(rand(0, 24))->addMinute(rand(0, 60));
     //     $post['is_published'] = true;
     //     $model = \Abnmt\MRC\Models\News::create($post);
     //     if (array_key_exists($model->slug, $fileData)) {
     //         $filePath = $fileData[$model->slug];
     //         if (!is_array($filePath)) {
     //             $pathinfo = pathinfo($filePath);
     //             $check    = File::where('attachment_id', '=', $model->id)
     //                 ->where('attachment_type', '=', get_class($model))
     //                 ->where('file_name', '=', $pathinfo['basename'])
     //             // ->where('field', '=', $pathinfo['filename'])
     //                 ->first();
     //             if (!is_null($check)) {
     //                 // echo $filePath . " ";
     //                 // echo filemtime($filePath) . " ";
     //                 // echo $check->updated_at->timestamp . "\n";
     //                 if (filemtime($filePath) > $check->updated_at->timestamp) {
     //                     // echo "File " . $filePath . " is Newer. Update!" . "\n";
     //                     echo "[^]";
     //                     $check->delete();
     //                 } else {
     //                     echo "[~]";
     //                     continue;
     //                 }
     //             } else {
     //                 // echo "File " . $filePath . " is New. Create!" . "\n";
     //                 echo "[+]";
     //             }
     //             $file = new File();
     //             $file->fromFile($filePath);
     //             $model->cover()->save($file, null, ['title' => $model->title]);
     //         }
     //     }
     // }
     // echo "\n";
 }
Beispiel #7
0
 /**
  *  List Courses
  */
 protected function listCourses()
 {
     $params = $this->params;
     $type_id = $params['type'];
     $cat_id = $params['category'];
     $types = TypeModel::select('id', 'title', 'sort_order', 'description');
     if ($type_id != 0) {
         $types = $types->where('id', $type_id);
     }
     $types = $types->get()->toArray();
     $categories = CategoryModel::select('id', 'title', 'sort_order');
     if ($cat_id != 0) {
         $categories = $categories->where('id', $cat_id);
     }
     $categories = $categories->get()->toArray();
     if (is_null($cmsuser = $this->user())) {
         return null;
     }
     // $user_courses = $cmsuser->userevents()->with(['course', 'course.events'])->get();
     // CW::info(['user_courses', $user_courses]);
     // $courses = $courses->select('id', 'title', 'content', 'course_type_id', 'course_category_id')
     $userevents = $cmsuser->userevents()->with(['state', 'event', 'course' => function ($q) {
         $q->with(['events' => function ($q) {
             $q->where('date_start', '>', \Carbon::now())->where('is_closed', '<>', 1)->orderBy('date_start', 'asc')->select('id', 'date_start', 'date_end', 'price', 'hours', 'course_id', 'course_type_id', 'course_category_id', 'teacher');
         }]);
     }])->get();
     // CW::info(['userevents' => $userevents]);
     // CW::info(['events' => $cmsuser->events()->get()]);
     if (!$userevents) {
         return;
     }
     // Delete deleted UserEvents
     $userevents->each(function ($userevent) {
         if ($userevent->state_id == 9) {
             $userevent->delete();
         }
     });
     // Filter UserEvents from deleted UserEvents
     $userevents = $userevents->filter(function ($userevent) {
         return $userevent->state_id != 9;
     });
     //
     $_types = [];
     foreach ($types as $type_id => &$type) {
         $_userevents = $userevents->filter(function ($course) use($type) {
             return $course->course_type_id == $type['id'];
         });
         // CW::info(['_userevents' => $_userevents]);
         if ($_userevents->count() == 0) {
             continue;
         }
         $type['userevents'] = $_userevents;
         $_categories = [];
         foreach ($categories as $cat_id => &$category) {
             $__userevents = $_userevents->filter(function ($course) use($category) {
                 return $course->course_category_id == $category['id'];
             });
             // CW::info(['__userevents' => $_userevents]);
             if ($__userevents->count() == 0) {
                 continue;
             }
             // Sorting Courses by Event dates
             $__userevents = $__userevents->sortByDesc(function ($userevent) {
                 CW::info(['UserEvent' => $userevent]);
                 if ($userevent->course->events->first() && ($start = $userevent->course->events->first()->date_start)) {
                     return -$start->timestamp;
                 } else {
                     return false;
                 }
             });
             $___userevents = $__userevents->toArray();
             foreach ($___userevents as $course_id => &$course) {
                 // CW::info(['userevent_course' => $course]);
                 // $events = $course['course']['events'];
                 // CW::info(is_null($course['course']));
                 if (is_null($course['course'])) {
                     continue;
                 }
                 // $course = array_merge($course, $course['course']);
                 // CW::info(array_merge($course, $course['course']));
                 // CW::info($course);
                 $course['title'] = $course['course']['title'];
                 $course['description_long'] = $course['course']['description_long'];
                 $course['events'] = collect($course['course']['events'])->filter(function ($event) use($type, $category) {
                     return $event['course_type_id'] == $type['id'] && $event['course_category_id'] == $category['id'];
                 });
                 $course['prices'] = [];
                 $course['teachers'] = [];
                 $course['hours'] = [];
                 foreach ($course['events'] as $event) {
                     if (trim($event['price']) != '') {
                         $course['prices'][] = $event['price'];
                     }
                     if (trim($event['teacher']) != '') {
                         $course['teachers'][] = $event['teacher'];
                     }
                     if (trim($event['hours']) != '') {
                         $course['hours'][] = $event['hours'];
                     }
                 }
             }
             $category['userevents'] = $___userevents;
             $_categories[$category['id']] = $category;
         }
         $type['categories'] = $_categories;
         $_types[$type['id']] = $type;
     }
     //
     CW::info(['Result' => $_types]);
     return $_types;
 }