function addToSearchableObjects($wasNew = false)
 {
     $columns_to_drop = array();
     if ($wasNew) {
         $columns_to_drop = $this->getSearchableColumns();
     } else {
         foreach ($this->getSearchableColumns() as $column_name) {
             if (isset($this->searchable_composite_columns[$column_name])) {
                 foreach ($this->searchable_composite_columns[$column_name] as $colName) {
                     if ($this->isColumnModified($colName)) {
                         $columns_to_drop[] = $column_name;
                         break;
                     }
                 }
             } else {
                 if ($this->isColumnModified($column_name)) {
                     $columns_to_drop[] = $column_name;
                 }
             }
         }
     }
     if (count($columns_to_drop) > 0) {
         if (!$wasNew) {
             SearchableObjects::dropContentByObjectColumns($this, $columns_to_drop);
         }
         foreach ($columns_to_drop as $column_name) {
             $content = $this->getSearchableColumnContent($column_name);
             if (get_class($this->manager()) == 'ProjectFiles') {
                 $content = utf8_encode($content);
             }
             if (trim($content) != '') {
                 $searchable_object = new SearchableObject();
                 $searchable_object->setRelObjectManager(get_class($this->manager()));
                 $searchable_object->setRelObjectId($this->getObjectId());
                 $searchable_object->setColumnName($column_name);
                 $searchable_object->setContent($content);
                 $searchable_object->setProjectId(0);
                 $searchable_object->setIsPrivate(false);
                 $searchable_object->save();
             }
             // if
         }
         // foreach
     }
     // if
     //Add Unique ID to search
     if ($wasNew) {
         SearchableObjects::dropContentByObjectColumns($this, array('uid'));
         // Fixes Query failed with message 'Duplicate entry 'xxxxx-31-uid' for key 1'
         $searchable_object = new SearchableObject();
         $searchable_object->setRelObjectManager(get_class($this->manager()));
         $searchable_object->setRelObjectId($this->getObjectId());
         $searchable_object->setColumnName('uid');
         $searchable_object->setContent($this->getUniqueObjectId());
         $searchable_object->setProjectId(0);
         $searchable_object->setIsPrivate(false);
         $searchable_object->save();
     }
 }
 /**
  * Save object. If object is searchable this function will add conetent of searchable fields 
  * to search index
  *
  * @param void
  * @return boolean
  */
 function save()
 {
     $result = parent::save();
     // If searchable refresh content in search table
     if ($this->isSearchable()) {
         SearchableObjects::dropContentByObject($this);
         $project = $this->getProject();
         foreach ($this->getSearchableColumns() as $column_name) {
             $content = $this->getSearchableColumnContent($column_name);
             if (trim($content) != '') {
                 $searchable_object = new SearchableObject();
                 $searchable_object->setRelObjectManager(get_class($this->manager()));
                 $searchable_object->setRelObjectId($this->getObjectId());
                 $searchable_object->setColumnName($column_name);
                 $searchable_object->setContent($content);
                 if ($project instanceof Project) {
                     $searchable_object->setProjectId($project->getId());
                 }
                 $searchable_object->setIsPrivate($this->isPrivate());
                 $searchable_object->save();
             }
             // if
         }
         // if
     }
     // if
     return $result;
 }
Exemplo n.º 3
0
 function addToSearchableObjects($wasNew)
 {
     $columns_to_drop = array();
     if ($wasNew) {
         $columns_to_drop = $this->getSearchableColumns();
     } else {
         foreach ($this->getSearchableColumns() as $column_name) {
             if (isset($this->searchable_composite_columns[$column_name])) {
                 foreach ($this->searchable_composite_columns[$column_name] as $colName) {
                     if ($this->isColumnModified($colName)) {
                         $columns_to_drop[] = $column_name;
                         break;
                     }
                 }
             } else {
                 if ($column_name == 'body') {
                     $columns_to_drop[] = $column_name;
                 } else {
                     if ($this->getMailData()->columnExists($column_name) && $this->getMailData()->isColumnModified($column_name)) {
                         $columns_to_drop[] = $column_name;
                     } else {
                         if ($this->isColumnModified($column_name)) {
                             $columns_to_drop[] = $column_name;
                         }
                     }
                 }
             }
         }
     }
     if (count($columns_to_drop) > 0) {
         SearchableObjects::dropContentByObjectColumns($this, $columns_to_drop);
         foreach ($columns_to_drop as $column_name) {
             $content = $this->getSearchableColumnContent($column_name);
             if (trim($content) != '') {
                 $searchable_object = new SearchableObject();
                 $searchable_object->setRelObjectManager(get_class($this->manager()));
                 $searchable_object->setRelObjectId($this->getObjectId());
                 $searchable_object->setColumnName($column_name);
                 $searchable_object->setContent($content);
                 $searchable_object->setProjectId(0);
                 $searchable_object->setIsPrivate(false);
                 $searchable_object->setUserId($this->getAccount() instanceof MailAccount ? $this->getAccount()->getUserId() : 0);
                 $searchable_object->save();
             }
             // if
         }
         // foreach
     }
     // if
     if ($wasNew) {
         SearchableObjects::dropContentByObjectColumns($this, array('uid'));
         $searchable_object = new SearchableObject();
         $searchable_object->setRelObjectManager(get_class($this->manager()));
         $searchable_object->setRelObjectId($this->getObjectId());
         $searchable_object->setColumnName('uid');
         $searchable_object->setContent($this->getUniqueObjectId());
         $searchable_object->setProjectId(0);
         $searchable_object->setIsPrivate(false);
         $searchable_object->setUserId($this->getAccount()->getUserId());
         $searchable_object->save();
     }
 }
 /**
  * Adds the custom properties of an object into the database.
  *
  * @param $object
  * @return unknown_type
  */
 function add_custom_properties($object)
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $obj_custom_properties = array_var($_POST, 'object_custom_properties');
     $customProps = CustomProperties::getAllCustomPropertiesByObjectType(get_class($object->manager()));
     //Sets all boolean custom properties to 0. If any boolean properties are returned, they are subsequently set to 1.
     foreach ($customProps as $cp) {
         if ($cp->getType() == 'boolean') {
             $custom_property_value = CustomPropertyValues::getCustomPropertyValue($object->getId(), $cp->getId());
             if (!$custom_property_value instanceof CustomPropertyValue) {
                 $custom_property_value = new CustomPropertyValue();
             }
             $custom_property_value->setObjectId($object->getId());
             $custom_property_value->setCustomPropertyId($cp->getId());
             $custom_property_value->setValue(0);
             $custom_property_value->save();
         }
     }
     if (is_array($obj_custom_properties)) {
         foreach ($obj_custom_properties as $id => $value) {
             //Get the custom property
             $custom_property = null;
             foreach ($customProps as $cp) {
                 if ($cp->getId() == $id) {
                     $custom_property = $cp;
                     break;
                 }
             }
             if ($custom_property instanceof CustomProperty) {
                 // save dates in standard format "Y-m-d H:i:s", because the column type is string
                 if ($custom_property->getType() == 'date') {
                     if (is_array($value)) {
                         $newValues = array();
                         foreach ($value as $val) {
                             $dtv = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $val);
                             $newValues[] = $dtv->format("Y-m-d H:i:s");
                         }
                         $value = $newValues;
                     } else {
                         $dtv = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $value);
                         $value = $dtv->format("Y-m-d H:i:s");
                     }
                 }
                 //Save multiple values
                 if (is_array($value)) {
                     CustomPropertyValues::deleteCustomPropertyValues($object->getId(), $id);
                     foreach ($value as &$val) {
                         if (is_array($val)) {
                             // CP type == table
                             $str_val = '';
                             foreach ($val as $col_val) {
                                 $col_val = str_replace("|", "\\|", $col_val);
                                 $str_val .= ($str_val == '' ? '' : '|') . $col_val;
                             }
                             $val = $str_val;
                         }
                         if ($val != '') {
                             if (strpos($val, ',')) {
                                 $val = str_replace(',', '|', $val);
                             }
                             $custom_property_value = new CustomPropertyValue();
                             $custom_property_value->setObjectId($object->getId());
                             $custom_property_value->setCustomPropertyId($id);
                             $custom_property_value->setValue($val);
                             $custom_property_value->save();
                         }
                     }
                 } else {
                     if ($custom_property->getType() == 'boolean') {
                         $value = isset($value);
                     }
                     $cpv = CustomPropertyValues::getCustomPropertyValue($object->getId(), $id);
                     if ($cpv instanceof CustomPropertyValue) {
                         $custom_property_value = $cpv;
                     } else {
                         $custom_property_value = new CustomPropertyValue();
                     }
                     $custom_property_value->setObjectId($object->getId());
                     $custom_property_value->setCustomPropertyId($id);
                     $custom_property_value->setValue($value);
                     $custom_property_value->save();
                 }
                 //Add to searchable objects
                 if ($object->isSearchable() && ($custom_property->getType() == 'text' || $custom_property->getType() == 'list' || $custom_property->getType() == 'numeric')) {
                     $name = $custom_property->getName();
                     $searchable_object = SearchableObjects::findOne(array("conditions" => "`rel_object_manager` = '" . get_class($object->manager()) . "' AND `rel_object_id` = " . $object->getId() . " AND `column_name` = '{$name}'"));
                     if (!$searchable_object) {
                         $searchable_object = new SearchableObject();
                     }
                     if (is_array($value)) {
                         $value = implode(', ', $value);
                     }
                     $searchable_object->setRelObjectManager(get_class($object->manager()));
                     $searchable_object->setRelObjectId($object->getId());
                     $searchable_object->setColumnName($name);
                     $searchable_object->setContent($value);
                     $searchable_object->setProjectId(0);
                     $searchable_object->setIsPrivate(false);
                     $searchable_object->save();
                 }
             }
         }
     }
     //Save the key - value pair custom properties (custom properties table)
     $object->clearObjectProperties();
     $names = array_var($_POST, 'custom_prop_names');
     $values = array_var($_POST, 'custom_prop_values');
     if (!is_array($names)) {
         return;
     }
     for ($i = 0; $i < count($names); $i++) {
         $name = trim($names[$i]);
         $value = trim($values[$i]);
         if ($name != '' && $value != '') {
             $property = new ObjectProperty();
             $property->setObject($object);
             $property->setPropertyName($name);
             $property->setPropertyValue($value);
             $property->save();
             if ($object->isSearchable()) {
                 $object->addPropertyToSearchableObject($property);
             }
         }
     }
 }