00001 <?
00010 class BaseGroup extends MyObject
00011 {
00015 public $joinerTable;
00016
00020 public $joinedObject;
00021
00025 public $members = array();
00026
00035 public function __construct($data = null, $table = 'groups', $joinerTable = 'users_to_groups', $joinedObject = 'User')
00036 {
00037 parent::__construct($data, $table);
00038
00039 $this->joinerTable = $joinerTable;
00040 $this->joinedObject = $joinedObject;
00041
00042
00043 $this->fullTextFields[] = 'name';
00044 $this->fullTextFields[] = 'description';
00045 }
00046
00052 public function getPagesXml()
00053 {
00054 $xml = parent::getPagesXml();
00055
00056 $xml .= <<<XML
00057 <page name="join">
00058 <param name="id" type="int" required="1"/>
00059 </page>
00060 <page name="leave">
00061 <param name="id" type="int" required="1"/>
00062 </page>
00063 XML;
00064 return $xml;
00065 }
00066
00071 public function isMember($id)
00072 {
00073 return ($this->members[$id] instanceOf $this->joinedObject);
00074 }
00075
00081 public function addMember($id)
00082 {
00083
00084 $this->removeMember($id);
00085
00086
00087 dbExecute("
00088 INSERT INTO $this->joinerTable
00089 (object_id, group_id, join_date)
00090 VALUES
00091 ('$id', '$this->id', NOW())
00092 ");
00093
00094
00095 $this->members[$id] = new $this->joinedObject($id);
00096 }
00097
00103 public function removeMember($id)
00104 {
00105
00106 dbExecute("
00107 DELETE FROM $this->joinerTable
00108 WHERE object_id = '$id'
00109 AND group_id = '$this->id'
00110 ");
00111
00112
00113 unset($this->members[$id]);
00114 }
00115
00121 public function getGroupMemberIds()
00122 {
00123 $ids = array();
00124 $rs = dbQuery("
00125 SELECT object_id
00126 FROM $this->joinerTable
00127 WHERE group_id = '$this->id'
00128 ");
00129 while ($ar = dbFetchAssoc($rs))
00130 $ids[] = $ar['object_id'];
00131
00132 return $ids;
00133 }
00134
00140 public function canJoin()
00141 {
00142 global $me;
00143
00144 return !$this->isMember($me->id);
00145 }
00146
00152 public function canLeave()
00153 {
00154 global $me;
00155
00156 return $this->isMember($me->id);
00157 }
00158
00162 public function initJoinPage()
00163 {
00164 $this->assertLogin();
00165
00166 $this->id = $this->params('id');
00167 $this->pageTitle = 'Join Group - ' . $this->getName();
00168
00169 if (!$this->canJoin())
00170 throw new PageError('You cannot join this group.');
00171 }
00172
00176 public function drawJoinPage()
00177 {
00178 global $me;
00179
00180 $this->addMember($me->id);
00181
00182 echo "<p>You are now a member of " . $this->getName(true) . "</p>";
00183 }
00184
00188 public function initLeavePage()
00189 {
00190 $this->assertLogin();
00191
00192 $this->id = $this->params('id');
00193 $this->pageTitle = 'Leave Group - ' . $this->getName();
00194
00195 if (!$this->canLeave())
00196 throw new PageError('You cannot leave this group.');
00197 }
00198
00202 public function drawLeavePage()
00203 {
00204 global $me;
00205
00206 $this->removeMember($me->id);
00207
00208 echo "<p>You are no longer a member of " . $this->getName(true) . "</p>";
00209 }
00210
00214 public function getName($link = false)
00215 {
00216 return parent::getName($link, $this->name);
00217 }
00218
00222 public function editFormAddFields($form)
00223 {
00224 parent::editFormAddFields($form);
00225
00226 $form->add('TextField', 'name', array(
00227 'required' => true,
00228 'width' => '100%'
00229 ));
00230 $form->add('EditorField', 'description', array(
00231 'required' => true,
00232 'width' => '100%',
00233 'height' => '125px'
00234 ));
00235
00236 return $form;
00237 }
00238
00242 protected function editPagePostSuccess($form)
00243 {
00244 global $me;
00245
00246 $this->addMember($me->id);
00247
00248 parent::editPagePostSuccess($form);
00249 }
00250
00257 public function getCreateTableSql()
00258 {
00259 $sql = parent::getCreateTableSql();
00260
00261 return $sql;
00262 }
00263
00267 public function drawCreateTablePage()
00268 {
00269 parent::drawCreateTablePage();
00270
00271 echo "<p>" . nl2br($this->getJoinerTableSql()). "</p>";
00272 }
00273
00277 public function runCreateTableSql()
00278 {
00279 dbExecute($this->getJoinerTableSql());
00280
00281 parent::runCreateTableSql();
00282 }
00283
00289 public function getJoinerTableSql()
00290 {
00291
00292 $fields = implode(",\n", $this->getJoinerFieldsArray());
00293 $indexes = implode(",\n", $this->getJoinerIndexesArray());
00294
00295 return "CREATE TABLE IF NOT EXISTS $this->joinerTable\n(\n$fields,\n$indexes\n);";
00296 }
00297
00301 public function getCreateFieldsArray()
00302 {
00303 $array = parent::getCreateFieldsArray();
00304
00305 $array['name'] = "name varchar(255) default '' not null";
00306 $array['description'] = "description text default '' not null";
00307
00308 return $array;
00309 }
00310
00316 public function getJoinerFieldsArray()
00317 {
00318 return array(
00319 'object_id' => 'object_id int(11) default 0 not null',
00320 'group_id' => 'group_id int(11) default 0 not null',
00321 'join_date' => 'join_date datetime not null',
00322 );
00323 }
00324
00330 public function getJoinerIndexesArray()
00331 {
00332 return array(
00333 'object_id' => 'KEY (object_id)',
00334 'group_id' => 'KEY (group_id)'
00335 );
00336 }
00337
00341 public function getRssItem()
00342 {
00343 $item = parent::getRssItem();
00344
00345 $item->title = $this->name;
00346 $item->description = $this->description;
00347
00348 return $item;
00349 }
00350
00354 public function getPublicData()
00355 {
00356 $data = parent::getPublicData();
00357
00358 $data['name'] = $this->name;
00359 $data['description'] = $this->description;
00360
00361 return $data;
00362 }
00363 }
00364 ?>