Created
September 15, 2014 15:28
-
-
Save veganista/0d400042ab5e35dda082 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('MediaAppModel', 'Media.Model'); | |
App::uses('Folder', 'Utility'); | |
App::uses('File', 'Utility'); | |
/** | |
* Attachment Model | |
* | |
*/ | |
class Attachment extends MediaAppModel { | |
/** | |
* Display field | |
* | |
* @var string | |
*/ | |
public $displayField = 'name'; | |
/** | |
* Behaviors | |
* | |
* @var array | |
*/ | |
public $actsAs = array( | |
'Media.Uploadable' => array( 'filename' ) | |
); | |
/** | |
* Validation rules | |
* | |
* @var array | |
*/ | |
public $validate = array( | |
'name' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'message' => 'Your custom message here', | |
//'allowEmpty' => false, | |
//'required' => false, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
'filename' => array( | |
'uploadError' => array( | |
'rule' => array('uploadError') | |
), | |
), | |
'mimetype' => array( | |
'notEmpty' => array( | |
'rule' => array('notEmpty'), | |
//'message' => 'Your custom message here', | |
//'allowEmpty' => false, | |
//'required' => false, | |
//'last' => false, // Stop validation after this rule | |
//'on' => 'create', // Limit validation to 'create' or 'update' operations | |
), | |
), | |
); | |
public function afterFind( $results, $primary = false ) { | |
die('afterFind'); | |
foreach ( $results as $index => $result ) { | |
if( !empty( $result[$this->alias] ) ) { | |
$path = Folder::slashTerm(Configure::read('Media.uploadDir')) . $result[$this->alias]['directory'] . $result[$this->alias]['filename']; | |
$url = MediaThumbnail::getUrl($path); | |
$results[$index] = Hash::merge($results[$index], array($this->alias => array( | |
'path' => $path, | |
'url' => $url, | |
))); | |
} | |
} | |
return $results; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* form method | |
* | |
* Responsible for rendering the form template for add and edit views. | |
* | |
* @return void | |
*/ | |
private function form() { | |
// create some empty answers if there are none | |
if( empty($this->request->data['Answer']) ){ | |
$this->request->data['Answer'] = array_fill(0, 3, array( | |
'Answer' => array('id' => '', 'required' => '', 'content' => ''), | |
'AnswerOption' => array ( | |
array('id' => '', 'content' => '', 'attachment_id' => ''), | |
) | |
)); | |
} | |
// ne need to get the attachments for all the AnswerOptions | |
$this->request->data['Answer'] = Hash::map($this->request->data['Answer'], '{n}', function( $answer ){ | |
$answer['AnswerOption'] = Hash::map($answer['AnswerOption'], '{n}', function( $answerOption ){ | |
if( empty($answerOption['Attachment']) && !empty($answerOption['attachment_id']) ){ | |
$attachment = $this->Question->Answer->AnswerOption->Attachment->find('first', array( | |
'conditions' => array('Attachment.id' => $answerOption['attachment_id']) | |
)); | |
$answerOption = array_merge($answerOption, $attachment); | |
} | |
return $answerOption; | |
}); | |
return $answer; | |
}); | |
echo '<pre>'; | |
print_r($this->request->data); | |
die(); | |
// reset the answer array keys to prevent clashes when adding/removing | |
$this->request->data['Answer'] = array_values($this->request->data['Answer']); | |
// create some empty options | |
/*$this->request->data['Answer'] = Hash::map($this->request->data['Answer'], "{n}", function( $item ){ | |
$item['AnswerOption'] = array_values($item['AnswerOption']); | |
return $item; | |
});*/ | |
$this->render('_form'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment