/** * Read file and returns an array filled up with its' content. * * @return array of objects */ protected function read() { $result = array(); $path = $this->path; if (!is_readable($path)) { return array(); } $items = \Import::csv_reader($path); foreach ($items as $item) { $item = (object) $item; $title = isset($item->title) ? trim($item->title) : ''; $content = isset($item->content) ? trim($item->content) : ''; $type = isset($item->type) ? trim($item->type) : ''; $title = \Security::remove_XSS($title); $content = \Security::remove_XSS($content); $type = \Security::remove_XSS($type); $is_blank_line = empty($title) && empty($content) && empty($type); if ($is_blank_line) { continue; } $type = CourseDescriptionType::repository()->find_one_by_name($type); $type_id = $type ? $type->id : 0; $description = CourseDescription::create(); $description->title = $title; $description->content = $content; $description->description_type = $type_id; $result[] = $description; } return $result; }
/** * * @param array $descriptions */ public function add($descriptions) { $this->objects_imported = 0; $this->objects_skipped = 0; foreach ($descriptions as $description) { $title = $description->title; $content = $description->content; $type = $description->type; if (empty($type)) { $type = CourseDescriptionType::repository()->find_one_by_name('general'); $description->description_type = $type->id; } if (empty($title) || empty($content)) { $this->objects_skipped++; continue; } // $description = $this->find_by_title($title); // if ($description && $this->update_existing_entries == false) { // $this->objects_skipped++; // continue; // } $description->c_id = $this->course->c_id; $description->session_id = $this->course->session_id; $repo = CourseDescription::repository(); $success = $repo->save($description); if ($success) { $this->objects_imported++; } else { $this->objects_skipped++; } } }
/** * Return one type from its id * * @return \CourseDescription\CourseDescriptionType */ public function get_type() { $type_id = $this->get_description_type(); if ($this->type && $this->type->id == $type_id) { return $this->type; } $this->type = CourseDescriptionType::repository()->find_one_by_id($type_id); return $this->type; }