SiteController.php 3.19 KB
Newer Older
JULIO JARAMILLO's avatar
JULIO JARAMILLO committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
<?php

class SiteController extends CController implements IWebServiceProvider
{
	/**
	 * Declares the 'phonebook' Web service action.
	 */
	public function actions()
	{
		return array(
			'phonebook'=>array(
				'class'=>'CWebServiceAction',
				'classMap'=>array(
					'Contact',
				),
			),
		);
	}

	/**
	 * This is the default action that displays the phonebook Flex client.
	 */
	public function actionIndex()
	{
		$this->render('index');
	}

	/**
	 * This action serves as a SOAP client to test the phonebook Web service.
	 */
	public function actionTest()
	{
		$wsdlUrl=Yii::app()->request->hostInfo.$this->createUrl('phonebook');
		$client=new SoapClient($wsdlUrl);
		echo "<pre>";
		echo "login...\n";
		$client->login('demo','demo');
		echo "fetching all contacts\n";
		print_r($client->getContacts());
		echo "\ninserting a new contact...";
		$contact=new Contact;
		$contact->name='Tester Name';
		$contact->phone='123-123-1234';
		$client->saveContact($contact);
		echo "done\n\n";
		echo "fetching all contacts\n";
		print_r($client->getContacts());
		echo "</pre>";
	}

	/**
	 * This method is required by IWebServiceProvider.
	 * It makes sure the user is logged in before making changes to data.
	 * @param CWebService the currently requested Web service.
	 * @return boolean whether the remote method should be executed.
	 */
	public function beforeWebMethod($service)
	{
		$safeMethods=array(
			'login',
			'getContacts',
		);
		$pattern='/^('.implode('|',$safeMethods).')$/i';
		if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))
			return true;
		else
			throw new CException('Login required.');
	}

	/**
	 * This method is required by IWebServiceProvider.
	 * @param CWebService the currently requested Web service.
	 */
	public function afterWebMethod($service)
	{
	}

	/*** The following methods are Web service APIs ***/

	/**
	 * @param string username
	 * @param string password
	 * @return boolean whether login is valid
	 * @soap
	 */
	public function login($username,$password)
	{
		$identity=new UserIdentity($username,$password);
		if($identity->authenticate())
			Yii::app()->user->login($identity);
		return $identity->isAuthenticated;
	}

	/**
	 * Returns all contact records.
	 * @return Contact[] the contact records
	 * @soap
	 */
	public function getContacts()
	{
		return Contact::model()->findAll();
	}

	/**
	 * Updates or inserts a contact.
	 * If the ID is null, an insertion will be performed;
	 * Otherwise it updates the existing one.
	 * @param Contact contact model
	 * @return boolean whether saving is successful
	 * @soap
	 */
	public function saveContact($contact)
	{
		if($contact->id > 0) // update
		{
			$contact->isNewRecord=false;
			if(($oldContact=Contact::model()->findByPk($contact->id))!==null)
			{
				$oldContact->attributes=$contact->attributes;
				return $oldContact->save();
			}
			else
				return false;
		}
		else // insert
		{
			$contact->isNewRecord=true;
			$contact->id=null;
			return $contact->save();
		}
	}

	/**
	 * Deletes the specified contact record.
	 * @param integer ID of the contact to be deleted
	 * @return integer number of records deleted
	 * @soap
	 */
	public function deleteContact($id)
	{
		return Contact::model()->deleteByPk($id);
	}
}