Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save stevemeisner/376599 to your computer and use it in GitHub Desktop.

Select an option

Save stevemeisner/376599 to your computer and use it in GitHub Desktop.
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $actsAs = array('Containable');
var $hasMany = array(
'Answer' => array(
'className' => 'Answer',
'foreignKey' => 'profile_id',
'dependent' => true,
'exclusive' => true
)
);
var $hasAndBelongsToMany = array(
'Media' => array(
'className' => 'Media',
'joinTable' => 'media_profiles',
'foreignKey' => 'profile_id',
'associationForeignKey' => 'media_id',
'unique' => true,
)
);
function beforeSave() {
if(isset($this->data['Profile']['id'])) {
// Delete all associated Answers so the resave is clean.
$this->delete($this->data['Profile']['id'], true);
}
// Process the media.
if(!empty($this->data['Media'])) {
$results = array();
foreach ($this->data['Media'] as &$item) {
$this->Media->id = null;
$this->Media->save($item);
$results[] = $this->Media->id;
}
$this->data['Media'] = $results;
}
return true;
}
function getAllProfiles($page) {
$params = array(
'conditions' => array('Profile.visible' => true),
'fields' => array(
'Profile.id',
'Profile.first_name',
'Profile.last_name',
'Profile.intro',
'Profile.first_year',
'Profile.college_profession',
'Profile.last_updated',
),
'contain' => array(
'Media' => array(
'order' => array('Media.order ASC')
)
),
'page' => $page,
'limit' => 20,
'order' => array('Profile.last_updated DESC'),
);
return $this->find('all', $params);
}
function getByFirstLetter($letter) {
$params = array(
'conditions' => array('Profile.visible' => true, 'Profile.last_name LIKE' => $letter.'%'),
'fields' => array(
'Profile.id',
'Profile.first_name',
'Profile.last_name',
'Profile.last_updated',
),
'contain' => array(),
'order' => array('Profile.last_name DESC'),
);
return $this->find('all', $params);
}
function getAllCompleteProfiles() {
$params = array(
'recursive' => 1,
'order' => array('Profile.last_updated DESC'),
'contain' => array(
'Media' => array(
'order' => array('Media.order ASC')
),
'Answer' => array('Question')
)
);
return $this->find('all', $params);
}
function getProfile($id) {
$params = array(
'conditions' => array('Profile.id' => $id, 'Profile.visible' => true),
'recursive' => 2,
'fields' => array(
'Profile.id',
'Profile.first_name',
'Profile.last_name',
'Profile.college_profession',
'Profile.intro',
'Profile.bio',
'Profile.scripture_quote',
'Profile.first_year',
'Profile.years_camper',
'Profile.years_staff',
'Profile.positions',
),
'contain' => array(
'Media' => array(
'order' => array('Media.order ASC')
),
'Answer' => array(
'Question'
)
),
'limit' => 1
);
return $this->find('first', $params);
}
function getFooterProfiles() {
$params = array(
'conditions' => array('Profile.visible' => true),
'fields' => array(
'Profile.id',
'Profile.college_profession',
'Profile.first_year',
'Profile.first_name',
'Profile.last_name'
),
'limit' => 4,
'contain' => array('Media'),
'order' => array('Profile.last_updated DESC')
);
return $this->find('all', $params);
}
function getNumPages() {
$params = array(
'recursive' => -1,
'conditions' => array('Profile.visible' => true)
);
return ceil( $this->find('count', $params) / 20 );
}
function getUniverseInfo($id) {
$params = array(
'conditions' => array('Profile.id' => $id),
'fields' => array(
'Profile.id',
'Profile.first_name',
'Profile.last_name',
'Profile.college_profession',
'Profile.first_year',
'Profile.years_camper',
'Profile.years_staff',
'Profile.last_updated',
'Profile.intro'
),
'contain' => array('Media')
);
return $this->find('first', $params);
}
function getProfileIDs($sortBy, $omitPhotoless) {
if($omitPhotoless) {
$this->bindModel(array('hasOne' => array('MediaProfile')));
$params = array(
'fields' => array('Profile.id'),
'conditions' => array(
'NOT' => array('MediaProfile.media_id' => null),
'contain' => '',
'order' => array('Profile.'.$sortBy.' DESC')
)
);
$results = $this->find('all', $params);
} else {
$params = array(
'conditions' => $conditions,
'fields' => array(
'Profile.id'),
'contain' => '',
'order' => array('Profile.'.$sortBy.' DESC')
);
$results = $this->find('all', $params);
}
return $results;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment