mentors

mentors Git Source Tree


Root/app/controllers/EmailController.php

<?php

class EmailController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$sent_emails = SentEmail::all();
		$received    = ReceivedEmail::all();

		$user = Auth::user()->level;

		if(!$user == 'admin')
		{
			return View::make('users.profile')
							->with('sent', $sent_emails)
					        ->with('received', $received);
		}else
		{
			return View::make('admin.index')
			                ->with('sent', $sent_emails)
			                ->with('received', $received);
		}
												
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$user = Auth::user();

		return View::make('emails.create')->with('user', $user);
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$user_id      = Auth::id();
		$from_address = Auth::user()->email;

		$rules = array(
			'to'           => 'required|email',
			'subject'      => 'required',
			'message'      => 'required'
		);

		$valid = Validator::make(array(Input::only('to', 'subject', 'message')), $rules);

		if($valid->fails())
		{
			return Redirect::route('emails.create')
							->withInput(Input::all())
							->withErrors($valid);
		}else
		{
			$recepient = User::where('email', '=', Input::get('to'))->first();

			Mail::queue('users.welcome', array('name'=> $recepient->first), function($message) use ($recepient)
			{
				$message->to($recepient->email, $recepient->first . ' '. $recepient->last)->subject(\Input::get('subject'));
			});

			$sentEmail = SentEmail::create(array(
				'user_id'      => $user_id,
				'from_address' => $from_address,
				'to_address'   => Input::get('to'),
				'subject'      => Input::get('subject'),
				'message'      => Input::get('message')
			));

			if($sentEmail->save())
			{
				return Redirect::route('user');
			}else
			{
				Session::flash('message', 'Something bad happened.');

				return Redirect::route('emails.create')
								->withInput(Input::all());
			} 
		}
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$email = SentEmail::find($id);

		return View::make('emails.show')->with('email', $email);
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//cannot edit emails
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//cannot update emails
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$email = SentEmail::find($id);

		$email->delete();
	}

	public function sendEmail($address, $recepient, $subject)
	{
		return;
	}


}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.12037s using 11 queries.