Skip to content

Instantly share code, notes, and snippets.

@gupta2205
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gupta2205/b33dcf762876e5df34d9 to your computer and use it in GitHub Desktop.
Save gupta2205/b33dcf762876e5df34d9 to your computer and use it in GitHub Desktop.
Rearranging the order of featured item by editing the positions though dropdown menu,
<?php
use \LoudDoor\Services\Validators\AdminPetitionCreationForm;
use \LoudDoor\Services\Validators\AdminPetitionSponsorForm;
class AdminPetitionsController extends BaseController
{
public function index()
{
$Petitions = Petition::orderBy('flag_featured', 'desc')->orderBy('call_to_action', 'asc')->get();
Petition::populateShareStats($Petitions);
return View::make('admin.petitions.index', array(
'Petitions' => $Petitions,
'User' => Auth::User(),
));
}
public function edit($id = null)
{
if ($id)
{
$Petition = Petition::find($id);
if (!$Petition)
{
//oh noes! invalid petition specified!
Alert::error("That petition no longer exists");
}
}
else
{
$Petition = new Petition;
}
$featureList = Petition::getFeaturedPetitions();
$featureList = $featureList->lists('call_to_action','featured_sort_order');
foreach($featureList as $key => &$featureLists)
{
$featureList[$key] = 'After '. $featureLists;
}
return View::make('admin.petitions.edit', array(
'featureList' => $featureList,
'Petition' => $Petition,
'User' => Auth::User(),
));
}
public function post_edit($id = null)
{
if ($id)
{
$Petition = Petition::find($id);
if (!$Petition)
{
//oh noes! invalid petition specified!
Alert::error("That petition no longer exists");
return Redirect::action('AdminPetitionsController@index');
}
}
else
{
$Petition = new Petition;
}
$PetitionCreationForm = new AdminPetitionCreationForm;
$errors = array();
if ($PetitionCreationForm->passes())
{
$Petition->call_to_action = Input::get('call_to_action');
if (empty($Petition->id))
{
$Petition->slug = Str::slug($Petition->call_to_action);
}
$Petition->recipient = Input::get('recipient');
if (Input::get('feature_type') == '1')
{
$Petition->featured_sort_order = Input::get('featured_sort_order') + 1;
$Petition->flag_featured = 1;
}
else if(Input::get('feature_type') == '0')
{
$Petition->featured_sort_order=null;
$Petition->flag_featured = 0;
}
$Petition->description_md = Input::get('description_md');
$Petition->description = Petition::parseMD($Petition->description_md);
$Petition->letter_md = Input::get('letter_md');
$Petition->letter = Petition::parseMD($Petition->letter_md);
$Petition->target_signatures = Input::get('target_signatures', 50000);
$Petition->flag_published = Input::get('flag_published');
$Petition->media_type = Input::get('media_type', null);
if (Input::get('media_type') == 'img' && Input::hasFile('petition_image'))
{
$Petition->media_url = Petition::uploadFile(Input::file('petition_image'));
}
else if (Input::get('media_type') == 'youtube' && Input::get('media_url_youtube'))
{
$Petition->media_url = Input::get('media_url_youtube');
}
try {
try {
$SponsoringUser = User::where('email', Input::get('user.email'))->firstOrFail();
}
catch (Exception $e)
{
$PetitionSponsorForm = new AdminPetitionSponsorForm(Input::get('user'));
if ($PetitionSponsorForm->passes())
{
$SponsoringUser = new User;
$SponsoringUser->email = Input::get('user.email');
$SponsoringUser->first_name = Input::get('user.first_name');
$SponsoringUser->last_name = Input::get('user.last_name');
$SponsoringUser->populateLocation(Input::get('user.zip'));
$SponsoringUser->save();
}
else
{
throw new Exception();
}
}
$Petition->sponsor_user_id = $SponsoringUser->id;
$Petition->save();
$SponsoringUser->signPetition($Petition, 1, '');
// how to fix this part
//how can i use only $SponceringUser instead of $ProfileUser
$ProfileUser= $Petition->User;
if (Input::get('profile_type') == 'image' && Input::hasFile('profile_image'))
{
$ProfileUser->profile_img_url = Petition::uploadFile(Input::file('profile_image'));
}
else if (Input::get('profile_type') == 'url' && Input::get('profile_url'))
{
$ProfileUser->profile_img_url = Input::get('profile_url');
}
$ProfileUser->save();
$Petition->save();
$featureList = Petition::getFeaturedPetitions();
foreach($featureList as $featureLists)
{
if ($featureLists->featured_sort_order >= $Petition->featured_sort_order
&& $featureLists->id != $Petition->id)
{
$featureLists->featured_sort_order += 1;
}
}
$featureList->sort(function($left, $right)
{
if ($left->featured_sort_order == $right->featured_sort_order)
{
return 0;
}
return ($left->featured_sort_order < $right->featured_sort_order)? -1 : 1;
});
$i=1;
foreach($featureList as &$featureLists)
{
$featureLists->featured_sort_order = $i++;
$featureLists->save();
}
Alert::success("Petition {$Petition->call_to_action} has been updated!")->flash();
return Redirect::action('AdminPetitionsController@index');
}
catch (Exception $e)
{
$errors = $PetitionSponsorForm->getErrors();
}
}
else
{
$errors = $PetitionCreationForm->getErrors();
}
Alert::error("There was an error saving this petition")->flash();
if ($id)
{
$route = 'admin.petitions.edit';
}
else
{
$route = 'admin.petitions.create';
}
return Redirect::route($route, $Petition->id)
->withInput()
->withErrors($errors);
}
public function delete($id)
{
$Petition = Petition::find($id);
if (!$Petition)
{
//oh noes! invalid petition specified!
Alert::error("That petition no longer exists");
return Redirect::action('AdminPetitionsController@index');
}
$Petition->delete();
Alert::success("Petition " . e($Petition->call_to_action) . " has been successfully deleted")->flash();
return Redirect::route('admin.petitions');
}
public function stats($id)
{
$Petition = Petition::find($id);
if (!$Petition)
{
Alert::error("That petition no longer exists");
return Redirect::action('AdminPetitionsController@index');
}
$stats = array(
'all' => $Petition->getStats(),
'cur' => $Petition->getStats(date('Y-m-d')),
);
if (Input::get('date') && Input::get('date2'))
{
$stats['target'] = $Petition->getStats(Input::get('date'), Input::get('date2'));
}
return View::make('admin.petitions.stats', array(
'Petition' => $Petition,
'stats' => $stats,
'User' => Auth::User(),
));
}
public function CreateCSV($id)
{
$Petition = Petition::find($id);
if (!$Petition)
{
Alert::error("That petition no longer exists");
return Redirect::action('AdminPetitionsController@index');
}
$requested_period = Input::get('pd');
$start_date = Input::get('date');
$end_date = Input::get('date2');
if ($requested_period == 'cur')
{
$start_date = date('Y-m-d');
$end_date = date('Y-m-d');
}
elseif ($requested_period == 'all')
{
$start_end = $end_date = null;
}
$stats = $Petition->getCSV($start_date, $end_date);
$filename = $Petition->slug . ' ';
if ($start_date && $end_date)
{
//start date to readable string
$filename .= date_create($start_date)->format('n.d.Y');
$filename .= ' thru ';
//end date to readable string
$filename .= date_create($end_date)->format('n.d.Y');
$filename .= " Generated at " . date('n.d.Y H.i.s');
$filename .= '.csv';
}
else
{
$filename .= " - full export - ";
$filename .= date_create($Petition->created_at)->format('n.d.Y');
$filename .= ' thru ';
$filename .= date('n.d.Y');
$filename .= '.csv';
}
return CSV::fromArray($stats)->render($filename);
}
public function add_to_homepage($id, $placement = null)
{
$Petition = Petition::find($id);
$return = array('status' => true, 'payload' => null, 'message' => null, 'code' => null);
if (!$Petition)
{
$return['status'] = false;
$return['message'] = 'Invalid petition specified';
}
else
{
$Petition->flag_featured = 1;
$Petition->save();
}
return Response::json($return);
}
public function remove_from_homepage($id, $placement = null)
{
$Petition = Petition::find($id);
$return = array('status' => true, 'payload' => null, 'message' => null, 'code' => null);
if (!$Petition)
{
$return['status'] = false;
$return['message'] = 'Invalid petition specified';
}
else
{
$Petition->flag_featured = 0;
$Petition->save();
}
return Response::json($return);
}
}
@extends('layouts.admin')
@section('content')
<? if (!empty($errors)): ?>
<div class="form-errors">
<? foreach ($errors->all('<p class="error">:message</p>') as $error): ?>
<?= $error; ?>
<? endforeach; ?>
</div>
<? endif; ?>
<div class="petition-edit-form">
{{ Form::model($Petition, array(
'route' => array($Petition->exists? 'admin.petitions.post_edit' : 'admin.petitions.create', $Petition->id),
'method' => 'POST',
'files' => true,
)) }}
<div class="petition-info-fields">
<div class="field" id="flag_published_field">
{{ Form::label("flag_published_yes", "Publish petition?") }}
<br>
<label for="flag_published_yes">
{{ Form::radio("flag_published", '1', $Petition->flag_published == 1, array(
'id' => 'flag_published_yes'
)) }}
Yes
</label>
<label for="flag_published_no">
{{ Form::radio("flag_published", "0", $Petition->flag_published != 1, array(
'id' => 'flag_published_no'
)) }}
No
</label>
<div class="field" id="feature_type_field">
{{ Form::label("feature_yes", 'Feature?') }}
<br>
<label for="feature_yes" class="featured-status-featured">
{{ Form::radio('feature_type', '1', $Petition->feature_type== '1' || $Petition->flag_featured == '1', array(
'id' => 'feature_yes',
'class' => 'feature-type-selection add-featured',
)) }}
Yes
</label>
&nbsp;&nbsp;
<label for="feature_no" class="featured-status-not-featured">
{{ Form::radio('feature_type', '0', $Petition->feature_type == '0'|| $Petition->flag_featured == '0', array(
'id' => 'feature_no',
'class' => 'feature-type-selection rm-featured',
)) }}
No
</label>
<!-- post the code for dropdown menu here -->
<div class="field" id="dropdown_feature">
{{ Form::label('featured_sort_order', "Feature Position") }}
<br>
{{ Form::select('featured_sort_order',$featureList ,null, array(
'class' => 'form-control' ) ) }}
</div>
</div>
</div>
<div class="field" id="recipient_field">
{{ Form::label("recipient", "Who are you petitioning?") }}
<br>
{{ Form::text('recipient', null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field" id="call_to_action_field">
{{ Form::label('call_to_action', 'Call to action:') }}
<br>
{{ Form::text('call_to_action', null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field" id="description_field">
{{ Form::label('description_md', 'Why is this important') }}
<br>
{{ Form::textarea('description_md', null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field" id="letter_field">
{{ Form::label('letter_md', 'Petition letter') }}
<br>
{{ Form::textarea('letter_md', null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field submit-field" id="submit-petition-field">
<button type="submit" class="submit" id="save-petition-btn">
<span class="fa fa-save"></span> Save
</button>
<a href="<?= action('AdminPetitionsController@index'); ?>" class="cancel-btn">
<span class="fa fa-reply"></span> Cancel
</a>
</div>
</div>
<div class="petition-meta-data">
<div class="field" id="image-field">
<? if (!empty($Petition->media_url)): ?>
{{ Form::label('current_image', 'Current image') }}
<div class="current-image">
<? if ($Petition->media_type == 'img'): ?>
<img src="<?= $Petition->media_url; ?>">
<? else: ?>
<img src="http://img.youtube.com/vi/{{ $Petition->media_url }}/hqdefault.jpg">
<? endif; ?>
</div>
<? endif; ?>
<div class="row" id="media-url-row">
<div class="field" id="media-type-field">
{{ Form::label('media_type_img', 'Attachment type:') }}
<br>
<label for="media_type_img">
{{ Form::radio('media_type', 'img', $Petition->media_type == 'img' || $Petition->media_type == null, array(
'id' => 'media_type_img',
'class' => 'media-type-selection',
)) }}
Image
</label>
&nbsp;&nbsp;
<label for="media_type_youtube">
{{ Form::radio('media_type', 'youtube', $Petition->media_type == 'youtube', array(
'id' => 'media_type_youtube',
'class' => 'media-type-selection',
)) }}
Youtube Video
<label>
</div>
<div class="field" id="image_field">
{{ Form::label('petition_image', "Choose an image to attach") }}
<br>
{{ Form::file('petition_image') }}
</div>
<div class="field" id="media-url-youtube-field">
{{ Form::label('media_url_youtube', 'Youtube Video ID') }}
{{ Form::text('media_url_youtube', null, array(
'class' => 'form-control'
)) }}
</div>
</div>
<div class="field" id="target-signatures-field">
{{ Form::label('target_signatures', "Signature Goal") }}
{{ Form::text('target_signatures', null, array(
'placeholder' => '50,000',
'class' => 'form-control',
)) }}
</div>
<strong>Sponsoring User</strong>
<div class="field" id="sponsor-user-email-field">
{{ Form::label('user[email]', 'E-mail address') }}
{{ Form::text('user[email]', $Petition->id? $Petition->User->email : null, array(
'class' => 'form-control'
)) }}
</div>
<div class="row" id="sponsor-user-name-field">
<div class="field" id="sponsor-user-first-name-field">
{{ Form::label('user[first_name]', 'First name') }}
{{ Form::text('user[first_name]', $Petition->id? $Petition->User->first_name : null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field" id="sponsor-user-first-name-field">
{{ Form::label('user[last_name]', 'Last name') }}
{{ Form::text('user[last_name]', $Petition->id? $Petition->User->last_name : null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field zip" id="sponsor-user-zip-field">
{{ Form::label('user[zip]', 'Zip code') }}
{{ Form::text('user[zip]', $Petition->id? $Petition->User->zip : null, array(
'class' => 'form-control'
)) }}
</div>
<div class="field image" id="sponsor-user-image-field">
<div class="petition-creator-img" >
<img src="<?= ($Petition['User']['profile_img_url']); ?>" width="50" height="50" id="logo" >
<a href="javascript:void(0);" id="change-user-image">Change Image</a>
<p><?= e($Petition['User']['first_name']) . ' ' . e($Petition['User']['last_name']).', '.e($Petition['User']['city'] . ', ' . $Petition['User']['state']); ?></p>
</div>
</div>
<!-- code to change logo image -->
<!-- <div id="user_image_upload">
<div class="field" id="logo_upload">
{{ Form::label('logo_image', "Choose an image to attach") }}
<br>
{{ Form::file('logo_image') }}
</div>
<div class="field" id="logo_url">
{{ Form::label('logo_url_img', 'Image URL') }}
{{ Form::text('logo_url_img', null, array(
'class' => 'form-control'
)) }}
</div>
</div> -->
<div id="user_image_upload">
<div class="field" id="profile-type-field">
{{ Form::label('profile_type_img', 'Attachment type:') }}
<br>
<label for="profile_type_img">
{{ Form::radio('profile_type', 'image',null , array(
'id' => 'profile_type_img',
'class' => 'profile-type-selection',
)) }}
Image
</label>
&nbsp;&nbsp;
<label for="profile_type_url">
{{ Form::radio('profile_type', 'url', null, array(
'id' => 'profile_type_url',
'class' => 'profile-type-selection',
)) }}
Image URL
<label>
</div>
<div class="field" id="profile_field">
{{ Form::label('profile_image', "Choose profile image to attach") }}
<br>
{{ Form::file('profile_image') }}
</div>
<div class="field" id="profile-url-field">
{{ Form::label('profile_url', 'Image url') }}
{{ Form::text('profile_url', null, array(
'class' => 'form-control'
)) }}
</div>
</div>
<!-- -end of logo - -->
</div>
</div>
{{ Form::close() }}
</div>
@stop
@section('js')
<script type="text/javascript">
(function($)
{
$(function()
{
var selectedType = $('.media-type-selection:checked').val();
if (selectedType == 'img') showImgUpload();
else showYTInput();
$('input[name=media_type]').click(function(evt)
{
var cbox = $(evt.target).closest('input[type=radio]'),
val = cbox.val();
if (cbox.is(':checked') && val == 'img') showImgUpload();
else if (cbox.is(':checked') && val == 'youtube') showYTInput();
});
$('a#change-user-image').click(function(evt)
{
ChangeImage();
});
var selectedTypeFeature = $('.feature-type-selection:checked').val();
if (selectedTypeFeature == '1') showFeaturePosition();
else HideFeaturePosition();
$('input[name=feature_type]').click(function(evt)
{
var cbox = $(evt.target).closest('input[type=radio]'),
val = cbox.val();
if (cbox.is(':checked') && val == '1')
{
showFeaturePosition();
}
else if (cbox.is(':checked') && val == '0')
{
HideFeaturePosition();
}
});
var selectedTypeProfile = $('.profile-type-selection:checked').val();
if (selectedTypeProfile == 'image')
{
showProfileUpload();
}
else
{
showURLInput();
}
$('input[name=profile_type]').click(function(evt)
{
var box = $(evt.target).closest('input[type=radio]'),
val = box.val();
if (box.is(':checked') && val == 'image')
{
showProfileUpload();
}
else if (box.is(':checked') && val == 'url')
{
showURLInput();
}
});
//end of profile function
});
function showImgUpload()
{
$('#image_field').show();
$('#media-url-youtube-field').hide();
}
function showYTInput()
{
$('#image_field').hide();
$('#media-url-youtube-field').show();
}
function showProfileUpload()
{
$('#profile_field').show();
$('#profile-url-field').hide();
}
function showURLInput()
{
$('#profile_field').hide();
$('#profile-url-field').show();
}
function showFeaturePosition()
{
$('#dropdown_feature').show();
}
function ChangeImage()
{
$('#user_image_upload').show();
}
function HideFeaturePosition()
{
$('#dropdown_feature').hide();
}
// $(function()
// {
// var featureSelectedType = $('.feature-type-selection:checked').val();
// if (selectedType == '1') showFeaturePosition();
// $('input[name=feature_type]').click(function(evt)
// {
// var cbox = $(evt.target).closest('input[type=radio]'),
// val = cbox.val();
// if (cbox.is(':checked') && val == '1') showFeaturePosition();
// })
// });
})(jQuery);
</script>
@stop
<?php
public static function getFeaturedPetitions()
{
$Petitions = Petition::with(array('User'))
->where('flag_published', 1)
->where('flag_featured', 1)
->orderBy('featured_sort_order', 'asc')
->get();
return $Petitions; //->lists('call_to_action', 'id');
//return $Petitions;
}
<?php
class PetitionsController extends BaseController
{
public function index()
{
$Petitions = Petition::with(array('User'))
->where('flag_published', 1)
->where('flag_featured', 1)
->orderBy('featured_sort_order', 'asc')
->get();
return View::make('petitions.index')
->with(array(
'Petitions' => $Petitions,
'User' => User::loadFromSession(),
));
}
<?php
Route::get('/', 'PetitionsController@index');
Route::get('/admin/petitions/{id}/edit', array(
'as' => 'admin.petitions.edit',
'uses' => 'AdminPetitionsController@edit'
));
Route::post('/admin/petitions/{id}/edit', array(
'as' => 'admin.petitions.post_edit',
'uses' => 'AdminPetitionsController@post_edit'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment