Пример #1
0
 /**
  * Retrieve a player.
  *
  * @param mixed Player name or ID.
  * @param bool (Optional) Boolean indicating if the player should be automatically created.  When
  *              $Name is numeric (an ID), this argument has no effect.
  * @return object my5280_Player object
  */
 public function getPlayer($Name, $Create = true)
 {
     global $connections;
     $cnRetrieve = $connections->retrieve;
     // Handle a numeric name
     if (is_numeric($Name)) {
         $entry = $cnRetrieve->entry($Name);
         if (!$entry) {
             return null;
         }
     } elseif (is_string($Name)) {
         // Parse the name
         $parts = explode(',', $Name);
         $count = count($parts);
         if ($count == 2) {
             $firstName = trim($parts[1]);
             $lastName = trim($parts[0]);
         } elseif ($count == 1) {
             $parts = explode(' ', $Name);
             $count = count($parts);
             $firstName = $parts[0];
             if ($count >= 2) {
                 $lastName = implode(' ', array_slice($parts, 1));
             } else {
                 $lastName = null;
             }
         } else {
             $firstName = $Name;
             $lastName = null;
         }
         // Search for the contact
         $ret = $cnRetrieve->entries(array('first_name' => $firstName, 'last_name' => $lastName));
         // Get the matching entry
         $entry = null;
         foreach ($ret as $possible) {
             if (strcasecmp($possible->first_name, $firstName) == 0 && strcasecmp($possible->last_name, $lastName) == 0) {
                 $entry = $possible;
                 break;
             }
         }
         // Create a new entry (if requested)
         if ($entry === null) {
             if ($Create) {
                 // Set basic information
                 $entry = new cnEntry();
                 $entry->setFirstName($firstName);
                 $entry->setLastName($lastName);
                 $entry->setEntryType('individual');
                 $entry->setVisibility('private');
                 $entry->setStatus('approved');
             } else {
                 return null;
             }
         }
     } else {
         throw new Exception("Invalid player identifier: {$Name}");
     }
     // Return the my5280_Player object
     require_once MY5280_PLUGIN_DIR . 'lib/player.php';
     return new my5280_Player($entry);
 }
Пример #2
0
 /**
  * Save the doubles team.
  */
 public function save(&$Error = null)
 {
     // Load existing data
     $this->load();
     // Determine if we are creating or updating
     if (isset($this->cnData->id)) {
         // Use the existing entry
         $entry = new cnEntry($this->cnData);
         $entry->setEntryType($this->cnData->entry_type);
     } else {
         // Initialize the new entry
         $entry = new cnEntry();
         $entry->setFamilyName($this->familyName);
         $entry->setEntryType('family');
         $entry->setVisibility('private');
         $entry->setStatus('approved');
         // Add the family members
         $family = array();
         foreach ($this->familyMembers as $player) {
             $id = $player->getId();
             if (!$id) {
                 $player->save();
                 $id = $player->getId();
             }
             $family[] = array('entry_id' => $id, 'relation' => 'partner');
         }
         $entry->setFamilyMembers($family);
         // Save the entry
         if ($entry->save() && $this->getId() === null) {
             global $connections;
             $this->cnData->id = $connections->lastInsertID;
             $isNew = true;
         } else {
             $isNew = false;
         }
         // Update meta data
         $meta = array();
         foreach ($this->cnMeta as $name => $value) {
             $meta[] = array('key' => $name, 'value' => $value);
         }
         if (count($meta)) {
             cnEntry_Action::meta($isNew ? 'add' : 'update', $this->getId(), $meta);
         }
     }
     return true;
 }