あと8日でサイトが作れるかどうか
2007/12/11 火曜日 - 1:03:37 by kensuu
けんすうです。
先週最後の最後でさぼってしまいました。
そもそも更新があと残り1回しかないので
引き締めまくっていきたいと思います。
先週はcakePHP勉強会にいってきました。
プログラムの先生をやってくれているあきやんが
スライドを公表しています。
CakePHP勉強会を開催しました! & 発表資料です : akiyan.com
こういうのがすごく勉強になる・・・。
他人のソースコードを見るのは超わかりやすいですね。
さて、100日プログラミング。
これが終わった暁には、成果物を出します。
サイトをオープンさせます。
ということで、コードを晒します。
profiles_controllerというファイルのものです。
class ProfilesController extends AppController { var $name = 'Profiles'; var $uses = array('Post','Profile','User','Poke'); //ページング用の数値 public $view_default_limit = 7; //プロフィールのテンプレを上書き〜 public $layout = 'profile_default'; function index() { //クッキー情報からログインかどうかを判定 $auth = $this->User->findById_hash($_COOKIE['auth']); $this->set('my_profiles', $my_profiles = $this->Profile->findByUserId($auth['User']['id'])); //ページタイトル $this->pageTitle = $my_profiles['Profile']['name'] . "のページ"; } function view($to_user_id = null,$page=1) { if(isset($_COOKIE['auth'])){ $auth = $this->User->findById_hash($_COOKIE['auth']); $cond = array('user_id' => $auth['User']['id']); $this->set('my_profiles', $my_profiles = $this->Profile->findByUserId($auth['User']['id'])); //ついでにセッションのIDもセットしよう $this->set('auth',$auth['User']['id']); } //ユーザーidをセットしておく $this->set('to_user_id', $to_user_id); //ユーザーIDの投稿画面&表示 $cond = array('to_user_id' => $to_user_id); //データの数を数える $count = $this->Post->findCount($cond); $this->set('count', $count); //取り出すレコードの開始位置 $st = 0; //ページング限界数をセット $lim = $this->view_default_limit; $this->set('lim',$lim); $page = !isset($_GET["page"]) || $_GET["page"] <1 ? 1 : intval($_GET["page"]); $st = ($page - 1) * $lim; //次の10件 $next = $page + 1; //前の10件 $prev = $page - 1; $prev = $prev <1 ? 1 : $prev; //ページングのやつを配列にいれてsetしちゃうよ $pages = array( 'page' => $page, 'prev' => $prev, 'next' => $next ); $this->set('pages',$pages); //とりあえず$postsにデータを入れる $posts = $this->Post->findAll($cond, null, 'Post.created DESC', $lim, $page,2); //セット $this->set('posts', $posts); //名前のためにprofilesをsetしないと $this->set('profiles', $profiles = $this->Profile->findByUserId($to_user_id)); //画像がなければデフォルトイメージ if (empty($profiles['Profile']['pic'])) { $profiles['Profile']['pic'] = 'no_image.jpg'; $this->set('profiles', $profiles); } /* 新しいユーザーや盛り上げ一覧で出す(ここに書くのは美しくない?) */ //新規ユーザー //とりあえず$postsにデータを入れる(最後の2はなんだ?調べる) $new_users = $this->Profile->findAll(null, null, 'Profile.user_id DESC', 10,null,2); $this->set('new_users',$new_users); //ページタイトル $this->pageTitle = $profiles['Profile']['name'] . "のページ"; /*ここからpokeのカウント用の処理*/ //GROUP BYするためにSQLは直接書きます //つっついた人はユニークでとりたいため、user_idでGROUP BYします $poke_rets = $this->Poke->query("SELECT Poke.user_id, count(Poke.user_id) as cnt FROM pokes as Poke where " . (int)$to_user_id . " = Poke.to_user_id GROUP BY Poke.user_id ORDER BY max(id) DESC LIMIT 10;"); //とりあえずuser_idを配列に入れます foreach($poke_rets as $k => $poke_ret){ $poke_rets[$k] = am($poke_ret, $this->Poke->findByUserIdAndToUserId($poke_ret['Poke']['user_id'], (int)$to_user_id, null, 'Poke.id DESC')); } $this->set('poke_users', $poke_rets); /*ここから書き込みをした人の処理*/ //GROUP BYするためにSQLは直接書きます //つっついた人はユニークでとりたいため、user_idでGROUP BYします $post_rets = $this->Post->query("SELECT Post.to_user_id, Post.user_id FROM posts as Post where " . (int)$to_user_id . " = Post.user_id GROUP BY Post.to_user_id ORDER BY max(id) DESC LIMIT 10;"); //とりあえずuser_idを配列に入れます foreach($post_rets as $kk => $post_ret){ $post_rets[$kk] = am($post_ret, $this->Profile->findById($post_ret['Post']['to_user_id'])); } //pr($post_rets); $this->set('post_users', $post_rets); } function edit($my_id = null) { //クッキー情報からログインかどうかを判定 $auth = $this->User->findById_hash($_COOKIE['auth']); //セッションのIDを$my_idへ $my_id = ($auth['User']['id']); //profileにセット $this->set('my_profiles', $my_profiles = $this->Profile->findByUserId($my_id)); //pr($my_profiles); //echo $my_profiles['Profile']['pic']; //画像アップ if (!empty($this->data['Profile']['pic']['name'])){ //大文字を小文字にしたりして拡張子をぶっこぬき(正規表現はあとで調べる) $ext = strtolower(preg_replace("!.*\.!", null, $this->data['Profile']['pic']['name'])); //書式指定変換した出力を文字列に格納 $filename = sprintf("%05d.%s",$my_id, $ext); pr($filename); move_uploaded_file($this->data['Profile']['pic']['tmp_name'], APP."webroot/img/user_pics".DS.$filename); $this->data['Profile']['pic'] = $filename; }else{ //picがなかったらpicそのもの自体を消しちゃうよ! unset($this->data['Profile']['pic']); } pr($this->data); //もしpostの中がからっぽだったらviewに渡す? if(empty($this->data)) { $this->Profile->id = $my_profiles['User']['id']; $this->data = $this->Profile->read(); } else{ if ($this->Profile->save($this->data['Profile'])){ $this->flash('プロフィールを変更しました!','/'); } } /* 新しいユーザーや盛り上げ一覧で出す(ここに書くのは美しくない?) */ //新規ユーザー //とりあえず$postsにデータを入れる $new_users = $this->Profile->findAll(null, null, 'Profile.user_id DESC', 10,null,2); $this->set('new_users',$new_users); //名前のためにprofilesをsetしないと $this->set('profiles', $profiles = $this->Profile->findByUserId($my_id)); //画像がなければデフォルトイメージ if (empty($profiles['Profile']['pic'])) { $profiles['Profile']['pic'] = 'no_image.jpg'; $this->set('profiles', $profiles); } //ページタイトル $this->pageTitle = $my_profiles['Profile']['name'] . "のページ"; /*ここからpokeのカウント用の処理*/ $cond = array('Poke.to_user_id' => $my_id); $order = array('Poke.created' => 'desc');//並び順 $this->set('pokes', $pokes = $this->Poke->findAll($cond,null,$order,10)); } }
もはやどう解説したらいいのかわからないので解説しません。
他人のコードを見たり、
人から教わりまくったりと、かなりあたふたしながら作っています。
こんなスライドが人気のようです。
すごくいいことを言っています。
そう、コードを晒すのは大事なのです。
しかし晒し方もなかなか難しい。
もうちょっと晒せるようになりたいのですが
いまだそのレベルですらない。
晒し方がうまくなるようにしないとなあ、と。
サイトを一つ作るというのができたら、
今度は細々としたコードを晒していけるようにしようと思っています。
というわけで、あと8日がんばります!