Beispiel #1
0
 /**
  *
  * @return [json Map] list
  */
 public static function insertData($row, $collection, $type, $isDummy, $linkAllToActiveUser)
 {
     $error = null;
     if (isset($row["_id"]) && isset($row["_id"]['$oid']) && PHDB::isValidMongoId($row["_id"]['$oid'])) {
         $id = $row["_id"]['$oid'];
         unset($row["_id"]);
         try {
             $id = new MongoId($id);
         } catch (MongoException $ex) {
             $id = new MongoId();
         }
     } else {
         $id = new MongoId();
     }
     $row["_id"] = $id;
     /* **************************************
      * always add a key to easily detect and remove dummy data 
      ***************************************** */
     if ($isDummy) {
         $row["dummyData"] = $type;
     }
     if (!$collection) {
         $collection = $fn;
     }
     /* **************************************
      *	Building links with the active User 
      ***************************************** */
     $where = array("_id" => new MongoId((string) $row["_id"]));
     $exist = PHDB::findOne($collection, $where) ? true : false;
     $info = array("collection" => $collection, "id" => (string) $row["_id"], "exist" => $exist, "msg" => array());
     $userId = Yii::app()->session["userId"];
     if ($linkAllToActiveUser) {
         $personType = array("type" => PHType::TYPE_CITOYEN, "isAdmin" => true);
         if (!isset($row["dontLink"]) || !$row["dontLink"]) {
             //Add creator to collection
             $row["creator"] = $userId;
             if ($collection == PHType::TYPE_CITOYEN) {
                 /* **************************************
                  *	KNOWS links for people
                  ***************************************** */
                 if (isset($row["links"])) {
                     if (isset($row["links"]["knows"])) {
                         // Pas d'admin pour le knows
                         unset($personType["isAdmin"]);
                         $row["links"]["knows"][$userId] = $personType;
                     } else {
                         $knows = array();
                         $knows[$userId] = $personType;
                         $row["links"]["knows"] = $knows;
                     }
                 } else {
                     $knows = array();
                     $knows[$userId] = $personType;
                     $row["links"] = array("knows" => $knows);
                 }
                 PHDB::update(PHType::TYPE_CITOYEN, array("_id" => new MongoId($userId)), array('$set' => array(Link::person2person . "." . (string) $row["_id"] => array("type" => $collection))));
                 array_push($info["msg"], "added links.knows activeUser");
             } elseif ($collection == PHType::TYPE_ORGANIZATIONS) {
                 /* **************************************
                  *	MEMBERS links for ORGANISATION
                  ***************************************** */
                 if (isset($row["links"])) {
                     if (isset($row["links"]["members"])) {
                         $row["links"]["members"][$userId] = $personType;
                     } else {
                         $members = array();
                         $members[$userId] = $personType;
                         $row["links"]["members"] = $members;
                     }
                 } else {
                     $members = array();
                     $members[$userId] = $personType;
                     $row["links"] = array("members" => $members);
                 }
                 PHDB::update(PHType::TYPE_CITOYEN, array("_id" => new MongoId($userId)), array('$set' => array(Link::person2organization . "." . (string) $row["_id"] => array("type" => $collection, "isAdmin" => true))));
                 array_push($info["msg"], "added links.members and memberOf for activeUser");
             } elseif ($collection == PHType::TYPE_EVENTS) {
                 /* **************************************
                  *	ATTENDEES links for events
                  ***************************************** */
                 if (isset($row["links"])) {
                     if (isset($row["links"]["attendees"])) {
                         $row["links"]["attendees"][$userId] = $personType;
                     } else {
                         $attendees = array();
                         $attendees[$userId] = $personType;
                         $row["links"]["attendees"] = $attendees;
                     }
                 } else {
                     $attendees = array();
                     $attendees[$userId] = $personType;
                     $row["links"] = array("attendees" => $attendees);
                 }
                 PHDB::update(PHType::TYPE_CITOYEN, array("_id" => new MongoId($userId)), array('$set' => array(Link::person2events . "." . (string) $row["_id"] => array("type" => $collection))));
                 array_push($info["msg"], "added links.events for activeUser");
             } elseif ($collection == PHType::TYPE_PROJECTS) {
                 /* **************************************
                  *	CONTRIBUTORS links for projects
                  ***************************************** */
                 if (isset($row["links"])) {
                     if (isset($row["links"]["contributors"])) {
                         $row["links"]["contributors"][$userId] = $personType;
                     } else {
                         $contributors = array();
                         $contributors[$userId] = $personType;
                         $row["links"]["contributors"] = $contributors;
                     }
                 } else {
                     $contributors = array();
                     $contributors[$userId] = $personType;
                     $row["links"] = array("contributors" => $contributors);
                 }
                 PHDB::update(PHType::TYPE_CITOYEN, array("_id" => new MongoId($userId)), array('$set' => array(Link::person2projects . "." . (string) $row["_id"] => array("type" => $collection))));
                 array_push($info["msg"], "added links.projects for activeUser");
             }
         }
     }
     if ($exist) {
         /* **************************************
          *	remove to renew existing data
          ***************************************** */
         PHDB::remove($collection, $where);
         array_push($info["msg"], "existed removed");
     }
     try {
         /* **************************************
          *	Create entry in DB
          ***************************************** */
         PHDB::insert($collection, $row);
         array_push($info["msg"], "inserted");
     } catch (MongoException $ex) {
         $error = array("id" => (string) $row["_id"], "exist" => $exist, "msg" => $ex->getMessage());
     }
     return array("info" => $info, "error" => $error);
 }