1. Trang chủ
  2. » Công Nghệ Thông Tin

Tạo mạng xã hội với PHP - part 27 ppt

10 104 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 3,62 MB

Nội dung

Statuses—Other Media [ 242 ] else { // 'invalid extention'; return false; } } } else { // 'not uploaded file'; return false; } } The following getter method is used to return the name of the image we are working with: /** * Get the image name * @return String */ public function getName() { return $this->name; } Finally, we have our save method, which again must detect the type of image, to work out which function to use. /** * Save changes to an image e.g. after resize * @param String $location location of image * @param String $type type of the image * @param int $quality image quality /100 * @return void */ public function save( $location, $type='', $quality=100 ) { $type = ( $type == '' ) ? $this->type : $type; if( $type == IMAGETYPE_JPEG ) { imagejpeg( $this->image, $location, $quality); } elseif( $type == IMAGETYPE_GIF ) { This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 8 [ 243 ] imagegif( $this->image, $location ); } elseif( $type == IMAGETYPE_PNG ) { imagepng( $this->image, $location ); } } } ?> Using the image manager library to process the le upload Now that we have a simple, centralized way of processing le uploads and resizing them, we can process the image the user is trying to upload as their extended status. /** * Process an image upload and set the image * @param String $postfield the $_POST field the image was uploaded through * @return boolean */ public function processImage( $postfield ) { require_once( FRAMEWORK_PATH . 'lib/images/imagemanager.class.php' ); $im = new Imagemanager(); $prefix = time() . '_'; if( $im->loadFromPost( $postfield, $this->registry- >getSetting('upload_path') . 'statusimages/', $prefix ) ) { $im->resizeScaleWidth( 150 ); $im->save( $this->registry->getSetting('upload_path') . 'statusimages/' . $im->getName() ); $this->image = $im->getName(); return true; } else { return false; } } This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Statuses—Other Media [ 244 ] Saving the status This leaves us with the nal method for saving the status. This calls the parent object's save method to create the record in the statuses table. Then it gets the ID, and inserts a new record into the images table with this ID as the ID. /** * Save the image status * @return void */ public function save() { // save the parent object and thus the status table parent::save(); // grab the newly inserted status ID $id = $this->getID(); // insert into the images status table, using the same ID $extended = array(); $extended['id'] = $id; $extended['image'] = $this->image; $this->registry->getObject('db')->insertRecords( 'statuses_images', $extended ); } } ?> Video (via YouTube) To support video (via YouTube), we need one additional eld on the form for the user to paste in the YouTube URL. From the URL, we can automatically generate code to play the video, and we can also look up the thumbnail image of the video from YouTube, from the data contained within the URL. Database As with our image's status type, we only require two elds in our new table: Field Type Description ID Integer, Primary key To relate to the main statuses table Video_id Varchar The YouTube video ID This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 8 [ 245 ] Model The model needs to be very similar to our image's model. Firstly, the class extends the status class. Then, we have our variable for the video ID, after which we construct the object by calling the parent object's setTypeReference and __construct methods. <?php /** * Video status object * extends the base status object */ class Videostatus extends status { private $video_id; /** * Constructor * @param Registry $registry * @param int $id * @return void */ public function __construct( Registry $registry, $id = 0 ) { $this->registry = $registry; parent::__construct( $this->registry, $id ); parent::setTypeReference('video'); } We then have a setter method to set the video ID (assuming we know what the video ID is). public function setVideoId( $vid ) { $this->video_id = $vid; } Then, we have a useful setter method that parses the YouTube URL, extracts the video ID from it, and sets the class variable accordingly. In this case, if no video ID is found in the URL, it uses a clip from the TV series "Dinosaurs" as a default video. public function setVideoIdFromURL( $url ) { $data = array(); parse_str( parse_url($url, PHP_URL_QUERY), $data ); $this->video_id = $this->registry->getObject('db')- >sanitizeData(isset( $data['v'] ) ? $data['v'] : '7NzzzcOWPH0'); } This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Statuses—Other Media [ 246 ] Finally we have our save method, which works in the same way as the image model. /** * Save the video status * @return void */ public function save() { // save the parent object and thus the status table parent::save(); // grab the newly inserted status ID $id = $this->getID(); // insert into the video status table, using the same ID $extended = array(); $extended['id'] = $id; $extended['video_id'] = $this->video_id; $this->registry->getObject('db')->insertRecords( 'statuses_videos', $extended ); } } ?> Links When sharing links with other users we need to at least store the URL itself. We could also store a brief description of the link or even an image from the site. If we wished, we could automatically populate this information from the link. However, for the moment, we will stick to just storing the link and a brief description of it. Database Our video statuses table requires three elds: an ID, the URL of the link, and the name or description of the link. Field Type Description ID Integer, Primary key To relate to the main statuses table URL Varchar The link itself Description Varchar Description of the link This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 8 [ 247 ] Model As with the video and image types, we require a simple model to extend our statuses model (models/videostatus.php). <?php /** * Link status object * extends the base status object */ class Linkstatus extends status { private $url; private $description; Our constructor needs to set the registry, call the parent class' constructor, and then set the type of status by calling the parent's setTypeReference method. /** * Constructor * @param Registry $registry * @param int $id * @return void */ public function __construct( Registry $registry, $id = 0 ) { $this->registry = $registry; parent::__construct( $this->registry, $id ); parent::setTypeReference('link'); } We then have setters for the variables we are extending onto the class. /** * Set the URL * @param String $url * @return void */ public function setURL( $url ) { $this->url = $url; } /** * Set the description of the link * @param String $description This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Statuses—Other Media [ 248 ] * @return void */ public function setDescription( $description ) { $this->description = $description; } Finally, our save method needs to call the parent object's save method, to save the status record. It then gets the ID of the status and creates a record in the statuses_links table, relating the core status with the custom link data. /** * Save the link status * @return void */ public function save() { // save the parent object and thus the status table parent::save(); // grab the newly inserted status ID $id = $this->getID(); // insert into the link status table, using the same ID $extended = array(); $extended['id'] = $id; $extended['URL'] = $this->url; $extended['description'] = $this->description; $this->registry->getObject('db')->insertRecords( 'statuses_links', $extended ); } } ?> Extending the proles With new database tables, status forms, and models in place for the new status types of Dino Space, we need to extend our proles to save these new statuses, and also to include the information from these additional tables in the prole's statuses. We already have provisions for including different templates depending on the type of the status (as per Chapter 6), so we just need to alter our status stream query. This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 8 [ 249 ] Processing the new status posts In our prole statuses controller (controllers/profile/ profilestatusescontroller.php ) in the addStatus method, in the two instances where we construct the status object, we instead need to check which type of status is being posted, and if it is a different status type, we must also include that le, and instead construct that object. if( isset( $_POST['status_type'] ) && $_POST['status_type'] != 'update' ) { if( $_POST['status_type'] == 'image' ) { require_once( FRAMEWORK_PATH . 'models/imagestatus.php' ); $status = new Imagestatus( $this->registry, 0 ); $status->processImage( 'image_file' ); } elseif( $_POST['status_type'] == 'video' ) { require_once( FRAMEWORK_PATH . 'models/videostatus.php' ); $status = new Videostatus( $this->registry, 0 ); $status->setVideoIdFromURL( $_POST['video_url'] ); } elseif( $_POST['status_type'] == 'link' ) { require_once( FRAMEWORK_PATH . 'models/linkstatus.php' ); $status = new Linkstatus( $this->registry, 0 ); $status->setURL( $this->registry->getObject('db')- >sanitizeData( $_POST['link_url'] ) ); $status->setDescription( $this->registry->getObject('db')- >sanitizeData( $_POST['link_description'] ) ); } } else { $status = new Status( $this->registry, 0 ); } This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Statuses—Other Media [ 250 ] Altering our prole status' query Our updates query (in the prole statuses controller) needs to be altered to left join onto the various extended statuses tables, pulling in additional information where appropriate, like the following code: $sql = "SELECT t.type_reference, t.type_name, s.*, pa.name as poster_name, i.image, v.video_id, l.URL, l.description FROM status_types t, profile p, profile pa, statuses s LEFT JOIN statuses_images i ON s.ID=i.id LEFT JOIN statuses_videos v ON s.ID=v.id LEFT JOIN statuses_links l ON s.ID=l.id WHERE t.ID=s.type AND p.user_id=s.profile AND pa.user_id=s.poster AND p.user_id={$user} ORDER BY s.ID DESC LIMIT 20"; Status views Next, we need to create the various template les to be included to display the relevant status information. Images It is saved as views/default/templates/profile/updates/image.tpl.php. <p><strong>{poster_name}</strong>: posted an image "{update}"</p> <img src="uploads/statusimages/{image}" alt="Image" /> <! START comments-{ID} > <p>&nbsp;Comments:</p> <p>&nbsp;{comment} by {commenter}</p> <! END comments-{ID} > Video It is saved as views/default/templates/profile/updates/video.tpl.php. <p><strong>{poster_name}</strong>: posted an video "{update}"</p> <object width="200" height="164"><param name="movie" value="http://www.youtube.com/v/{video_id}&amp;hl=en_GB&amp;fs=1? rel=0&amp;border=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{video_id}&amp;hl=en_GB&amp;fs=1?rel= 0&amp;border=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="200" height="164"></embed></object> <! START comments-{ID} > <p>&nbsp;Comments:</p> <p>&nbsp;{comment} by {commenter}</p> <! END comments-{ID} > This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com Chapter 8 [ 251 ] Links It is saved as views/default/templates/profile/updates/link.tpl.php. <p><strong>{poster_name}</strong>: posted an link "{update}"</p> <a href="{URL}">{description}</a> <! START comments-{ID} > <p>&nbsp;Comments:</p> <p>&nbsp;{comment} by {commenter}</p> <! END comments-{ID} > In action Let's now take a look at our prole with these new status types on! Images After posting a status update with an image, the image is resized and displayed beneath the status. This material is copyright and is licensed for the sole use by RAYMOND ERAZO on 25th October 2010 3146 KERNAN LAKE CIRCLE, JACKSONVILLE, 32246 Download from www.eBookTM.com . { $im->resizeScaleWidth( 150 ); $im->save( $this->registry->getSetting('upload_path') . 'statusimages/' . $im->getName() ); $this->image = $im->getName(); . FRAMEWORK_PATH . 'models/linkstatus .php& apos; ); $status = new Linkstatus( $this->registry, 0 ); $status->setURL( $this->registry->getObject('db' )- >sanitizeData( $_POST['link_url']. { $data = array(); parse_str( parse_url($url, PHP_ URL_QUERY), $data ); $this->video_id = $this->registry->getObject('db' )- >sanitizeData(isset( $data['v']

Ngày đăng: 04/07/2014, 21:20