Saturday, September 26, 2015

Getting CKEditor to work with laravel 5.1

I rarely code in PHP these days but I had to use  Laravel, a great framework, for one of my project. My project required a WYSIWYG editor for its blogging section. I thought of giving ckeditor a try. The installation process was fairly simple.  All I had to do was place the necessary files/folders inside public directory (in my case) and then link the ckeditor.js file in my html. You can also use CDN to include ckeditor.The installation process was painless, however I found it very hard to get the file browser working. In this tutorial, I will talk about getting the file browser up and running.

During the time of writing this, my laravel version is 5.1.17(LTS). I am using Linux Mint 17.1 , sublime text 3 as my text editor, nginx as my server and mysql as my database. Except my laravel version and OS, the others are just extraneous, haha.

Ok, click in the editor`s image icon. You will see that there is no upload option.

To activate upload option, you have to add filebrowserImageUploadUrl.

CKEDITOR.replace('editor1',{
        filebrowserImageUploadUrl : "{{route('infos.upload',['_token' => csrf_token() ])}}",
        filebrowserWindowWidth  : 800,
        filebrowserWindowHeight : 500
    });



Here the filebrowserImageUploadUrl , will send a POST request to the route. If you remove the '_token' => csrf_token() you will get a tokenMismatch error.filebrowserWindowWidth & filebrowserWindowHeight is related with the appearance of the popup window`s dimension.

Lets create a route for 'info.upload'.

Route::post('infos/uploadImage',[
        'as' => 'infos.upload',
        'uses' => 'InfoController@uploadImage'
    ]);


uploadImage method in InfoController

public function uploadImage(Request $request)
    {
        $file = $request->file('upload');
        $uploadDestination = public_path() . '/uploads/about';
        $filename = preg_replace('/\s+/', '', $file->getClientOriginalName());
        $fileName = md5($filename) . "_" . $filename;
        $file->move($uploadDestination, $fileName);
    }


All the uploaded files will be upload to uploads/about folder, which is arbitrary.

At this point, your file should be uploaded. If not try opening your developers tool and check the network tab, there you can trace any errors.

Ok, now your file has been uploaded but you have to include it . To do that you have to add certain keys.
CKEDITOR.replace('editor1',{
        filebrowserBrowseUrl: "{{route('infos.image.browse')}}",
        filebrowserUploadUrl : '/browser/upload/type/all',
        filebrowserImageBrowseUrl: "{{route('infos.image.browse')}}",
        filebrowserImageUploadUrl : "{{route('infos.upload',['_token' => csrf_token() ])}}",
        filebrowserWindowWidth  : 800,
        filebrowserWindowHeight : 500
    });


Lets take a look at the routes now.

Route::post('infos/uploadImage',[
        'as' => 'infos.upload',
        'uses' => 'InfoController@uploadImage'
    ]);

    Route::get('infos/image/browse',[
        'as' => 'infos.image.browse',
        'uses' => 'InfoController@browseImage'
    ]);


This will how our controller looks right now:

public function uploadImage(Request $request)
    {
        $file = $request->file('upload');
        $uploadDestination = public_path() . '/uploads/about';
        $filename = preg_replace('/\s+/', '', $file->getClientOriginalName());
        $fileName = md5($filename) . "_" . $filename;
        $file->move($uploadDestination, $fileName);
    }

    public function browseImage(Request $request)
    {
        $test = $_GET['CKEditorFuncNum'];
        $images = [];
        $files = \File::files(public_path() . '/uploads/about');
        foreach ($files as $file) {
            $images[] = pathinfo($file);
        }
        return view('infos.file',[
            'files' => $images,
            'test' => $test
        ]);
     
    }


This will be the contents of our view file
@extends('app')

@section('content')
    @foreach($files as $file)
        <a href='{{url("uploads/about/".$file["basename"])}}'><img src='{{url("uploads/about/".$file["basename"])}}'></a>
    @endforeach
@endsection

@section('footer')
<script type="text/javascript">
$('a[href]').on('click', function(e){
    window.opener.CKEDITOR.tools.callFunction(<?php echo $test; ?>,$(this).find('img').prop('src'))
});
</script>
@endsection


This line of code window.opener.CKEDITOR.tools.callFunction(<?php echo $test; ?>,$(this).find('img').prop('src')) gets the url of clicked image and then put it into the url field.
That`s all there is to it.
If you run into any problem, leave a comment below. I hope to sort it out.

Monday, September 7, 2015

Alone at an altitude of 3800 m

 [Link to all the pictures that I took]

Trip cost break down

I paid Rs 426 for my ticket to Charikot.
I bought 2 kilo`s of guava at Rs 100.
I ate aloo and puri at Rs 50.
I bought Rs 600 worth of Churpee.
I paid Rs 250 for a room.
I paid Rs 50 for a bowl of hot noodle.
I paid Rs 130 for Daal-Bhaat-Aloo
I paid Rs 325 for my bus fare from Charikot to Kathmandu.

Tips and tricks for solo trekking


If anyone asks you whether you are traveling alone , tell them that your friends are coming behind you. That way if s/he is a potential threat, they will back off.

Put all your chargers in one polythene bag.






The sun had not yet peeped through its curtain of clouds when I boarded the so called super express bus from the old bus park and I was all set to go to Charikot. I had already bought my ticket a day before my solo trip. After 5 hours of bus ride, covering 150km from Kathmandu, I reached Charikot. The recent earthquake had its toll up to there too. 5 hrs of bus ride was not exhausting as there were a lot of waterfalls on the way. They were sort of eye candy for me.

Upon reaching Charikot, as unknown a tourist can be, I asked some locals for direction to Kuri. Carrying my 3kilo bag pack, I followed the direction. I had never traveled alone before and never had I carried anything while traveling. I would take just a few steps and before I know I would be resting. Since I had no plans of getting lost, I frequently asked the locals for direction. Little did I know, one of the fellow local was heading towards his village near Deurali.

Deurali
Deurali, is also the point in the route to get to Kuri. We talked about the shortes way to Kuri and shared guava that I had bought at Dolalghat. After an hour we reached Kuri and exchanged goodbyes.

Road to Kuri

From Deurali, I started to trek alone. I had read in some blogs that bears and leopards were common there. That information really motivated me to keep pacing until I reached to Kuri. Reaching Kuri was a far cry as the 3 kilo bag and the uphill ascend was holding me back, slowing me down.





My solo trip started to get really scary as the fog started to set in and the visibility was poor. Whenever the fog moved , the trees would make rustling noise and it would drizzle too. To make the scene even more scarier there were the roaring noise of waterfalls, scary bird`s call and the branches of the tress covered with mosses hanging down like in any ghost or thriller movies. I wanted to quit so bad but I had no idea how far Kuri was. I told myself ,"This whole solo trek is what I have been whining about from a year. Now I am doing it, why quit? Look, if I survive this, then I have one hell of a story to tell. SOLO , of all of the things I could do , I choose solo. How stupid have I become?". After 3 hours of walk I saw an old man appearing out of dense fog from the opposite direction. I was so happy to finally meet someone and again I asked him about the direction and time to reach Kuri.

After another hour of hiking, I heard someone chopping trees. I again asked him for direction and he told me that the nearest settlement was just 5 mins away.


 I reached there and talked with the locals.

There I bought 6 pieces of churpee. Since I had to reach Kuri the very same day, I had to bid them goodbye. After walking for another hour I finally met two young guys riding down on a motorcycle. I asked them about the direction and they told me it is just 10 mins away.

Visual of Kuri

Because the visibility was very poor I did not see Kuri until I reached near Kuri. I had to book the lodge as soon as possible as it was really cold up there. To satisfy my hunger I ate hot noodles. Since it was off-season there was not much options to choose from for dinner. So I settled for Daal-Bhaat-Aloo.

Kalinchowk hill

The morning of the very next day, I started my journey up to Kalinchowk. At an altitude of nearly 4000 m I was all alone. After I had finished worshiping to the goddess Kali, I started my descend which only took me about 30 mins.

Good bye Kalinchowk and Kuri
I had to leave for Charikot as soon as possible because if the fog would again set it , it would be another suicide mission. On the way I met a local who was traveling to Charikot to sell his Churpee and he took me to Charikot.