public function set_primary_photoAction($id = null) { $this->view->disable(); $photos = MemberPhotos::findFirst($id); $userSession = $this->session->get('userSession'); if (!$photos || $userSession['id'] != $photos->member_id) { return $this->response->redirect('biz/add_photo/' . $userSession['id']); } else { $currentPhotos = MemberPhotos::find('member_id = "' . $userSession['id'] . '"'); foreach ($currentPhotos as $key => $currentPhoto) { $currentPhoto->primary_pic = 'No'; if (!$currentPhoto->update()) { $this->view->disable(); echo "failed to update current photo."; } } $photos->modified = date('Y-m-d H:i:s'); $photos->primary_pic = 'Yes'; if ($photos->update()) { $userSession['primary_pic'] = $photos->file_path . $photos->filename; $this->session->set('userSession', $userSession); $this->flash->success('<button type="button" class="close" data-dismiss="alert">×</button>Photo has been set as primary.'); return $this->response->redirect('biz/add_photo/' . $userSession['id']); } else { $this->view->disable(); echo "failed to modified data. "; } } }
public function add_photoAction($id = null) { $userSession = $this->session->get('userSession'); $id = $userSession['id']; $member = Members::findFirst($id); if (!$member) { $this->response->redirect('member/add_photo/' . $userSession['id']); } $this->view->setVar('member', $member); // $photos = MemberPhotos::find(array('columns' => '*', // 'conditions' => 'member_id LIKE :member_id: AND primary_pic LIKE :primary_pic: ', // 'order' => 'id DESC', // 'bind' => array('member_id' => $id, 'primary_pic' => 'yes' ) // )); $photos = MemberPhotos::find(array('member_id = "' . $id . '"', 'order' => 'id DESC')); $this->view->setVar('photos', $photos); if ($this->request->isPost() && $this->request->hasFiles() == true) { //ini_set('upload_max_filesize', '64M'); set_time_limit(1200); $uploads = $this->request->getUploadedFiles(); $isUploaded = false; #do a loop to handle each file individually foreach ($uploads as $upload) { #define a “unique” name and a path to where our file must go $fileName = $upload->getname(); $fileInfo = new SplFileInfo($fileName); $fileExt = $fileInfo->getExtension(); $fileExt = strtolower($fileExt); $newFileName = substr(md5(uniqid(rand(), true)), 0, 10) . date('ymdhis') . '.' . $fileExt; //$fileExt = $upload->getExtension(); $fileImageExt = array('jpeg', 'jpg', 'png'); //error_log("File Extension :".$fileExt, 0); $fileType = ''; $filePath = ''; $path = ''; //$path = ''.$newFileName; if (in_array($fileExt, $fileImageExt)) { $path = 'img/member/' . $newFileName; $filePath = 'img/member/'; //$fileType = 'Image'; } #move the file and simultaneously check if everything was ok $upload->moveTo($path) ? $isUploaded = true : ($isUploaded = false); } #if any file couldn't be moved, then throw an message if ($isUploaded) { $memberPhotos = new MemberPhotos(); $memberPhotos->created = date('Y-m-d H:i:s'); $memberPhotos->modified = date('Y-m-d H:i:s'); $memberPhotos->member_id = $userSession['id']; $memberPhotos->file_path = $filePath; $memberPhotos->filename = $newFileName; $memberPhotos->caption = $this->request->getPost('caption'); if (count($photos) > 0) { $memberPhotos->primary_pic = 'No'; } else { $memberPhotos->primary_pic = 'Yes'; $userSession['primary_pic'] = $filePath . $newFileName; $this->session->set('userSession', $userSession); } if ($memberPhotos->create()) { return $this->response->redirect('member/add_photo/' . $id); } } } }