Skip to content

Instantly share code, notes, and snippets.

@Tmw
Created November 17, 2011 09:09
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 Tmw/1372754 to your computer and use it in GitHub Desktop.
Save Tmw/1372754 to your computer and use it in GitHub Desktop.
A overview of our usersController
<?php
class UsersController extends AppController
{
function index()
{
$userName = $this->Session->read('User.username');
if($userName == null){
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
if($this->Session->read('Pass.confirm' == 'True'))
{
$this->set('msg', 'Uw nieuwe wachtwoord is succesvol opgeslagen');
}
}
function edit_user()
{
$this->Session->write('Company.new', 'False');
if($this->Session->read('Pass.error') == 'True')
{
$this->set('msg', 'Het door u opgegeven oude wachtwoord is foutief.');
}
}
function new_user(){}
function new_company()
{
$this->loadModel('Category');
$categories = $this->Category->find('list');
array_unshift($categories, '');
$this->set('categories', $categories);
}
function new_category(){}
function new_subcategory($id)
{
$this->set('categoryId', $id);
}
function new_location($id)
{
$this->loadModel('Category');
$categoryDetails = $this->Category->find('list');
$this->set('categoryDetails', $categoryDetails);
$this->set('companyId', $id);
}
function new_contact($id)
{
$this->set('locationId', $id);
}
function new_media($id)
{
$this->set('locationId', $id);
}
function new_location_category($locationId)
{
$this->loadModel('Category');
$this->loadModel('Location');
$categoryDetails = $this->Category->find('list');
$companyDetails = $this->Location->query('SELECT * FROM locations WHERE id = ' . $locationId);
$this->set('categoryDetails', $categoryDetails);
$this->set('companyId', $companyDetails);
$this->set('locationId', $locationId);
}
function add_media()
{
$this->loadModel('Media');
$newMediaName = $this->data['User']['mediaName'];
$newMediaUrl = $this->data['User']['mediaUrl'];
$newMediaDetails = $this->data['User']['mediaDetails'];
$locationId = $this->data['User']['locationId'];
$newMediaData = array('Media' => array('name' => $newMediaName, 'url' => $newMediaUrl, 'details' => $newMediaDetails, 'location_id' => $locationId));
$this->Media->save($newMediaData);
$this->redirect(array('controller' => 'users', 'action' => 'medias/'.$locationId));
}
function add_user()
{
$newUserName = $this->data['User']['newUsername'];
$newUserPassword = $this->data['User']['newPassword'];
$userData = array('User' => array('email' => $newUserName, 'password' => $newUserPassword));
$this->User->save($userData);
$this->redirect(array('controller' => 'users', 'action' => 'index'));
}
function add_category()
{
$this->loadModel('Category');
$newCategoryName = $this->data['User']['categoryName'];
$categoryData = array('Category' => array('name' => $newCategoryName));
$this->Category->save($categoryData);
$this->redirect(array('controller' => 'users', 'action' => 'categories'));
}
function add_subcategory()
{
$this->loadModel('Subcategory');
$newsubcategory = $this->data['User']['subCategoryName'];
$categoryId = $this->data['User']['categoryId'];
$newSubCategoryData = array('Subcategory' => array('name' => $newsubcategory, 'category_id' => $categoryId));
$this->Subcategory->save($newSubCategoryData);
$this->redirect(array('controller' => 'users', 'action' => 'subcategories/' . $categoryId));
}
function add_location()
{
$this->loadModel('Location');
$newLocationAdress = $this->data['User']['locationAdress'];
$newLocationPostcode = $this->data['User']['locationpostcode'];
$newLocationPlaats = $this->data['User']['locationPlaats'];
$newLocationCategoryId = $this->data['User']['subCategory'];
$companyId = $this->data['User']['companyId'];
$getCords = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='. urlencode($newLocationAdress));
$cordResult = json_decode($getCords);
$latitude = $cordResult->results[0]->geometry->location->lat;
$longitude = $cordResult->results[0]->geometry->location->lng;
$newLocationData = array('Location' => array('adress' => $newLocationAdress, 'postcode' => $newLocationPostcode, 'plaats' => $newLocationPlaats, 'longitude' => $longitude, 'latitude' => $latitude, 'company_id' => $companyId));
$this->Location->save($newLocationData);
//add category relation
$conditionNewLocationId = array('Location.adress' => $newLocationAdress);
$newLocationId = $this->Location->find('first', array('conditions' => $conditionNewLocationId));
if(!empty($newLocationId))
{
$this->loadModel('Cat_comp');
$newCatCompData = array('company_id' => $companyId, 'subcategory_id' => $newLocationCategoryId, 'location_id' => $newLocationId['Location']['id']);
$this->Cat_comp->save($newCatCompData);
$this->redirect(array('controller' => 'users', 'action' => 'locations/' . $companyId));
}
}
function add_contact()
{
$this->loadModel('Contact');
$newContactName = $this->data['User']['contactName'];
$newContactPhone = $this->data['User']['contactPhone'];
$newContactTime = $this->data['User']['contactTime'];
$newContactEmail = $this->data['User']['contactEmail'];
$locationId = $this->data['User']['locationId'];
$newContactData = array('Contact' => array('name' => $newContactName, 'phone' => $newContactPhone, 'time' => $newContactTime, 'email' => $newContactEmail, 'location_id' => $locationId));
$this->Contact->save($newContactData);
$this->redirect(array('controller' => 'users', 'action' => 'contacts/' . $locationId));
}
function dynamicSubCatSelectBox()
{
$this->autoRender = false;
$this->layout = 'ajax';
$this->loadModel('Subcategory');
$subCategories = $this->Subcategory->findAllByCategoryId($_GET['categoryId']);
echo json_encode($subCategories);
}
function add_new_location_category()
{
$this->loadModel('Cat_comp');
$locationId = $this->data['User']['locationId'];
$companyId = $this->data['User']['companyId'];
$subCatId = $this->data['User']['subCategory'];
$categoryData = array('Cat_comp' => array('company_id' => $companyId, 'location_id' => $locationId, 'subcategory_id' => $subCatId));
$this->Cat_comp->save($categoryData);
$this->redirect(array('controller' => 'users', 'action' => 'location_categories/' . $locationId));
}
function add_company()
{
$companyName = $this->data['User']['companyName'];
$companyDetails = $this->data['User']['companyDetails'];
$companyUrl = $this->data['User']['companyPhoto'];
$locationAdress = $this->data['User']['companyAdress'];
$locationPostcode = $this->data['User']['companyPostcode'];
$locationPlaats = $this->data['User']['companyCity'];
$contactName = $this->data['User']['contactName'];
$contactPhone = $this->data['User']['contactPhone'];
$contactEmail = $this->data['User']['contactEmail'];
$contactTime = $this->data['User']['contactTime'];
$companyWebsite = $this->data['User']['companyWebsite'];
$subCategory = $this->data['User']['subCategory'];
$getCords = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='. urlencode($locationAdress));
$cordResult = json_decode($getCords);
$latitude = $cordResult->results[0]->geometry->location->lat;
$longitude = $cordResult->results[0]->geometry->location->lng;
$this->loadModel('Company');
$this->Company->create();
$this->Company->saveField('name', $companyName);
$this->Company->saveField('details', $companyDetails);
$this->Company->saveField('afbeelding', $companyUrl);
$this->Company->saveField('url', $companyWebsite);
$condition = array('Company.name' => $companyName);
$companyId = $this->Company->find('first', array('conditions' => $condition));
if(!empty($companyId))
{
$this->loadModel('Location');
$locationData = array('Location'=>array('adress'=>$locationAdress, 'postcode'=>$locationPostcode, 'plaats'=>$locationPlaats, 'longitude'=>$longitude, 'latitude'=>$latitude, 'company_id'=>$companyId['Company']['id']));
$this->Location->save($locationData);
$conditionContact = array('Location.adress' => $locationAdress);
$locationId = $this->Location->find('first', array('conditions' => $conditionContact));
if(!empty($locationId))
{
$this->loadModel('Contact');
$contactData = array('Contact' => array('name' => $contactName, 'phone' => $contactPhone, 'time' => $contactTime, 'email' => $contactEmail, 'location_id' => $locationId['Location']['id']));
$this->Contact->save($contactData);
$this->loadModel('Subcategory');
$conditionCategory = array('Subcategory.id' => $subCategory);
$categoryId = $this->Subcategory->find('first', array('conditions' => $conditionCategory));
if(!empty($categoryId))
{
$this->loadModel('Cat_comp');
$categoryData = array('Cat_comp' => array('company_id' => $companyId['Company']['id'], 'location_id' => $locationId['Location']['id'], 'subcategory_id' => $categoryId['Subcategory']['id']));
$this->Cat_comp->save($categoryData);
//print_r($this->Cat_comp);
$this->redirect(array('controller' => 'users', 'action' => 'index'));
}
}
}
}
function delete_category()
{
$this->loadModel('Category');
$this->Category->delete($this->data['User']['categoryId']);
$this->redirect(array('controller' => 'users', 'action' => 'categories'));
}
function delete_subcategory()
{
$this->loadModel('Subcategory');
$this->Subcategory->delete($this->data['User']['subcategoryId']);
$this->redirect(array('controller' => 'users', 'action' => 'subcategories/' . $this->data['User']['categoryId']));
}
function delete_company($id)
{
$this->loadModel('Company');
$this->Company->delete($id);
$this->redirect(array('controller' => 'companies', 'action' => 'index'));
}
function delete_contact()
{
$this->loadModel('Contact');
$contactId = $this->data['User']['contactId'];
$locationId = $this->data['User']['locationId'];
$this->Contact->delete($contactId);
$this->redirect(array('controller' => 'users', 'action' => 'contacts/' . $locationId));
}
function delete_location()
{
$this->loadModel('Location');
$locationId = $this->data['User']['locationId'];
$companyId = $this->data['User']['companyId'];
$this->Location->delete($locationId);
$this->redirect(array('controller' => 'users', 'action' => 'locations/' . $companyId));
}
function delete_media()
{
$this->loadModel('Media');
$mediaId = $this->data['User']['mediaId'];
$locationId = $this->data['User']['locationId'];
$this->Media->delete($mediaId);
$this->redirect(array('controller' => 'users', 'action' => 'medias/' . $locationId));
}
function delete_location_category()
{
$this->loadModel('Cat_comp');
$locationId = $this->data['User']['locationId'];
$subcatId = $this->data['User']['subcategoryId'];
$this->Cat_comp->query('DELETE FROM cat_comps WHERE location_id = ' . $locationId . ' AND subcategory_id = ' . $subcatId);
$this->redirect(array('controller' => 'users', 'action' => 'location_categories/' . $locationId));
}
function edit_company($id)
{
$this->loadModel('Company');
//Get Company ID
$conditionId = array('Company.id' => $id);
//Get company details
$companyDetails = $this->Company->find('first', array('conditions' => $conditionId));
//Transfer company details to view
$this->set('companyDetail', $companyDetails);
}
function edit_location($id)
{
$this->loadModel('Location');
$conditionId = array('Location.id' => $id);
$locationDetails = $this->Location->find('first', array('conditions' => $conditionId));
$this->loadModel('Category');
$categoryDetails = $this->Category->find('list');
$this->set('categoryDetails', $categoryDetails);
$this->set('locationDetail', $locationDetails);
}
function edit_contact($id)
{
$this->loadModel('Contact');
$conditionId = array('Contact.id' => $id);
$contactDetails = $this->Contact->find('first', array('conditions' => $conditionId));
$this->set('contactDetail', $contactDetails);
}
function edit_media($id)
{
$this->loadModel('Media');
$conditionId = array('Media.id' => $id);
$mediaDetails = $this->Media->find('first', array('conditions' => $conditionId));
$this->set('mediaDetail', $mediaDetails);
}
function location_categories($locationId)
{
$this->loadModel('Cat_comps');
$this->loadModel('Subcategory');
$catCompDetails = $this->Subcategory->query('SELECT * FROM subcategories INNER JOIN cat_comps ON subcategories.id = cat_comps.subcategory_id WHERE location_id = ' . $locationId);
$this->set('categoryDetails', $catCompDetails);
$this->set('locationId', $locationId);
}
function medias($id)
{
$this->loadModel('Media');
$condition = array('Media.location_id' => $id);
$mediaDetails = $this->Media->find('all', array('conditions' => $condition));
$this->loadModel('Location');
$conditionLocId = array('Location.id' => $id);
$companyId = $this->Location->find('first', array('conditions' => $conditionLocId));
$this->set('mediaDetails', $mediaDetails);
$this->set('compayDetails', $companyId);
$this->set('locationId', $id);
}
function categories()
{
$this->loadModel('Category');
$categoryDetails = $this->Category->find('all');
$this->set('categoryDetails', $categoryDetails);
}
function subcategories($categoryId)
{
$this->loadModel('Subcategory');
$subCategories = $this->Subcategory->findAllByCategoryId($categoryId);
$this->set('subcategories', $subCategories);
$this->set('categoryId', $categoryId);
}
function locations($companyId)
{
$this->loadModel('Location');
//Get Location details
$locationDetails = $this->Location->findAllByCompanyId($companyId);
$this->set('locationDetail', $locationDetails);
$this->set('companyId', $companyId);
}
function contacts($locationId)
{
$this->loadModel('Contact');
$this->loadModel('Location');
$contactDetails = $this->Contact->findAllByLocationId($locationId);
$companyIdCondition = array("Location.id" => $locationId);
$getCompanyId = $this->Location->find('first', array('conditions' => $companyIdCondition));
$this->set('contactDetail', $contactDetails);
$this->set('companyId', $getCompanyId);
$this->set('locationId', $locationId);
}
function save_password()
{
$oldPassword = $this->data['User']['oldPassword'];
$newPassword = $this->data['User']['newPassword'];
$condition = array ('User.password' => $oldPassword);
$passConfirm = $this->User->find('count', array('conditions' => $condition));
if($passConfirm == 1)
{
$this->User->read(null, $this->Session->read('User.id'));
$this->User->set('password', $newPassword);
$this->User->save();
$this->Session->write(array('Pass.confirm' => 'True'));
$this->Session->write(array('Pass.error' => 'False'));
$this->redirect(array('controller' => 'users', 'action' => 'index'));
}
else
{
$this->Session->write(array('Pass.error' => 'True'));
$this->Session->write(array('Pass.confirm' => 'False'));
$this->redirect(array('controller' => 'users', 'action' => 'edit_user'));
}
}
function save_company()
{
$this->loadModel('Company');
$newCompanyName = $this->data['User']['companyName'];
$newCompanyDetails = $this->data['User']['companyDetails'];
$newCompanyUrl = $this->data['User']['companyUrl'];
$companyId = $this->data['User']['companyId'];
$companyWebsite = $this->data['User']['companyWebsite'];
$this->Company->read(null, $companyId);
$this->Company->set('name', $newCompanyName);
$this->Company->set('details', strip_tags($newCompanyDetails));
$this->Company->set('afbeelding', $newCompanyUrl);
$this->Company->set('url', $companyWebsite);
$this->Company->save();
$this->redirect(array('controller' => 'users', 'action' => 'edit_company/' . $companyId));
}
function save_location()
{
$this->loadModel('Location');
$newLocationAdres = $this->data['User']['companyAdress'];
$newLocationPostcode = $this->data['User']['companyPostcode'];
$newLocationCity = $this->data['User']['companyCity'];
$locationId = $this->data['User']['locationId'];
$companyId = $this->data['User']['companyId'];
$newLocationCategoryId = $this->data['User']['subCategory'];
$getCords = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='. urlencode($newLocationAdres));
$cordResult = json_decode($getCords);
$latitude = $cordResult->results[0]->geometry->location->lat;
$longitude = $cordResult->results[0]->geometry->location->lng;
$this->Location->read(null, $locationId);
$this->Location->set('adress', $newLocationAdres);
$this->Location->set('postcode', $newLocationPostcode);
$this->Location->set('plaats', $newLocationCity);
$this->Location->set('longitude', $longitude);
$this->Location->set('latitude', $latitude);
$this->Location->save();
}
function save_contact()
{
$this->loadModel('Contact');
$newContactName = $this->data['User']['contactName'];
$newContactPhone = $this->data['User']['contactPhone'];
$newContactTime = $this->data['User']['contactTime'];
$newContactEmail = $this->data['User']['contactEmail'];
$contactId = $this->data['User']['contactId'];
$this->Contact->read(null, $contactId);
$this->Contact->set('name', $newContactName);
$this->Contact->set('phone', $newContactPhone);
$this->Contact->set('time', $newContactTime);
$this->Contact->set('email', $newContactEmail);
$this->Contact->save();
$this->redirect(array('controller' => 'users', 'action' => 'contacts/' . $contactId));
}
function save_media()
{
$this->loadModel('Media');
$newMediaName = $this->data['User']['mediaName'];
$newMediaUrl = $this->data['User']['mediaUrl'];
$newMediaDetail = $this->data['User']['mediaDetail'];
$mediaId = $this->data['User']['mediaId'];
$locationId = $this->data['User']['locationId'];
$this->Media->read(null, $mediaId);
$this->Media->set('name', $newMediaName);
$this->Media->set('url', $newMediaUrl);
$this->Media->set('details', $newMediaDetail);
$this->Media->save();
$this->redirect(array('controller' => 'users', 'action' => 'medias/' . $locationId));
}
function login()
{
//Show error when user does not fill all fields
if($this->Session->read('Error.blank') == 'True')
{
$this->set("msg", "U dient wel alle velden in te vullen.");
}
//Show error if the email doesnt match the password
if($this->Session->read('Error.wrong') == 'True')
{
$this->set("msg", "Combinatie van e-mail en wachtwoord is onbekend.");
}
}
function logout()
{
//Destroy all sessions
$this->Session->delete('User'); // (TIP:) Dont use Destroy. That will destroy all the sessions. Just remove the user-session ;)
//Redirect user to login page
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
function authenticate()
{
//Save POST date into variables for checkup
$userNameIn = $this->data['User']['username'];
$passwordIn = $this->data['User']['password'];
if($userNameIn == '' || $passwordIn == '') //Check for blank fields
{
$this->Session->write(array('Error.blank' => 'True'));
$this->Session->write(array("Error.wrong" => "False"));
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
else
{
$this->Session->write(array("Error.blank" => "False"));
//Save the query into a variable for further use
$condition = array('User.email' => $userNameIn, "AND" => array('User.password' => $this->data['User']['password']));
//Results based on query: if user is found, teh result will be 1; if the user is not found, result is 0;
$userFound = $this->User->find('count', array('conditions' => $condition));
//Check if user is found, if yes: the user will be redirected to the index page
if($userFound == 1)
{
$this->Session->write(array("Error.wrong" => "False"));
//Save information
$this->Session->write('User.username', $this->data['User']['username']);
$userID = $this->User->find('first', array('conditions' => $condition));
$this->Session->write('User.id', $userID['User']['id']);
if($userID['User']["admin"] == 1){
$this->Session->write('User.admin', "");
}
// redirect
$this->redirect(array('controller' => 'users', 'action' => 'index'));
}
else // If not, the user will be redirected to the login page with an error
{
$this->Session->write(array("Error.wrong" => "True"));
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment