コード例 #1
0
ファイル: Create.php プロジェクト: NareshChennuri/pyng
 public function __construct($data)
 {
     $db = JO_Db::getDefaultAdapter();
     try {
         $db->beginTransaction();
         $date_added = WM_Date::format(time(), 'yy-mm-dd H:i:s');
         $data['date_added'] = $date_added;
         $data['last_login'] = $date_added;
         $data['status'] = 1;
         $data['last_action_datetime'] = $date_added;
         $data['ip_address'] = JO_Request_Server::encode_ip(JO_Request::getInstance()->getClientIp());
         $data['new_email'] = $data['email'];
         $data['store'] = JO_Registry::get('default_upload_method');
         if (!$data['store']) {
             $data['store'] = 'Model_Upload_Locale';
         }
         /*$avatar = '';
         		if(isset($data['avatar']) && $data['avatar']) {
         			$avatar = $data['avatar'];
         			$data['avatar'] = '';
         		}*/
         $rows = Helper_Db::describeTable('users');
         $insert = array();
         $avatar = '';
         foreach ($rows as $row => $def) {
             if (isset($data[$row])) {
                 if (in_array($row, array('password', 'new_password'))) {
                     if ($data[$row]) {
                         if ($data[$row] instanceof JO_Db_Expr) {
                             $insert[$row] = $data[$row];
                         } else {
                             $insert[$row] = md5($data[$row]);
                         }
                     } else {
                         $insert[$row] = '';
                     }
                 } elseif ($row == 'avatar') {
                     $avatar = $data[$row];
                     $data[$row] = '';
                 } else {
                     $insert[$row] = $data[$row];
                 }
             } else {
                 $insert[$row] = $def;
             }
         }
         //create user
         $user_id = Helper_Db::create('users', $insert);
         if (!$user_id) {
             return $this;
         }
         //upload avatar
         if ($avatar) {
             $method_for_upload = Helper_Config::get('file_upload_method');
             if ($method_for_upload) {
                 $image = call_user_func(array($method_for_upload, 'uploadUserAvatar'), $avatar, $user_id);
                 $error = call_user_func(array($method_for_upload, 'getError'));
                 if ($error) {
                     $this->error[] = $error;
                 }
                 if ($image && isset($image['image']) && $image['image']) {
                     Helper_Db::update('users', array('avatar' => $image['image'], 'store' => $image['store'], 'height' => $image['height'], 'width' => $image['width']), array('user_id = ?' => (string) $user_id));
                 }
             }
         }
         //create user alias
         new Model_Users_Autoseo($user_id);
         /*Helper_Db::insert('url_alias', array(
         				'query' => 'user_id=' . (string)$user_id,
         				'keyword' => $data['username'],
         				'path' => $data['username'],
         				'route' => 'users/profile'
         		));*/
         //add default boards
         if (is_array(Helper_Config::get('default_boards'))) {
             foreach (Helper_Config::get('default_boards') as $def) {
                 new Model_Boards_Create(array('category_id' => $def['category_id'], 'title' => $def['title'], 'user_id' => (string) $user_id));
             }
         }
         //set following
         $config_private_boards = Helper_Config::get('config_private_boards');
         if (isset($data['following_user']) && $data['following_user'] && $data['following_user'] != -1) {
             Helper_Db::insert('users_following_user', array('user_id' => (string) $user_id, 'following_id' => (string) $data['following_user']));
             Helper_Db::insert('users_following_user', array('user_id' => (string) $data['following_user'], 'following_id' => (string) $user_id));
             //update following user info
             Helper_Db::update('users', array('pins' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND public = 1' : '') . ')'), 'boards' => new JO_Db_Expr('(SELECT COUNT(DISTINCT board_id) FROM boards WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND public = 1' : '') . ')'), 'likes' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins_likes WHERE user_id = users.user_id)'), 'following' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT following_id) FROM users_following_user WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT user_id) FROM users_following WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) )'), 'followers' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT user_id) FROM users_following_user WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT user_id) FROM users_following WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) )')), array('user_id = ?' => (string) $data['following_user']));
         }
         //update user info
         Helper_Db::update('users', array('pins' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND public = 1' : '') . ')'), 'boards' => new JO_Db_Expr('(SELECT COUNT(DISTINCT board_id) FROM boards WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND public = 1' : '') . ')'), 'likes' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins_likes WHERE user_id = users.user_id)'), 'following' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT following_id) FROM users_following_user WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT following_id) FROM users_following WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) )'), 'followers' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT user_id) FROM users_following_user WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT user_id) FROM users_following WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) )')), array('user_id = ?' => (string) $user_id));
         $this->user_id = $user_id;
         $db->commit();
     } catch (JO_Exception $e) {
         $this->error[] = $e->getMessage();
         $db->rollBack();
     }
 }
コード例 #2
0
ファイル: Edit.php プロジェクト: NareshChennuri/pyng
 public function __construct($user_id, $data = array())
 {
     $db = JO_Db::getDefaultAdapter();
     try {
         $db->beginTransaction();
         $user_info_get = new Model_Users_User($user_id);
         if (!$user_info_get) {
             return $this;
         }
         $rows = Helper_Db::describeTable('users');
         $update = array();
         $avatar = '';
         foreach ($rows as $row => $def) {
             if (isset($data[$row])) {
                 if (in_array($row, array('password', 'new_password'))) {
                     if ($data[$row]) {
                         if ($data[$row] instanceof JO_Db_Expr) {
                             $update[$row] = $data[$row];
                         } else {
                             $update[$row] = md5($data[$row]);
                         }
                     } else {
                         $update[$row] = '';
                     }
                 } elseif ($row == 'avatar') {
                     $avatar = $data[$row];
                     unset($data[$row]);
                 } else {
                     $update[$row] = $data[$row];
                 }
             }
         }
         if (count($update) == 0 && $avatar == '') {
             return $this;
         }
         $result = Helper_Db::update('users', $update, array('user_id = ?' => (string) $user_id));
         //add user avatar
         if ($avatar) {
             $image = false;
             $method_for_upload = Helper_Config::get('file_upload_method');
             if ($method_for_upload) {
                 $image = call_user_func(array($method_for_upload, 'uploadUserAvatar'), $avatar, $user_id);
             }
             $error = call_user_func(array($method_for_upload, 'getError'));
             if (!$error && $image && isset($image['image']) && $image['image']) {
                 $res = Helper_Db::update('users', array('avatar' => $image['image'], 'store' => $image['store'], 'height' => $image['height'], 'width' => $image['width'], 'last_action_datetime' => new JO_Db_Expr('NOW()')), array('user_id = ?' => (string) $user_id));
                 if (!$result) {
                     $result = $res;
                 }
                 Helper_Db::delete('users_avatars', array('user_id = ?' => (string) $user_id));
                 if ($user_info_get && $user_info_get['avatar']) {
                     if ($user_info_get['avatar'] != $image['image']) {
                         call_user_func(array($user_info_get['store'], 'deleteUserImage'), $user_info_get);
                     }
                 }
             }
         }
         if (isset($data['username'])) {
             $result = new Model_Users_Autoseo($user_id);
             if (!$result->affected_rows) {
                 $result = $res;
             }
         }
         $config_private_boards = Helper_Config::get('config_private_boards');
         $res = Helper_Db::update('users', array('pins' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND (public = 1 OR user_id = users.user_id)' : '') . ')'), 'boards' => new JO_Db_Expr('(SELECT COUNT(DISTINCT board_id) FROM boards WHERE user_id = users.user_id ' . ($config_private_boards ? ' AND (public = 1 OR user_id = users.user_id)' : '') . ')'), 'likes' => new JO_Db_Expr('(SELECT COUNT(DISTINCT pin_id) FROM pins_likes WHERE user_id = users.user_id)'), 'following' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT following_id) FROM users_following_user WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT following_id) FROM users_following WHERE user_id = users.user_id AND following_id != users.user_id LIMIT 1) )'), 'followers' => new JO_Db_Expr('( (SELECT COUNT(DISTINCT user_id) FROM users_following_user WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) + (SELECT COUNT(DISTINCT user_id) FROM users_following WHERE following_id = users.user_id AND user_id != users.user_id LIMIT 1) )'), 'latest_pins' => new JO_Db_Expr('( SUBSTRING_INDEX( (SELECT GROUP_CONCAT(pin_id ORDER BY `pin_id` DESC) FROM `pins` WHERE user_id = users.user_id), \',\', 15 ) )')), array('user_id = ?' => (string) $user_id));
         if (!$result) {
             $result = $res;
         }
         $this->affected_rows = $result;
         $db->commit();
     } catch (JO_Exception $e) {
         $db->rollBack();
     }
 }