[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/customfields/customfield.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/dao/customfields/customfieldsvalues.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/dao/daocacheconstants.properties.php" ); 7 8 /** 9 * different custom field types available 10 * 11 * \ingroup DAO 12 */ 13 define( "CUSTOM_FIELD_TEXTBOX", 1 ); 14 define( "CUSTOM_FIELD_TEXTAREA", 2 ); 15 define( "CUSTOM_FIELD_CHECKBOX", 3 ); 16 define( "CUSTOM_FIELD_DATE", 4 ); 17 define( "CUSTOM_FIELD_LIST", 5 ); 18 define( "CUSTOM_FIELD_MULTILIST", 6 ); 19 20 /** 21 * Model for the custom fields 22 */ 23 class CustomFields extends Model 24 { 25 26 function CustomFields() 27 { 28 $this->Model(); 29 $this->table = $this->getPrefix()."custom_fields_definition"; 30 } 31 32 /** 33 * returns a custom field given its id 34 * 35 * @param id The id of the custom field 36 * @return A CustomField object with information about the custom field 37 */ 38 function getCustomField( $id ) 39 { 40 return( $this->get( "id", $id, CACHE_CUSTOMFIELDS )); 41 } 42 43 /** 44 * returns a custom field given its name and the blog where it belongs 45 * 46 * @param blogId The id of the blog where this field belongs 47 * @param fieldName The name of the custom field 48 * @return A CustomField object with information about the custom field 49 */ 50 function getCustomFieldByName( $blogId, $fieldName ) 51 { 52 $blogFields = $this->getMany( "blog_id", 53 $blogId, 54 CACHE_CUSTOMFIELDS_BLOG, 55 Array( CACHE_CUSTOMFIELDS => "getId" ), 56 Array( "field_name" => "ASC" )); 57 if( !$blogFields ) 58 return false; 59 foreach( $blogFields as $field ) { 60 if( $field->getName() == $fieldName ) { 61 // we can return right away, no need to bother... 62 return( $field ); 63 } 64 } 65 } 66 67 /** 68 * returns all the custom fields defined in a blog 69 * 70 * @param blogId The id of the blog 71 * @param includeHidden Whether to return the fields that have been marked as 72 * hidden or not. 73 * @param page 74 * @param itemsPerPage 75 * @return An array of CustomField objects with information about the different 76 * custom fields defined. 77 */ 78 function getBlogCustomFields( $blogId, $includeHidden = true, $page = -1, $itemsPerPage = 15 ) 79 { 80 $blogFields = $this->getMany( "blog_id", 81 $blogId, 82 CACHE_CUSTOMFIELDS_BLOG, 83 Array( CACHE_CUSTOMFIELDS => "getId" ), 84 Array( "field_name" => "ASC" ), 85 "", 86 $page, 87 $itemsPerPage ); 88 89 if( !$blogFields ) 90 return( Array()); 91 92 // if we have to include the hidden fields, we can return the whole array as 93 // those are already there 94 if( $includeHidden ) 95 return( $blogFields ); 96 97 // if not, filter out the non-hidden 98 $result = Array(); 99 foreach( $blogFields as $field ) { 100 if( !$field->isHidden()) 101 $result[] = $field; 102 } 103 104 return( $result ); 105 } 106 107 /** 108 * returns the number of custom fields defined for the blog 109 * 110 * @param blogId 111 * @param includeHidden 112 */ 113 function getNumBlogCustomFields( $blogId, $includeHidden = true ) 114 { 115 return( count( $this->getBlogCustomFields( $blogId, $includeHidden ))); 116 } 117 118 /** 119 * adds a custom field to the database 120 * 121 * @param field A CustomField object 122 * @return True if successful or false otherwise. 123 */ 124 function addCustomField( &$field ) 125 { 126 // does the field already exist? 127 $existingField = $this->getCustomFieldByName( $field->getBlogId(), $field->getName()); 128 if( $existingField ) // it already exists, we cannot add it! 129 return false; 130 131 if( $result = $this->add( $field )) { 132 // clean the caches 133 $this->_cache->removeData( $field->getId(), CACHE_CUSTOMFIELDS ); 134 $this->_cache->removeData( $field->getBlogId(), CACHE_CUSTOMFIELDS_BLOG ); 135 } 136 return( $result ); 137 } 138 139 /** 140 * removes a custom field, but also all the values that have been created 141 * based on this field and that have been assigned to different articles. 142 * Otherwise, we would have data which is not linked to any article... but if still 143 * needed, set the second parameter to false 144 * 145 * @param id 146 * @param deleteValues 147 * @return Returns true if successful or false otherwise 148 */ 149 function removeCustomField( $id, $deleteValues = true ) 150 { 151 $field = $this->getCustomField( $id ); 152 153 if( !$field ) 154 return false; 155 156 if( !$this->delete( "id", $id )) 157 return false; 158 159 // clean the caches 160 $this->_cache->removeData( $field->getId(), CACHE_CUSTOMFIELDS ); 161 $this->_cache->removeData( $field->getBlogId(), CACHE_CUSTOMFIELDS_BLOG ); 162 163 if( !$deleteValues ) 164 return true; 165 166 // remove the values that were associated to this field 167 $fieldValues = new CustomFieldsValues(); 168 return( $fieldValues->removeCustomFieldValues( $id )); 169 } 170 171 /** 172 * update a field in the database 173 * 174 * @param field 175 * @return True if successful or false otherwise 176 */ 177 function updateCustomField( $field ) 178 { 179 if( ($result = $this->update( $field ))) { 180 // clean the caches 181 $this->_cache->removeData( $field->getId(), CACHE_CUSTOMFIELDS ); 182 $this->_cache->removeData( $field->getBlogId(), CACHE_CUSTOMFIELDS_BLOG ); 183 } 184 return( $result ); 185 } 186 187 /** 188 * @private 189 */ 190 function mapRow( $row ) 191 { 192 $field = new CustomField( $row["field_name"], 193 $row["field_description"], 194 $row["field_type"], 195 $row["blog_id"], 196 $row["hidden"], 197 $row["searchable"], 198 $row["id"] ); 199 // set the field with the possible values, but check first if it can be unserialized before we get an error 200 !isset( $row["field_values"] ) ? $values = Array() : $values = unserialize( $row["field_values"] ); 201 202 203 $field->setFieldValues( $values ); 204 205 return $field; 206 } 207 } 208 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |