Hacked By AnonymousFox
plugin.min.js 0000644 00000010614 15177533563 0007203 0 ustar 00 tinymce.PluginManager.add("link",function(a){function b(b){return function(){var c=a.settings.link_list;"string"==typeof c?tinymce.util.XHR.send({url:c,success:function(a){b(tinymce.util.JSON.parse(a))}}):"function"==typeof c?c(b):b(c)}}function c(a,b,c){function d(a,c){return c=c||[],tinymce.each(a,function(a){var e={text:a.text||a.title};a.menu?e.menu=d(a.menu):(e.value=a.value,b&&b(e)),c.push(e)}),c}return d(a,c||[])}function d(b){function d(a){var b=l.find("#text");(!b.value()||a.lastControl&&b.value()==a.lastControl.text())&&b.value(a.control.text()),l.find("#href").value(a.control.value())}function e(b){var c=[];return tinymce.each(a.dom.select("a:not([href])"),function(a){var d=a.name||a.id;d&&c.push({text:d,value:"#"+d,selected:-1!=b.indexOf("#"+d)})}),c.length?(c.unshift({text:"None",value:""}),{name:"anchor",type:"listbox",label:"Anchors",values:c,onselect:d}):void 0}function f(){!k&&0===u.text.length&&m&&this.parent().parent().find("#text")[0].value(this.value())}function g(b){var c=b.meta||{};o&&o.value(a.convertURL(this.value(),"href")),tinymce.each(b.meta,function(a,b){l.find("#"+b).value(a)}),c.text||f.call(this)}function h(a){var b=v.getContent();if(/</.test(b)&&(!/^<a [^>]+>[^<]+<\/a>$/.test(b)||-1==b.indexOf("href=")))return!1;if(a){var c,d=a.childNodes;if(0===d.length)return!1;for(c=d.length-1;c>=0;c--)if(3!=d[c].nodeType)return!1}return!0}var i,j,k,l,m,n,o,p,q,r,s,t,u={},v=a.selection,w=a.dom;i=v.getNode(),j=w.getParent(i,"a[href]"),m=h(),u.text=k=j?j.innerText||j.textContent:v.getContent({format:"text"}),u.href=j?w.getAttrib(j,"href"):"",j?u.target=w.getAttrib(j,"target"):a.settings.default_link_target&&(u.target=a.settings.default_link_target),(t=w.getAttrib(j,"rel"))&&(u.rel=t),(t=w.getAttrib(j,"class"))&&(u["class"]=t),(t=w.getAttrib(j,"title"))&&(u.title=t),m&&(n={name:"text",type:"textbox",size:40,label:"Text to display",onchange:function(){u.text=this.value()}}),b&&(o={type:"listbox",label:"Link list",values:c(b,function(b){b.value=a.convertURL(b.value||b.url,"href")},[{text:"None",value:""}]),onselect:d,value:a.convertURL(u.href,"href"),onPostRender:function(){o=this}}),a.settings.target_list!==!1&&(a.settings.target_list||(a.settings.target_list=[{text:"None",value:""},{text:"New window",value:"_blank"}]),q={name:"target",type:"listbox",label:"Target",values:c(a.settings.target_list)}),a.settings.rel_list&&(p={name:"rel",type:"listbox",label:"Rel",values:c(a.settings.rel_list)}),a.settings.link_class_list&&(r={name:"class",type:"listbox",label:"Class",values:c(a.settings.link_class_list,function(b){b.value&&(b.textStyle=function(){return a.formatter.getCssText({inline:"a",classes:[b.value]})})})}),a.settings.link_title!==!1&&(s={name:"title",type:"textbox",label:"Title",value:u.title}),l=a.windowManager.open({title:"Insert link",data:u,body:[{name:"href",type:"filepicker",filetype:"file",size:40,autofocus:!0,label:"Url",onchange:g,onkeyup:f},n,s,e(u.href),o,p,q,r],onSubmit:function(b){function c(b,c){var d=a.selection.getRng();window.setTimeout(function(){a.windowManager.confirm(b,function(b){a.selection.setRng(d),c(b)})},0)}function d(){var b={href:e,target:u.target?u.target:null,rel:u.rel?u.rel:null,"class":u["class"]?u["class"]:null,title:u.title?u.title:null};j?(a.focus(),m&&u.text!=k&&("innerText"in j?j.innerText=u.text:j.textContent=u.text),w.setAttribs(j,b),v.select(j),a.undoManager.add()):m?a.insertContent(w.createHTML("a",b,w.encode(u.text))):a.execCommand("mceInsertLink",!1,b)}var e;return u=tinymce.extend(u,b.data),(e=u.href)?e.indexOf("@")>0&&-1==e.indexOf("//")&&-1==e.indexOf("mailto:")?void c("The URL you entered seems to be an email address. Do you want to add the required mailto: prefix?",function(a){a&&(e="mailto:"+e),d()}):a.settings.link_assume_external_targets&&!/^\w+:/i.test(e)||!a.settings.link_assume_external_targets&&/^\s*www\./i.test(e)?void c("The URL you entered seems to be an external link. Do you want to add the required http:// prefix?",function(a){a&&(e="http://"+e),d()}):void d():void a.execCommand("unlink")}})}a.addButton("link",{icon:"link",tooltip:"Insert/edit link",shortcut:"Meta+K",onclick:b(d),stateSelector:"a[href]"}),a.addButton("unlink",{icon:"unlink",tooltip:"Remove link",cmd:"unlink",stateSelector:"a[href]"}),a.addShortcut("Meta+K","",b(d)),a.addCommand("mceLink",b(d)),this.showDialog=d,a.addMenuItem("link",{icon:"link",text:"Insert/edit link",shortcut:"Meta+K",onclick:b(d),stateSelector:"a[href]",context:"insert",prependToContext:!0})}); category_form.class.php 0000644 00000004126 15200437077 0011231 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* Link category form class definition
* @package chamilo.link
*/
/**
* Init
*/
namespace Link;
/**
* Edit/create a LinkCategory.
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class CategoryForm extends \FormValidator
{
protected $category;
function __construct($form_name = 'category', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
{
parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
}
/**
*
* @return object
*/
public function get_category()
{
return $this->category;
}
public function set_category($value)
{
$this->category = $value;
}
/**
*
* @param \Link\LinkCategory $category
*/
function init($category = null)
{
$this->set_category($category);
$defaults = array();
$defaults['category_title'] = $category->category_title;
$defaults['category_description'] = $category->description;
$this->addElement('hidden', 'c_id', $category->c_id);
$this->addElement('hidden', 'id', $category->id);
$this->addElement('hidden', 'session_id', $category->session_id);
$form_name = $category->id ? get_lang('ModifyCategory') : get_lang('AddCategory');
$this->add_header($form_name);
$this->add_textfield('category_title', get_lang('Title'));
$this->addRule('category_title', get_lang('Required'), 'required');
$this->addElement('textarea', 'category_description', get_lang('Description'));
$this->addElement('button', 'save', get_lang('Save'), array('class' => 'btn save'));
$this->setDefaults($defaults);
}
function update_model()
{
$values = $this->exportValues();
$category = $this->get_category();
$category->category_title = $values['category_title'];
$category->description = $values['category_description'];
}
}
ajax_controller.class.php 0000644 00000015037 15200437077 0011562 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* Link controller (ajax) script
* @package chamilo.link
*/
/**
* Init
*/
namespace Link;
use \Model\Course;
use \CourseDescription;
use \CourseDescriptionRoutes;
use \Display;
use \Template;
use \FormValidator;
use \Security;
use \Uri;
use Header;
/**
* Ajax controller. Dispatch request and perform required action.
*
* - delete category/link
* - hide/show link
* - sort links/categories
*
* Usage:
*
* $controller = AjaxController::instance();
* $controller->run();
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class AjaxController extends \Controller
{
const ACTION_DELETE_CATEGORY = 'delete_category';
const ACTION_HIDE_LINK = 'hide_link';
const ACTION_SHOW_LINK = 'show_link';
const ACTION_DELETE_LINK = 'delete_link';
const ACTION_DELETE_BY_COURSE = 'delete_by_course';
const ACTION_SORT_CATEGORIES = 'sort_categories';
const ACTION_SORT_LINKS = 'sort_links';
const ACTION_VALIDATE_LINK = 'validate_link';
/**
* Return the instance of the controller.
*
* @return \Link\AjaxController
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
protected function __construct()
{
}
/**
* Prepare the environment. Set up breadcrumps and raise tracking event.
*/
protected function prolog()
{
event_access_tool(TOOL_LINK);
}
public function authorize()
{
$authorize = api_protect_course_script();
if (!$authorize) {
return false;
}
$c_id = Request::get_c_id();
if (empty($c_id)) {
return false;
}
if (Request::is_student_view()) {
return false;
}
if (!$this->is_allowed_to_edit()) {
return false;
}
return true;
}
public function is_allowed_to_edit()
{
$session_id = Request::get_session_id();
if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
return false;
}
// if (!Security::check_token('get')) {
// return false;
// }
if (!api_is_allowed_to_edit(false, true, true)) {
return false;
}
return true;
}
/**
*
*/
public function hide_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$success = LinkRepository::instance()->make_invisible($c_id, $id);
$this->response($success);
}
/**
*
*/
public function show_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$success = LinkRepository::instance()->make_visible($c_id, $id);
$this->response($success);
}
/**
*
*/
public function delete_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$link = (object) array();
$link->c_id = Request::get_c_id();
$link->id = Request::get_id();
$success = LinkRepository::instance()->remove($link);
$this->response($success);
}
/**
*
*/
public function delete_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->c_id = Request::get_c_id();
$category->id = Request::get_id();
$success = LinkCategoryRepository::instance()->remove($category);
$this->response($success);
}
/**
*
*/
public function delete_by_course()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$success_link = LinkRepository::instance()->remove_by_course($c_id, $session_id);
$success_cat = LinkCategoryRepository::instance()->remove_by_course($c_id, $session_id);
$this->response($success_link && $success_cat);
}
public function sort_categories()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$ids = Request::get_ids();
if (empty($ids)) {
return;
}
$repo = LinkCategoryRepository::instance();
$success = $repo->order($c_id, $ids);
$this->response($success);
}
public function sort_links()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$ids = Request::get_ids();
if (empty($ids)) {
return;
}
$repo = LinkRepository::instance();
$success = $repo->order($c_id, $ids);
$this->response($success);
}
public function validate_link()
{
$c_id = Request::get_c_id();
$id = Request::get_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$success = $link ? $link->validate() : false;
$this->response($success);
}
function forbidden()
{
$this->response(false, get_lang('YouAreNotAuthorized'));
}
public function unknown()
{
$this->response(false, get_lang('UnknownAction'));
}
/**
* Action exists but implementation is missing.
*/
public function missing()
{
$this->response(false, get_lang('NoImplementation'));
}
/**
* Display a standard json responce.
*
* @param bool $success
* @param string $message
* @param object $data
*/
public function response($success = false, $message = '', $data = null)
{
$message = trim($message);
$response = (object) array();
$response->success = $success;
if ($message) {
$response->message = Display::return_message($message, $success ? 'normal' : 'error');
} else {
$response->message = '';
}
$response->data = $data;
$this->render_json($response);
}
}
controller.class.php 0000644 00000034330 15200437077 0010554 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* Link controller script
* @package chamilo.link
*/
/**
* Init
*/
namespace Link;
use Security;
use Uri;
use Redirect;
use Chamilo;
use Javascript;
use Header;
/**
* Html controller. Dispatch request and perform required action:
*
* - list
* - add/edit/delete link
* - add/edit/delete category
* - make visible/invisible link
* - go to link target
*
* Note:
* Currently some actions are only implemented in the Ajax controller.
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class Controller extends \Controller
{
const ACTION_LISTING = 'listing';
const ACTION_VIEW = 'view';
const ACTION_ADD_LINK = 'add_link';
const ACTION_EDIT_LINK = 'edit_link';
const ACTION_DELETE_LINK = 'delete_link';
const ACTION_MAKE_VISIBLE = 'make_visisble';
const ACTION_MAKE_INVISIBLE = 'make_invisible';
const ACTION_ADD_CATEGORY = 'add_category';
const ACTION_EDIT_CATEGORY = 'edit_category';
const ACTION_DELETE_CATEGORY = 'delete_category';
const ACTION_GO = 'go';
const ACTION_IMPORT_CSV = 'import_csv';
const ACTION_EXPORT_CSV = 'export_csv';
const ACTION_DEFAULT = 'listing';
/**
*
* @return \Link\Controller
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
public function is_allowed_to_edit()
{
if (Request::is_student_view()) {
return false;
}
//$c_id = self::params()->get_c_id();
//$id = self::params()->get_id();
$session_id = Request::get_session_id();
if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
return false;
}
if (!api_is_allowed_to_edit(false, true, true)) {
return false;
}
return true;
}
/**
* Action to perform.
* Returns the request parameter.
*
* @return string
*/
public function get_action()
{
$result = parent::get_action();
// if (self::params()->is_student_view()) {
// if ($result != self::ACTION_LISTING && $result != self::ACTION_VIEW) {
// return self::ACTION_LISTING;
// }
// }
$result = $result ? $result : self::ACTION_DEFAULT;
return $result;
}
public function prolog()
{
event_access_tool(TOOL_LINK);
//legacy
global $interbreadcrumb;
$interbreadcrumb = array();
$interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Links'));
global $current_course_tool;
global $this_section;
global $nameTools;
$current_course_tool = TOOL_LINK;
$this_section = SECTION_COURSES;
$nameTools = get_lang('Links');
}
/**
* Whether the call is authorized or not.
*
* @return boolean
*/
public function authorize()
{
$authorize = api_protect_course_script();
if (!$authorize) {
return false;
}
$c_id = Request::get_c_id();
if (empty($c_id)) {
return false;
}
return true;
}
/**
* Javascript used by the controller
*
* @return string
*/
public function javascript()
{
$src = Chamilo::url('/main/link/resources/js/main.js');
$result = Javascript::tag($src);
$www = Chamilo::url();
$result .= Javascript::tag_code($code);
return $result;
}
/**
* Returns a url for an action that the controller can process
*
* @param string $action
* @param array $params
* @return string
*/
public function url($action, $params = array())
{
$url_params = Uri::course_params();
if ($c_id = Request::get_c_id()) {
$url_params[Request::PARAM_C_ID] = $c_id;
}
if ($id = Request::get_id()) {
$url_params[Request::PARAM_ID] = $id;
}
if($session_id = Request::get_session_id()){
$url_params[Request::PARAM_SESSION_ID] = $session_id;
}
$url_params[Request::PARAM_ACTION] = $action;
foreach ($params as $key => $value) {
$url_params[$key] = $value;
}
$result = Uri::url('/main/link/index.php', $url_params, false);
return $result;
}
public function listing()
{
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$root = (object) array();
$root->c_id = $c_id;
$root->id = 0;
$root->session_id = $session_id;
$links = LinkRepository::instance()->find_by_category($root);
$repo = LinkCategory::repository();
$categories = $repo->find_by_course($c_id, $session_id);
//$data = compact('links', 'categories');
$data = (object) array();
$data->categories = $categories;
$data->links = $links;
$this->render('index', $data);
}
public function add_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$link = new Link();
$link->id = 0;
$link->c_id = Request::get_c_id();
$link->session_id = Request::get_session_id();
/**
* @todo: ensure session_id is correctly defaulted
*/
$action = $this->url(self::ACTION_ADD_LINK);
$form = new LinkForm('link', 'post', $action);
$form->init($link);
if ($form->validate()) {
$form->update_model();
$repo = LinkRepository::instance();
$success = $repo->save($link);
$message = $success ? get_lang('LinkAdded') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddLink'));
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function import_csv()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$action = $this->url(self::ACTION_IMPORT_CSV);
$form = new UploadFileForm('import_csv', 'post', $action);
$form->init();
if ($form->validate()) {
$file = $form->get_file();
$path = $file['tmp_name'];
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$import = new ImportCsv($c_id, $session_id, $path);
$import->run();
//import_csvfile();
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function export_csv()
{
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$root = (object) array();
$root->c_id = $c_id;
$root->id = 0;
$root->session_id = $session_id;
$links = LinkRepository::instance()->find_by_category($root);
$repo = LinkCategory::repository();
$categories = $repo->find_by_course($c_id, $session_id);
$temp = Chamilo::temp_file();
$writer = \CsvWriter::create(new \FileWriter($temp));
$headers = array();
$headers[] = 'url';
$headers[] = 'title';
$headers[] = 'description';
$headers[] = 'target';
$headers[] = 'category_title';
$headers[] = 'category_description';
$writer->put($headers);
foreach ($links as $link) {
$data = array();
$data[] = $link->url;
$data[] = $link->title;
$data[] = $link->description;
$data[] = $link->target;
$data[] = '';
$data[] = '';
$writer->put($data);
}
foreach ($categories as $category) {
foreach ($category->links as $link) {
$data = array();
$data[] = $link->url;
$data[] = $link->title;
$data[] = $link->description;
$data[] = $link->target;
$data[] = $category->category_title;
$data[] = $category->description;
$writer->put($data);
}
}
\DocumentManager :: file_send_for_download($temp, true, get_lang('Links').'.csv');
}
public function delete_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function edit_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$id = Request::get_id();
$c_id = Request::get_c_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$action = $this->url(self::ACTION_EDIT_LINK);
$form = new LinkForm('link', 'post', $action);
$form->init($link);
if ($form->validate()) {
$form->update_model();
$success = $repo->save($link);
$message = $success ? get_lang('LinkUpdated') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditLink'));
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function make_visible()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function make_invisible()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function add_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->id = 0;
$category->c_id = Request::get_c_id();
$category->session_id = Request::get_session_id();
$category->category_title = '';
$category->description = '';
$category->display_order = 0;
$action = $this->url(self::ACTION_ADD_CATEGORY);
$form = new CategoryForm('category', 'post', $action);
$form->init($category);
if ($form->validate()) {
$form->update_model();
$repo = LinkCategoryRepository::instance();
$success = $repo->save($category);
$message = $success ? get_lang('CategoryAdded') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddCategory'));
$data = (object) array();
$data->form = $form;
$this->render('edit_category', $data);
}
public function edit_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$repo = LinkCategoryRepository::instance();
$category = $repo->find_one_by_id($c_id, $id);
$action = $this->url(self::ACTION_EDIT_CATEGORY);
$form = new CategoryForm('category', 'post', $action);
$form->init($category);
if ($form->validate()) {
$form->update_model();
$repo = LinkCategoryRepository::instance();
$success = $repo->save($category);
$message = $success ? get_lang('CategorySaved') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditCategory'));
$data = (object) array();
$data->form = $form;
$this->render('edit_category', $data);
}
public function delete_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->id = Request::get_id();
$category->c_id = Request::get_c_id();
$success = $repo = CategoryRepo::instance()->remove($category);
$message = $success ? get_lang('CategoryRemoved') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
public function go()
{
$id = Request::get_id();
$c_id = Request::get_c_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$url = $link->url;
event_link($id);
Header::cache_control('no-store, no-cache, must-revalidate');
Header::pragma('no-cache');
Redirect::go($url);
}
/**
* Render a template using data. Adds a few common parameters to the data array.
*
* @see /main/template/default/course_description/
* @param string $template
* @param array $data
*/
protected function render($template, $data)
{
$data = $data ? $data : (object) array();
$_user = api_get_user_info();
$session_id = Request::get_session_id();
$data->session_image = api_get_session_image($session_id, $_user);
$sec_token = Security::get_token();
$data->sec_token = $sec_token;
$context = Uri::course_params();
$data->root = Uri::url('/main/link/index.php', $context);
$data->session_id = $session_id;
$data->c_id = Request::get_c_id();
$data->is_allowed_to_edit = $this->is_allowed_to_edit();
parent::render("link/$template.tpl", $data);
}
}
import_csv.class.php 0000644 00000012412 15200437077 0010553 0 ustar 00 <?php
/* For licensing terms, see /license.txt */
/**
* Link CSV import class definition
* @package chamilo.link
*/
/**
* Init
*/
namespace Link;
/**
* Import a csv file into the course/session.
*
*
*
* @license /licence.txt
* @author Laurent Opprecht <laurent@opprecht.info>
*/
class ImportCsv
{
protected $c_id;
protected $session_id;
protected $path;
protected $links_imported = 0;
protected $links_skipped = 0;
protected $update_existing_entries = false;
public function __construct($c_id, $session_id, $path, $update_existing_entries = false)
{
$this->c_id = $c_id;
$this->session_id = $session_id;
$this->path = $path;
$this->update_existing_entries = $update_existing_entries;
}
public function get_path()
{
return $this->path;
}
public function get_c_id()
{
return $this->c_id;
}
public function get_session_id()
{
return $this->session_id;
}
public function get_links_imported()
{
return $this->links_imported;
}
public function get_links_skipped()
{
return $this->links_skipped;
}
public function get_update_existing_entries()
{
return $this->update_existing_entries;
}
/**
* Read file and returns an array filled up with its' content.
*
* @return array of objects
*/
public function get_data()
{
$result = array();
$path = $this->path;
if (!is_readable($path)) {
return array();
}
$items = \Import::csv_reader($path);
foreach ($items as $item) {
$item = (object) $item;
$url = isset($item->url) ? trim($item->url) : '';
$title = isset($item->title) ? trim($item->title) : '';
$description = isset($item->description) ? trim($item->description) : '';
$target = isset($item->target) ? trim($item->target) : '';
$category_title = isset($item->category_title) ? trim($item->category_title) : '';
$category_description = isset($item->category_description) ? trim($item->category_description) : '';
if (empty($url)) {
continue;
}
if ($category_title) {
$category_title = \Security::remove_XSS($category_title);
$category_description = \Security::remove_XSS($category_description);
} else {
$category_description = '';
}
$url = \Security::remove_XSS($url);
$title = \Security::remove_XSS($title);
$description = \Security::remove_XSS($description);
$target = \Security::remove_XSS($target);
$item->url = $url;
$item->title = $title;
$item->description = $description;
$item->target = $target;
$item->category_title = $category_title;
$item->category_description = $category_description;
$result[] = $item;
}
return $result;
}
public function run()
{
$path = $this->path;
if (!is_readable($path)) {
return false;
}
$this->links_imported = 0;
$this->links_skipped = 0;
$items = $this->get_data();
foreach ($items as $item) {
$url = $item->url;
$title = $item->title;
$description = $item->description;
$target = $item->target;
$category_title = $item->category_title;
$category_description = $item->category_description;
if ($category_title) {
$category = $this->ensure_category($category_title, $category_description);
}
$link = $this->find_link_by_url($url);
if ($link && $this->update_existing_entries == false) {
$this->links_skipped++;
continue;
}
if (empty($link)) {
$link = new Link();
$link->c_id = $this->c_id;
$link->session_id = $this->session_id;
$link->url = $url;
}
$link->title = $title;
$link->description = $description;
$link->target = $target;
$link->category_id = $category ? $category->id : 0;
$repo = LinkRepository::instance();
$repo->save($link);
$this->links_imported++;
}
}
public function ensure_category($title, $description = '')
{
$c_id = $this->c_id;
$session_id = $this->session_id;
$repo = LinkCategoryRepository::instance();
$result = $repo->find_one_by_course_and_name($c_id, $session_id, $title);
if (empty($result)) {
$result = new LinkCategory();
$result->c_id = $c_id;
$result->category_title = $title;
$result->description = $description;
$result->session_id = $session_id;
$repo->save($result);
}
return $result;
}
public function find_link_by_url($url)
{
$c_id = $this->c_id;
$session_id = $this->session_id;
$repo = LinkRepository::instance();
$link = $repo->find_one_by_course_and_url($c_id, $session_id, $url);
return $link;
}
}
index.html 0000644 00000000142 15200437077 0006543 0 ustar 00 <html>
<head>
<meta http-equiv="refresh" content="0; url=link.php">
</head>
<body>
</body>
</html>